Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create onCreate and onStart spans for all Activities #4025

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- Warm starts cleanup ([#3954](https://github.com/getsentry/sentry-java/pull/3954))

## Behavioural Changes

- Create onCreate and onStart spans for all Activities ([#4025](https://github.com/getsentry/sentry-java/pull/4025))

### Dependencies

- Bump Native SDK from v0.7.16 to v0.7.17 ([#4003](https://github.com/getsentry/sentry-java/pull/4003))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ public void onActivityPostCreated(
final @NotNull Activity activity, final @Nullable Bundle savedInstanceState) {
final ActivityLifecycleSpanHelper helper = activitySpanHelpers.get(activity);
if (helper != null) {
helper.createAndStopOnCreateSpan(appStartSpan);
helper.createAndStopOnCreateSpan(
appStartSpan != null ? appStartSpan : activitiesWithOngoingTransactions.get(activity));
}
}

Expand Down Expand Up @@ -453,7 +454,8 @@ public synchronized void onActivityStarted(final @NotNull Activity activity) {
public void onActivityPostStarted(final @NotNull Activity activity) {
final ActivityLifecycleSpanHelper helper = activitySpanHelpers.get(activity);
if (helper != null) {
helper.createAndStopOnStartSpan(appStartSpan);
helper.createAndStopOnStartSpan(
appStartSpan != null ? appStartSpan : activitiesWithOngoingTransactions.get(activity));
// Needed to handle hybrid SDKs
helper.saveSpanToAppStartMetrics();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,18 @@ public void setOnStartStartTimestamp(final @NotNull SentryDate onStartStartTimes
this.onStartStartTimestamp = onStartStartTimestamp;
}

public void createAndStopOnCreateSpan(final @Nullable ISpan appStartSpan) {
if (onCreateStartTimestamp != null && appStartSpan != null) {
public void createAndStopOnCreateSpan(final @Nullable ISpan parentSpan) {
if (onCreateStartTimestamp != null && parentSpan != null) {
onCreateSpan =
createLifecycleSpan(appStartSpan, activityName + ".onCreate", onCreateStartTimestamp);
createLifecycleSpan(parentSpan, activityName + ".onCreate", onCreateStartTimestamp);
onCreateSpan.finish();
}
}

public void createAndStopOnStartSpan(final @Nullable ISpan appStartSpan) {
if (onStartStartTimestamp != null && appStartSpan != null) {
public void createAndStopOnStartSpan(final @Nullable ISpan parentSpan) {
if (onStartStartTimestamp != null && parentSpan != null) {
onStartSpan =
createLifecycleSpan(appStartSpan, activityName + ".onStart", onStartStartTimestamp);
createLifecycleSpan(parentSpan, activityName + ".onStart", onStartStartTimestamp);
onStartSpan.finish();
}
}
Expand Down Expand Up @@ -106,19 +106,18 @@ public void saveSpanToAppStartMetrics() {
}

private @NotNull ISpan createLifecycleSpan(
final @NotNull ISpan appStartSpan,
final @NotNull ISpan parentSpan,
final @NotNull String description,
final @NotNull SentryDate startTimestamp) {
final @NotNull ISpan span =
appStartSpan.startChild(
parentSpan.startChild(
APP_METRICS_ACTIVITIES_OP, description, startTimestamp, Instrumenter.SENTRY);
setDefaultStartSpanData(span);
return span;
}

public void clear() {
// in case the appStartSpan isn't completed yet, we finish it as cancelled to avoid
// memory leak
// in case the parentSpan isn't completed yet, we finish it as cancelled to avoid memory leak
if (onCreateSpan != null && !onCreateSpan.isFinished()) {
onCreateSpan.finish(SpanStatus.CANCELLED);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1484,6 +1484,43 @@ class ActivityLifecycleIntegrationTest {
assertFalse(appStartMetrics.activityLifecycleTimeSpans.isEmpty())
}

@Test
fun `Creates activity lifecycle spans even when no app start span is available`() {
val sut = fixture.getSut()
fixture.options.tracesSampleRate = 1.0
val startDate = SentryNanotimeDate(Date(2), 0)
val appStartMetrics = AppStartMetrics.getInstance()
val activity = mock<Activity>()
fixture.options.dateProvider = SentryDateProvider { startDate }
// Don't set app start time, so there's no app start span
// setAppStartTime(appStartDate)

sut.register(fixture.hub, fixture.options)
assertTrue(sut.activitySpanHelpers.isEmpty())

sut.onActivityPreCreated(activity, null)

assertFalse(sut.activitySpanHelpers.isEmpty())
val helper = sut.activitySpanHelpers.values.first()
assertNotNull(helper.onCreateStartTimestamp)

sut.onActivityCreated(activity, null)
assertNull(sut.appStartSpan)

sut.onActivityPostCreated(activity, null)
assertTrue(helper.onCreateSpan!!.isFinished)

sut.onActivityPreStarted(activity)
assertNotNull(helper.onStartStartTimestamp)

sut.onActivityStarted(activity)
assertTrue(appStartMetrics.activityLifecycleTimeSpans.isEmpty())

sut.onActivityPostStarted(activity)
assertTrue(helper.onStartSpan!!.isFinished)
assertFalse(appStartMetrics.activityLifecycleTimeSpans.isEmpty())
}

@Test
fun `Save activity lifecycle spans in AppStartMetrics onPostSarted`() {
val sut = fixture.getSut()
Expand Down
Loading