feat(android): Add standalone app start tracing#5342
feat(android): Add standalone app start tracing#5342
Conversation
Introduce experimental `enableStandaloneAppStartTracing` option that creates a separate app start transaction instead of attaching app start as a child span of the first activity transaction. This is the happy path only (foreground importance, activity launch, first frame drawn as end time). The standalone transaction shares the same trace ID as the activity transaction but is not bound to the scope. App start measurements and child spans (process init, content providers, application.onCreate) are attached to the standalone transaction instead of the activity transaction. Includes foreground importance check branching to prepare for the non-activity launch path (next PR). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
When the app starts without launching an activity (service, broadcast receiver, content provider), create a standalone app start transaction with the end time determined by priority: 1. onApplicationPostCreate (Gradle plugin bytecode instrumentation) 2. ApplicationStartInfo timestamps (API 35+) 3. firstIdle - main thread idle handler (pre-API 35 fallback) The non-activity app start transaction stores its trace ID so that if an activity is later launched, the activity transaction reuses the same trace ID to keep both in the same trace. Adds OnNoActivityStartedListener callback from AppStartMetrics to ActivityLifecycleIntegration, triggered by checkCreateTimeOnMain() when no activity was created after Application.onCreate(). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…entation When an app is launched via broadcast receiver, service, or content provider (no activity), detect this via Handler.post() and create a standalone app start transaction. Resolves app start end time with priority: Gradle plugin > ApplicationStartInfo (API 35+) > process init time. Also attaches child spans (process init, content providers, Application.onCreate) to standalone transactions. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Extract the "try appStartSpan, fall back to sdkInitTimeSpan" logic used for standalone (non-activity) app start transactions into a new AppStartMetrics.getAppStartTimeSpanDirect() helper, removing the duplicated inline fallback in ActivityLifecycleIntegration and the private helper in PerformanceAndroidEventProcessor. Also cache the API 35+ ApplicationStartInfo on registerLifecycleCallbacks so onAppStartSpansSent no longer re-queries ActivityManager, and simplify the non-activity detection path to always use the main-thread IdleHandler. Regenerates the sentry-android-core API to include method additions missed in prior commits on this branch (standalone-app-start options, trace id accessors, OnNoActivityStartedListener). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Wires up the TestBroadcastReceiver added earlier so the sample app can trigger a non-activity cold start via `adb shell am broadcast`. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
…tart-tracing # Conflicts: # sentry-android-core/src/main/java/io/sentry/android/core/performance/AppStartMetrics.java
…icate emission Two pre-merge fixes for the standalone app-start tracing path introduced on this branch (issue #5046): - AppStartMetrics.checkCreateTimeOnMain() now defaults appStartType to COLD when UNKNOWN with no active activities. On API < 35 (where ApplicationStartInfo is unavailable) non-activity cold starts were stuck at UNKNOWN, which both misclassified the standalone transaction as App Start Warm and caused PerformanceAndroidEventProcessor.attachAppStartSpans to early-return (dropping process.load / application.load / contentprovider.load phase spans). - ActivityLifecycleIntegration.onActivityPreCreated() now skips emitting a second standalone App Start transaction when the non-activity path has already reported the process's app start (detected via the stashed appStartTraceId). Previously a broadcast followed by an activity launch produced two standalone transactions (a spurious App Start Warm in addition to the broadcast's App Start Cold), violating one-per-process semantics. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
|
📲 Install BuildsAndroid
|
Performance metrics 🚀
|
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 0220a5c | 332.62 ms | 390.85 ms | 58.24 ms |
| 2195398 | 344.65 ms | 403.96 ms | 59.30 ms |
| ab8a72d | 316.24 ms | 356.38 ms | 40.14 ms |
| d364ace | 384.53 ms | 453.51 ms | 68.98 ms |
| 85d7417 | 347.21 ms | 394.35 ms | 47.15 ms |
| 6727e14 | 337.22 ms | 373.94 ms | 36.71 ms |
| d15471f | 310.66 ms | 368.19 ms | 57.53 ms |
| fc5ccaf | 276.52 ms | 370.46 ms | 93.93 ms |
| b8bd880 | 314.56 ms | 336.50 ms | 21.94 ms |
| ce0a49e | 532.00 ms | 609.96 ms | 77.96 ms |
App size
| Revision | Plain | With Sentry | Diff |
|---|---|---|---|
| 0220a5c | 0 B | 0 B | 0 B |
| 2195398 | 0 B | 0 B | 0 B |
| ab8a72d | 1.58 MiB | 2.12 MiB | 551.55 KiB |
| d364ace | 1.58 MiB | 2.11 MiB | 539.75 KiB |
| 85d7417 | 1.58 MiB | 2.10 MiB | 533.44 KiB |
| 6727e14 | 1.58 MiB | 2.28 MiB | 718.64 KiB |
| d15471f | 1.58 MiB | 2.13 MiB | 559.54 KiB |
| fc5ccaf | 1.58 MiB | 2.13 MiB | 557.54 KiB |
| b8bd880 | 1.58 MiB | 2.29 MiB | 722.92 KiB |
| ce0a49e | 1.58 MiB | 2.10 MiB | 532.94 KiB |
Rename the standalone app-start transaction to a single App Start name so cold and warm starts group consistently while preserving the app.start op. Co-authored-by: Cursor <cursoragent@cursor.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
🚨 Detected changes in high risk code 🚨High-risk code has higher potential to break the SDK and may be hard to test. To prevent severe bugs, apply the rollout process for releasing such changes and be extra careful when changing and reviewing these files:
|
Co-authored-by: Cursor <cursoragent@cursor.com>
🚨 Detected changes in high risk code 🚨High-risk code has higher potential to break the SDK and may be hard to test. To prevent severe bugs, apply the rollout process for releasing such changes and be extra careful when changing and reviewing these files:
|
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 2 potential issues.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 87ff0d6. Configure here.
| timestamps.get(ApplicationStartInfo.START_TIMESTAMP_APPLICATION_ONCREATE); | ||
| if (onCreateNanos != null && onCreateNanos > 0) { | ||
| final long onCreateUptimeMs = TimeUnit.NANOSECONDS.toMillis(onCreateNanos); | ||
| stopNonActivityAppStartAt(onCreateUptimeMs); |
There was a problem hiding this comment.
ApplicationStartInfo timestamps may use wrong clock base
Medium Severity
ApplicationStartInfo.getStartupTimestamps() returns nanoseconds based on SystemClock.elapsedRealtimeNanos(), but the converted value is passed to setStoppedAt() which expects SystemClock.uptimeMillis(). These two clocks diverge by the total accumulated deep sleep time since boot. On a device awake for hours with prior deep sleep, the mismatch can produce wildly inflated app start durations (off by hours). The variable name onCreateUptimeMs masks this, and the test uses mock values so it cannot catch the discrepancy.
Additional Locations (1)
Reviewed by Cursor Bugbot for commit 87ff0d6. Configure here.


📜 Description
Adds standalone Android app-start transaction support behind the opt-in
enableStandaloneAppStartTracingoption and theio.sentry.standalone-app-start-tracing.enablemanifest key.This PR also covers the non-activity startup path so cold starts triggered by broadcasts or foreground services can emit an
App Start Cold/Warmtransaction without waiting for an activity. The legacy flag-off behavior is preserved: activity app-start data remains nested under theui.loadtransaction.Additionally this PR adds two new attributes:
app.vitals.start.reasonapp.vitals.start.screen💡 Motivation and Context
Resolves: #5046
Standalone app-start traces make app-start performance visible independently from activity load transactions, and they let Android report app starts where no activity is launched. The implementation keeps the default behavior unchanged and adds tests for the new option, manifest metadata, transaction context trace reuse, app-start metrics, event processing, and activity lifecycle behavior.
App-start creation paths
This PR preserves the existing flag-off behavior and adds a separate standalone app-start branch when
enableStandaloneAppStartTracingis enabled.ActivityLifecycleIntegrationstarts a standaloneApp Starttransaction and the normal activityui.loadtransaction on the same trace.app.startfor app-start phases, andui.loadfor TTID/TTFD. Theui.loadtransaction does not contain anapp.start.cold/warmchild.ui.loadtransaction with nestedapp.start.cold/warm, plus app-start phase spans under that child.Application.onCreatestart/end throughAppStartMetrics.onApplicationCreate/PostCreate. When no activity starts,ActivityLifecycleIntegrationemits from that completed app-start span.App Starttransaction and noui.load; includesprocess.loadandapplication.loadphase spans.AppStartMetricsreadsApplicationStartInfofromActivityManagerto determine start type and use the platformAPPLICATION_ONCREATEtimestamp as the app-start end time.App Starttransaction and noui.load; includesprocess.load, but notapplication.loadbecause the SDK does not have plugin instrumentation for that phase.ApplicationStartInfois unavailable, soAppStartMetricsdefaults the no-activity process start to cold and uses the class-loaded timestamp fallback for the app-start end time.App Starttransaction and noui.load; includesprocess.loadonly and remains classified as cold.App Starttransaction stores its trace ID for a later activity.ui.loadreuses the stored trace ID and does not create a second standalone app-start transaction.💚 How did you test it?
./gradlew spotlessApply apiDumpscripts/test-standalone-app-start.sh, summarized below.End-to-end Sentry verification
App Startemits alongside siblingMainActivity ui.load; shared trace ID;ui.loadhas noapp.start.*child.app.start(app_start_cold=1156ms) +activity.loadx2 +process.load+application.load; siblingui.load(time_to_initial_display=1156ms)52154e91...ui.loadwithapp.start.coldnested inside.ui.load(time_to_initial_display=1536ms) -> nestedapp.start.cold(app_start_cold=1537ms) ->process.load+activity.loadx2d3f0e049...app.start(app_start_cold=1408ms) +process.load+application.load; noui.load81ae80f6...ApplicationStartInfo.app.start(app_start_cold=525ms) +process.loadonly; noui.load0dcd120e...CLASS_LOADED_UPTIME_MS.app.start(app_start_cold=614ms) +process.loadonly; correctly classified Coldac8a9189...app.start(app_start_cold=4605ms) +process.load+application.load; noui.load905262ce...app.start(app_start_cold=1751ms) + later siblingui.load(time_to_initial_display=4563ms); no duplicate standalone5da4a88f...📝 Checklist
sendDefaultPIIis enabled.🔮 Next steps