Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
101 commits
Select commit Hold shift + click to select a range
3fbaf80
Bump reactivecircus/android-emulator-runner from 2.30.1 to 2.31.0 (#3…
dependabot[bot] Jul 2, 2024
25f1ca4
Bump codecov/codecov-action from 4.3.1 to 4.5.0 (#3533)
dependabot[bot] Jul 2, 2024
e34c467
[SR] Session Replay (#3339)
romtsn Jul 15, 2024
2937c11
release: 7.12.0
getsentry-bot Jul 15, 2024
9c4170f
Merge branch 'release/7.12.0'
Jul 15, 2024
a449452
Bump github/codeql-action from 3.25.10 to 3.25.11 (#3529)
dependabot[bot] Jul 16, 2024
028e225
Bump JamesIves/github-pages-deploy-action from 4.5.0 to 4.6.1 (#3531)
dependabot[bot] Jul 16, 2024
83b0c04
Bump gradle/actions (#3532)
dependabot[bot] Jul 16, 2024
7620eac
Add sentry-android-replay module to craft and readme (#3578)
romtsn Jul 16, 2024
73237da
Check app start spans time and foreground state (#3550)
stefanosiano Jul 17, 2024
391c199
release: 7.12.1
getsentry-bot Jul 23, 2024
60865fe
Bump github/codeql-action from 3.25.11 to 3.25.13 (#3591)
dependabot[bot] Jul 23, 2024
485ff61
Bump reactivecircus/android-emulator-runner from 2.31.0 to 2.32.0 (#3…
dependabot[bot] Jul 23, 2024
74ed0f6
Bump JamesIves/github-pages-deploy-action from 4.6.1 to 4.6.3 (#3590)
dependabot[bot] Jul 23, 2024
a0423a0
Bump gradle/actions (#3597)
dependabot[bot] Jul 23, 2024
fc84053
Bump gradle/wrapper-validation-action from 3.4.2 to 3.5.0 (#3589)
dependabot[bot] Jul 23, 2024
7ee0493
Merge branch 'release/7.12.1'
Jul 25, 2024
3a89243
Bump Spring Boot to 3.3.2 (#3541)
adinauer Jul 29, 2024
e039872
[SR] Capture Replays for ANRs and crashes (#3565)
romtsn Jul 30, 2024
b64477e
release: 7.13.0
getsentry-bot Jul 30, 2024
ae2294f
Merge branch 'release/7.13.0'
Jul 31, 2024
7c34b37
Fix lazy select queries instrumentation (#3604)
stefanosiano Aug 1, 2024
09dab51
Avoid ArrayIndexOutOfBoundsException on Android cpu data collection (…
stefanosiano Aug 1, 2024
d4b1f82
[SR] ANR with buffered Replay integration test (#3612)
romtsn Aug 2, 2024
9486895
[SR] Buffer mode improvements (#3622)
romtsn Aug 9, 2024
19d98e8
[SR] Gesture/touch support for Flutter (#3623)
romtsn Aug 9, 2024
32eed6a
Fix app start spans missing from Pixel devices (#3634)
stefanosiano Aug 12, 2024
f6e97b1
[SR] Fix Session Replay crashes (#3628)
romtsn Aug 12, 2024
65295e4
release: 7.14.0
getsentry-bot Aug 12, 2024
9762f09
Merge branch 'release/7.14.0'
Aug 13, 2024
a22aea0
Verify sentry-android-replay for Google Play SDK Console (#3651)
markushi Aug 29, 2024
014dbef
[SR] Rename errorSampleRate to onErrorSampleRate (#3637)
romtsn Sep 3, 2024
70d1da1
Avoid stopping appStartProfiler after application creation (#3630)
stefanosiano Sep 3, 2024
1aaf7d1
Bump JamesIves/github-pages-deploy-action from 4.6.3 to 4.6.4 (#3681)
dependabot[bot] Sep 9, 2024
8586d1f
Bump github/codeql-action from 3.25.13 to 3.26.6 (#3672)
dependabot[bot] Sep 9, 2024
731ae5a
[SR] Detect dominant color for TextViews with Spans (#3682)
romtsn Sep 11, 2024
9e9e16d
Bump github/codeql-action from 3.26.6 to 3.26.7 (#3692)
dependabot[bot] Sep 16, 2024
80d5908
Bump gradle/actions (#3691)
dependabot[bot] Sep 16, 2024
6368d4f
[SR] Add custom redaction options (#3689)
romtsn Sep 16, 2024
61c8d80
Add support for `feedback` envelope header item type (#3687)
denrase Sep 17, 2024
6c8acb8
Bump github/codeql-action from 3.26.7 to 3.26.8 (#3708)
dependabot[bot] Sep 25, 2024
b11dc55
Ensure app context is used even when SDK is initialized via Activity …
markushi Sep 26, 2024
7e57220
Adds breadcrumb origin field (#3727)
antonis Oct 1, 2024
b5b093e
Replace Calendar.getInstance with System.currentTimeMillis for breadc…
romtsn Oct 3, 2024
955c6ee
chore(readme): Add info about updated release channels (#3773)
kahest Oct 8, 2024
503f916
[QA] Lazily load SentryOptions members (#3749)
romtsn Oct 8, 2024
1607621
[QA] Fix potential ANRs due to default integrations (#3778)
markushi Oct 9, 2024
6548825
[SR] Support Jetpack Compose redaction (#3739)
romtsn Oct 9, 2024
0ab3bb3
[SR] Change terminology from redact/ignore to mask/unmask (#3741)
romtsn Oct 9, 2024
f74af4a
release: 7.15.0
getsentry-bot Oct 9, 2024
fe8ba2c
Merge branch 'release/7.15.0'
Oct 9, 2024
f79c9c1
Deprecate `enableTracing` in v7 (#3777)
adinauer Oct 10, 2024
2ab34eb
[QA] Replace SecureRandom with vendored Random (#3783)
romtsn Oct 14, 2024
a9c767e
[QA] Move NDK scope sync to background thread (#3754)
markushi Oct 14, 2024
6259a9f
[QA] Offload System.loadLibrary call to background thread (#3670)
markushi Oct 14, 2024
9c19319
Add meta option to attach ANR thread dumps (#3791)
markushi Oct 14, 2024
8fdee54
fix invalid profiles when the transaction name is empty (#3747)
stefanosiano Oct 15, 2024
274c295
chore: Add action to warn about potentially risky PR changes (#3726)
kahest Oct 15, 2024
654c2dc
Bump reactivecircus/android-emulator-runner from 2.32.0 to 2.33.0 (#3…
dependabot[bot] Oct 16, 2024
55ea3cc
[QA] Make logging faster on startup (#3793)
romtsn Oct 16, 2024
94071da
[QA] Hardcode integration names (#3794)
romtsn Oct 16, 2024
bd82483
Fix ANRv2 test flakyness (#3798)
stefanosiano Oct 16, 2024
ee6ab95
[QA] Make replay lazy and faster (#3799)
romtsn Oct 16, 2024
31f96ce
Bump codecov/codecov-action from 4.5.0 to 4.6.0 (#3768)
dependabot[bot] Oct 17, 2024
2a8b4fe
Bump github/codeql-action from 3.26.8 to 3.26.12 (#3787)
dependabot[bot] Oct 17, 2024
87bdc75
Bump gradle/actions (#3788)
dependabot[bot] Oct 17, 2024
a8c72dd
Fix ensure ndk libs are loaded before calling close (#3797)
markushi Oct 17, 2024
eb5d294
docs(okhttp): update documented default value to match actual (#3800)
marcin-kozinski Oct 17, 2024
143f91a
Bump JamesIves/github-pages-deploy-action from 4.6.4 to 4.6.8 (#3728)
dependabot[bot] Oct 17, 2024
9182d86
[QA] Cache parsed Dsn (#3796)
stefanosiano Oct 17, 2024
f14a299
release: 7.16.0-alpha.1
getsentry-bot Oct 17, 2024
a9b3fbc
Merge branch 'release/7.16.0-alpha.1'
Oct 17, 2024
0132cdd
[QA] Replace setOf with HashSet.add (#3801)
stefanosiano Oct 17, 2024
0cc9262
[QA] Load lazy fields on init in the background (#3803)
stefanosiano Oct 18, 2024
4988d5b
test(ui): Add critical tests run by Maestro (#3802)
krystofwoldrich Oct 21, 2024
28b6ac9
ci(build): Lower gradle workers to max 2 (#3814)
krystofwoldrich Oct 23, 2024
285450b
Prepare Changelog for 7.16.0 release (#3816)
markushi Oct 23, 2024
95f6443
release: 7.16.0
getsentry-bot Oct 23, 2024
7b3de1d
Merge branch 'release/7.16.0'
Oct 23, 2024
378d12c
Bump github/codeql-action from 3.26.12 to 3.26.13 (#3808)
dependabot[bot] Oct 25, 2024
771d13f
Bump androidx test libraries (#3743)
markushi Oct 28, 2024
92ab1d9
Add callout about `addIntegrationToSdkVersion` breaking change (#3829)
romtsn Oct 28, 2024
283c6cc
Accept manifest integer values when requiring floating values (#3823)
stefanosiano Oct 28, 2024
58da2a3
update benchmark devices on saucelabs (#3822)
stefanosiano Oct 28, 2024
4c5d0ff
Bump reactivecircus/android-emulator-runner from 2.32.0 to 2.33.0 (#3…
dependabot[bot] Oct 28, 2024
c362c98
Bump github/codeql-action from 3.26.13 to 3.27.0 (#3827)
dependabot[bot] Oct 28, 2024
5183da9
test(critical): Add API Level matrix (#3810)
krystofwoldrich Oct 28, 2024
28a11a7
Use `Random` through `ThreadLocal<Random>` (#3835)
adinauer Oct 31, 2024
2af8d1a
Fix: Allow MaxBreadcrumb 0 / Expose MaxBreadcrumb metadata. (#3836)
lucas-zimerman Nov 4, 2024
fd1151b
Bump gradle/actions (#3842)
dependabot[bot] Nov 4, 2024
fba10b8
Limit emulator size to 4096M (#3875)
markushi Nov 12, 2024
566da76
Fix standalone tomcat jndi issue (#3873)
lbloder Nov 12, 2024
4bd1aa3
release: 7.17.0
getsentry-bot Nov 12, 2024
cb0ecf1
Merge branch 'release/7.17.0'
Nov 12, 2024
17a41c1
Ensure android initialization process continues even if options confi…
romtsn Nov 13, 2024
091f84e
Add support for 16KB page sizes (Android 15) (#3620)
markushi Nov 14, 2024
a183163
Do not report parsing ANR error when there are no threads (#3888)
romtsn Nov 14, 2024
dab52e2
[QA] Avoid collecting normal frames (#3782)
stefanosiano Nov 14, 2024
05024c4
Extend CheckInUtils with check-in for a specific environment
maxhov Nov 14, 2024
4a98ac5
Merge branch '8.x.x' into checkinutils-environment
adinauer Nov 18, 2024
49624de
changelog
adinauer Nov 18, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Check app start spans time and foreground state (#3550)
* App start now takes AppStartMetrics.appLaunchedInForeground variable to add spans to the transaction
* App starts longer than 1 minute are dropped (same as Firebase)
* added Activity lifecycle registration to check start launch time and foreground status
* added AppStartMetrics.registerApplicationForegroundCheck in the SentryPerformanceProvider and SentryAndroid.init, other than AppStartMetrics.onApplicationCreate
* ActivityLifecycleIntegration now reads first activity creation on create instead of class instantiation
* AppStartMetrics stops app start profiler if no activity is being created
  • Loading branch information
stefanosiano authored Jul 17, 2024
commit 73237da99b1e3a7edfa5e72ed9afa9b09622462a
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Changelog

## Unreleased

### Fixes

- Check app start spans time and ignore background app starts ([#3550](https://github.com/getsentry/sentry-java/pull/3550))
- This should eliminate long-lasting App Start transactions

## 7.12.0

### Features
Expand Down
5 changes: 4 additions & 1 deletion sentry-android-core/api/sentry-android-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public class io/sentry/android/core/performance/ActivityLifecycleTimeSpan : java
public final fun getOnStart ()Lio/sentry/android/core/performance/TimeSpan;
}

public class io/sentry/android/core/performance/AppStartMetrics {
public class io/sentry/android/core/performance/AppStartMetrics : io/sentry/android/core/performance/ActivityLifecycleCallbacksAdapter {
public fun <init> ()V
public fun addActivityLifecycleTimeSpans (Lio/sentry/android/core/performance/ActivityLifecycleTimeSpan;)V
public fun clear ()V
Expand All @@ -443,10 +443,13 @@ public class io/sentry/android/core/performance/AppStartMetrics {
public static fun getInstance ()Lio/sentry/android/core/performance/AppStartMetrics;
public fun getSdkInitTimeSpan ()Lio/sentry/android/core/performance/TimeSpan;
public fun isAppLaunchedInForeground ()Z
public fun onActivityCreated (Landroid/app/Activity;Landroid/os/Bundle;)V
public static fun onApplicationCreate (Landroid/app/Application;)V
public static fun onApplicationPostCreate (Landroid/app/Application;)V
public static fun onContentProviderCreate (Landroid/content/ContentProvider;)V
public static fun onContentProviderPostCreate (Landroid/content/ContentProvider;)V
public fun registerApplicationForegroundCheck (Landroid/app/Application;)V
public fun setAppLaunchedInForeground (Z)V
public fun setAppStartProfiler (Lio/sentry/ITransactionProfiler;)V
public fun setAppStartSamplingDecision (Lio/sentry/TracesSamplingDecision;)V
public fun setAppStartType (Lio/sentry/android/core/performance/AppStartMetrics$AppStartType;)V
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.sentry.NoOpTransaction;
import io.sentry.SentryDate;
import io.sentry.SentryLevel;
import io.sentry.SentryNanotimeDate;
import io.sentry.SentryOptions;
import io.sentry.SpanStatus;
import io.sentry.TracesSamplingDecision;
Expand All @@ -37,6 +38,7 @@
import java.io.Closeable;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Date;
import java.util.Map;
import java.util.WeakHashMap;
import java.util.concurrent.Future;
Expand Down Expand Up @@ -75,7 +77,7 @@ public final class ActivityLifecycleIntegration
private @Nullable ISpan appStartSpan;
private final @NotNull WeakHashMap<Activity, ISpan> ttidSpanMap = new WeakHashMap<>();
private final @NotNull WeakHashMap<Activity, ISpan> ttfdSpanMap = new WeakHashMap<>();
private @NotNull SentryDate lastPausedTime = AndroidDateUtils.getCurrentSentryDateTime();
private @NotNull SentryDate lastPausedTime = new SentryNanotimeDate(new Date(0), 0);
private final @NotNull Handler mainHandler = new Handler(Looper.getMainLooper());
private @Nullable Future<?> ttfdAutoCloseFuture = null;

Expand Down Expand Up @@ -627,6 +629,14 @@ WeakHashMap<Activity, ISpan> getTtfdSpanMap() {
}

private void setColdStart(final @Nullable Bundle savedInstanceState) {
// The very first activity start timestamp cannot be set to the class instantiation time, as it
// may happen before an activity is started (service, broadcast receiver, etc). So we set it
// here.
if (hub != null && lastPausedTime.nanoTimestamp() == 0) {
lastPausedTime = hub.getOptions().getDateProvider().now();
} else if (lastPausedTime.nanoTimestamp() == 0) {
lastPausedTime = AndroidDateUtils.getCurrentSentryDateTime();
}
if (!firstActivityCreated) {
// if Activity has savedInstanceState then its a warm start
// https://developer.android.com/topic/performance/vitals/launch-time#warm
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.sentry.android.core;

import android.annotation.SuppressLint;
import android.app.Application;
import android.content.Context;
import android.os.Process;
import android.os.SystemClock;
Expand Down Expand Up @@ -141,6 +142,10 @@ public static synchronized void init(
appStartTimeSpan.setStartedAt(Process.getStartUptimeMillis());
}
}
if (context.getApplicationContext() instanceof Application) {
appStartMetrics.registerApplicationForegroundCheck(
(Application) context.getApplicationContext());
}
final @NotNull TimeSpan sdkInitTimeSpan = appStartMetrics.getSdkInitTimeSpan();
if (sdkInitTimeSpan.hasNotStarted()) {
sdkInitTimeSpan.setStartedAt(sdkInitMillis);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ private void onAppLaunched(

final @NotNull TimeSpan appStartTimespan = appStartMetrics.getAppStartTimeSpan();
appStartTimespan.setStartedAt(Process.getStartUptimeMillis());
appStartMetrics.registerApplicationForegroundCheck(app);

final AtomicBoolean firstDrawDone = new AtomicBoolean(false);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
package io.sentry.android.core.performance;

import android.app.Activity;
import android.app.Application;
import android.content.ContentProvider;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.SystemClock;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import io.sentry.ITransactionProfiler;
import io.sentry.SentryDate;
import io.sentry.SentryNanotimeDate;
import io.sentry.TracesSamplingDecision;
import io.sentry.android.core.ContextUtils;
import io.sentry.android.core.SentryAndroidOptions;
Expand All @@ -13,6 +21,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.TestOnly;
Expand All @@ -23,7 +32,7 @@
* transformed into SDK specific txn/span data structures.
*/
@ApiStatus.Internal
public class AppStartMetrics {
public class AppStartMetrics extends ActivityLifecycleCallbacksAdapter {

public enum AppStartType {
UNKNOWN,
Expand All @@ -45,6 +54,9 @@ public enum AppStartType {
private final @NotNull List<ActivityLifecycleTimeSpan> activityLifecycles;
private @Nullable ITransactionProfiler appStartProfiler = null;
private @Nullable TracesSamplingDecision appStartSamplingDecision = null;
private @Nullable SentryDate onCreateTime = null;
private boolean appLaunchTooLong = false;
private boolean isCallbackRegistered = false;

public static @NotNull AppStartMetrics getInstance() {

Expand All @@ -65,6 +77,7 @@ public AppStartMetrics() {
applicationOnCreate = new TimeSpan();
contentProviderOnCreates = new HashMap<>();
activityLifecycles = new ArrayList<>();
appLaunchedInForeground = ContextUtils.isForegroundImportance();
}

/**
Expand Down Expand Up @@ -102,6 +115,11 @@ public boolean isAppLaunchedInForeground() {
return appLaunchedInForeground;
}

@VisibleForTesting
public void setAppLaunchedInForeground(final boolean appLaunchedInForeground) {
this.appLaunchedInForeground = appLaunchedInForeground;
}

/**
* Provides all collected content provider onCreate time spans
*
Expand Down Expand Up @@ -137,12 +155,20 @@ public long getClassLoadedUptimeMs() {
// Only started when sdk version is >= N
final @NotNull TimeSpan appStartSpan = getAppStartTimeSpan();
if (appStartSpan.hasStarted()) {
return appStartSpan;
return validateAppStartSpan(appStartSpan);
}
}

// fallback: use sdk init time span, as it will always have a start time set
return getSdkInitTimeSpan();
return validateAppStartSpan(getSdkInitTimeSpan());
}

private @NotNull TimeSpan validateAppStartSpan(final @NotNull TimeSpan appStartSpan) {
// If the app launch took too long or it was launched in the background we return an empty span
if (appLaunchTooLong || !appLaunchedInForeground) {
return new TimeSpan();
}
return appStartSpan;
}

@TestOnly
Expand All @@ -158,6 +184,10 @@ public void clear() {
}
appStartProfiler = null;
appStartSamplingDecision = null;
appLaunchTooLong = false;
appLaunchedInForeground = false;
onCreateTime = null;
isCallbackRegistered = false;
}

public @Nullable ITransactionProfiler getAppStartProfiler() {
Expand Down Expand Up @@ -195,7 +225,55 @@ public static void onApplicationCreate(final @NotNull Application application) {
final @NotNull AppStartMetrics instance = getInstance();
if (instance.applicationOnCreate.hasNotStarted()) {
instance.applicationOnCreate.setStartedAt(now);
instance.appLaunchedInForeground = ContextUtils.isForegroundImportance();
instance.registerApplicationForegroundCheck(application);
}
}

/**
* Register a callback to check if an activity was started after the application was created
*
* @param application The application object to register the callback to
*/
public void registerApplicationForegroundCheck(final @NotNull Application application) {
if (isCallbackRegistered) {
return;
}
isCallbackRegistered = true;
appLaunchedInForeground = appLaunchedInForeground || ContextUtils.isForegroundImportance();
application.registerActivityLifecycleCallbacks(instance);
new Handler(Looper.getMainLooper())
.post(
() -> {
// if no activity has ever been created, app was launched in background
if (onCreateTime == null) {
appLaunchedInForeground = false;
}
application.unregisterActivityLifecycleCallbacks(instance);
// we stop the app start profiler, as it's useless and likely to timeout
if (appStartProfiler != null && appStartProfiler.isRunning()) {
appStartProfiler.close();
appStartProfiler = null;
}
});
}

@Override
public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle savedInstanceState) {
// An activity already called onCreate()
if (!appLaunchedInForeground || onCreateTime != null) {
return;
}
onCreateTime = new SentryNanotimeDate();

final long spanStartMillis = appStartSpan.getStartTimestampMs();
final long spanEndMillis =
appStartSpan.hasStopped()
? appStartSpan.getProjectedStopTimestampMs()
: System.currentTimeMillis();
final long durationMillis = spanEndMillis - spanStartMillis;
// If the app was launched more than 1 minute ago, it's likely wrong
if (durationMillis > TimeUnit.MINUTES.toMillis(1)) {
appLaunchTooLong = true;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import org.robolectric.shadow.api.Shadow
import org.robolectric.shadows.ShadowActivityManager
import java.util.Date
import java.util.concurrent.Future
import java.util.concurrent.TimeUnit
import kotlin.test.AfterTest
import kotlin.test.BeforeTest
import kotlin.test.Test
Expand Down Expand Up @@ -94,6 +95,7 @@ class ActivityLifecycleIntegrationTest {

whenever(hub.options).thenReturn(options)

AppStartMetrics.getInstance().isAppLaunchedInForeground = true
// We let the ActivityLifecycleIntegration create the proper transaction here
val optionCaptor = argumentCaptor<TransactionOptions>()
val contextCaptor = argumentCaptor<TransactionContext>()
Expand Down Expand Up @@ -709,15 +711,19 @@ class ActivityLifecycleIntegrationTest {
sut.register(fixture.hub, fixture.options)

val date = SentryNanotimeDate(Date(1), 0)
val date2 = SentryNanotimeDate(Date(2), 2)
setAppStartTime(date)

val activity = mock<Activity>()
// The activity onCreate date will be ignored
fixture.options.dateProvider = SentryDateProvider { date2 }
sut.onActivityCreated(activity, fixture.bundle)

verify(fixture.hub).startTransaction(
any(),
check<TransactionOptions> {
assertEquals(date.nanoTimestamp(), it.startTimestamp!!.nanoTimestamp())
assertNotEquals(date2.nanoTimestamp(), it.startTimestamp!!.nanoTimestamp())
assertFalse(it.isAppStartTransaction)
}
)
Expand Down Expand Up @@ -756,6 +762,30 @@ class ActivityLifecycleIntegrationTest {
)
}

@Test
fun `When firstActivityCreated is true and no app start time is set, default to onActivityCreated time`() {
val sut = fixture.getSut()
fixture.options.tracesSampleRate = 1.0
sut.register(fixture.hub, fixture.options)

// usually set by SentryPerformanceProvider
val date = SentryNanotimeDate(Date(1), 0)
val date2 = SentryNanotimeDate(Date(2), 2)

val activity = mock<Activity>()
// Activity onCreate date will be used
fixture.options.dateProvider = SentryDateProvider { date2 }
sut.onActivityCreated(activity, fixture.bundle)

verify(fixture.hub).startTransaction(
any(),
check<TransactionOptions> {
assertEquals(date2.nanoTimestamp(), it.startTimestamp!!.nanoTimestamp())
assertNotEquals(date.nanoTimestamp(), it.startTimestamp!!.nanoTimestamp())
}
)
}

@Test
fun `Create and finish app start span immediately in case SDK init is deferred`() {
val sut = fixture.getSut(importance = RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
Expand Down Expand Up @@ -940,6 +970,46 @@ class ActivityLifecycleIntegrationTest {
assertEquals(span.startDate.nanoTimestamp(), date.nanoTimestamp())
}

@Test
fun `When firstActivityCreated is true and app started more than 1 minute ago, app start spans are dropped`() {
val sut = fixture.getSut()
fixture.options.tracesSampleRate = 1.0
sut.register(fixture.hub, fixture.options)

val date = SentryNanotimeDate(Date(1), 0)
val duration = TimeUnit.MINUTES.toMillis(1) + 2
val durationNanos = TimeUnit.MILLISECONDS.toNanos(duration)
val stopDate = SentryNanotimeDate(Date(duration), durationNanos)
setAppStartTime(date, stopDate)

val activity = mock<Activity>()
sut.onActivityCreated(activity, null)

val appStartSpan = fixture.transaction.children.firstOrNull {
it.description == "Cold Start"
}
assertNull(appStartSpan)
}

@Test
fun `When firstActivityCreated is true and app started in background, app start spans are dropped`() {
val sut = fixture.getSut()
AppStartMetrics.getInstance().isAppLaunchedInForeground = false
fixture.options.tracesSampleRate = 1.0
sut.register(fixture.hub, fixture.options)

val date = SentryNanotimeDate(Date(1), 0)
setAppStartTime(date)

val activity = mock<Activity>()
sut.onActivityCreated(activity, null)

val appStartSpan = fixture.transaction.children.firstOrNull {
it.description == "Cold Start"
}
assertNull(appStartSpan)
}

@Test
fun `When firstActivityCreated is false, start transaction but not with given appStartTime`() {
val sut = fixture.getSut()
Expand Down Expand Up @@ -1412,18 +1482,22 @@ class ActivityLifecycleIntegrationTest {
shadowOf(Looper.getMainLooper()).idle()
}

private fun setAppStartTime(date: SentryDate = SentryNanotimeDate(Date(1), 0)) {
private fun setAppStartTime(date: SentryDate = SentryNanotimeDate(Date(1), 0), stopDate: SentryDate? = null) {
// set by SentryPerformanceProvider so forcing it here
val sdkAppStartTimeSpan = AppStartMetrics.getInstance().sdkInitTimeSpan
val appStartTimeSpan = AppStartMetrics.getInstance().appStartTimeSpan
val millis = DateUtils.nanosToMillis(date.nanoTimestamp().toDouble()).toLong()
val stopMillis = DateUtils.nanosToMillis(stopDate?.nanoTimestamp()?.toDouble() ?: 0.0).toLong()

sdkAppStartTimeSpan.setStartedAt(millis)
sdkAppStartTimeSpan.setStartUnixTimeMs(millis)
sdkAppStartTimeSpan.setStoppedAt(0)
sdkAppStartTimeSpan.setStoppedAt(stopMillis)

appStartTimeSpan.setStartedAt(millis)
appStartTimeSpan.setStartUnixTimeMs(millis)
appStartTimeSpan.setStoppedAt(0)
appStartTimeSpan.setStoppedAt(stopMillis)
if (stopDate != null) {
AppStartMetrics.getInstance().onActivityCreated(mock(), mock())
}
}
}
Loading