Skip to content

Commit 153db9a

Browse files
authored
Add Android App Start Profiling (#8899)
* added enable-app-start manifest option * added small description of App Start Profiling * mention TransactionContext.isForNextAppStart in sampler functions * added profiling options in Basic Options
1 parent 0b88f19 commit 153db9a

File tree

4 files changed

+52
-0
lines changed

4 files changed

+52
-0
lines changed

src/platform-includes/performance/traces-sampler-as-sampler/android.mdx

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ SentryAndroid.init(this, options -> {
1010
options.setTracesSampleRate(1.0);
1111
// OR: Determine traces sample rate based on the sampling context
1212
options.setTracesSampler(context -> {
13+
if (context.getTransactionContext().isForNextAppStart()) {
14+
// These refer to app start (if app start profiling is enabled), and are important - take a big sample
15+
return 0.7;
16+
}
1317
CustomSamplingContext ctx = context.getCustomSamplingContext();
1418
if (ctx != null) {
1519
if ("LoginActivity".equals(ctx.get("ActivityName"))) {
@@ -38,6 +42,10 @@ import io.sentry.SentryOptions.TracesSamplerCallback
3842

3943
SentryAndroid.init(this) { options ->
4044
options.tracesSampler = TracesSamplerCallback { context ->
45+
if (context.transactionContext.isForNextAppStart) {
46+
// These refer to app start (if app start profiling is enabled), and are important - take a big sample
47+
return@TracesSamplerCallback 0.7
48+
}
4149
val ctx = context.customSamplingContext
4250
if (ctx != null) {
4351
when (ctx.get("ActivityName")) {

src/platforms/android/configuration/options.mdx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,3 +307,29 @@ If <PlatformIdentifier name="trace-propagation-targets" /> is not provided, trac
307307
Set this boolean to `false` to disable tracing for `OPTIONS` requests. This options default value will likely be changed in the next major version, meaning you will have to set it to `true` if you want to keep tracing `OPTIONS` requests.
308308

309309
</ConfigKey>
310+
311+
## Profiling Options
312+
313+
<ConfigKey name="enable-tracing">
314+
315+
A boolean value, if true, transactions and trace data will be generated and captured. This will set the `traces-sample-rate` to the recommended default of 1.0 if `traces-sample-rate` is not defined. Note that `traces-sample-rate` and `traces-sampler` take precedence over this option.
316+
317+
</ConfigKey>
318+
319+
<ConfigKey name="profiles-sample-rate">
320+
321+
A number between 0 and 1, controlling the percentage chance a given profile will be sent to Sentry. (0 represents 0% while 1 represents 100%.) Applies only to sampled transactions created in the app. Either this or <PlatformIdentifier name="profiles-sampler" /> must be defined to enable profiling.
322+
323+
</ConfigKey>
324+
325+
<ConfigKey name="profiles-sampler">
326+
327+
A function responsible for determining the percentage chance a given profile will be sent to Sentry. It will automatically be passed information about the transaction and the context in which it's being created, and must return a number between `0` (0% chance of being sent) and `1` (100% chance of being sent). Can also be used for filtering profiles, by returning 0 for those that are unwanted. Either this or <PlatformIdentifier name="profiles-sample-rate" /> must be defined to enable profiling.
328+
329+
</ConfigKey>
330+
331+
<ConfigKey name="enable-app-start-profiling">
332+
333+
A boolean value, if true, the startup process, including ContentProviders, Application and first Activity creation, will be profiled. Note that `profiles-sample-rate` or `profiles-sampler` must be defined.
334+
335+
</ConfigKey>

src/platforms/android/manual-setup/index.mdx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ Configuration is done via the application `AndroidManifest.xml`. Here's an examp
4848
<meta-data android:name="io.sentry.traces.sample-rate" android:value="1.0" />
4949
<!-- enable profiling when starting transactions, adjust in production env -->
5050
<meta-data android:name="io.sentry.traces.profiling.sample-rate" android:value="1.0" />
51+
<!-- enable app start profiling -->
52+
<meta-data android:name="io.sentry.traces.profiling.enable-app-start" android:value="true" />
5153
</application>
5254
```
5355

src/platforms/android/profiling/index.mdx

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ By default, some transactions will be created automatically for common operation
3030
<Note>
3131

3232
Android profiling is available starting in SDK version `6.16.0`.
33+
App start profiling is available starting in SDK version `7.3.0`.
3334

3435
</Note>
3536

@@ -42,6 +43,7 @@ In `AndroidManifest.xml`:
4243
<meta-data android:name="io.sentry.dsn" android:value="___PUBLIC_DSN___" />
4344
<meta-data android:name="io.sentry.traces.sample-rate" android:value="1.0" />
4445
<meta-data android:name="io.sentry.traces.profiling.sample-rate" android:value="1.0" />
46+
<meta-data android:name="io.sentry.traces.profiling.enable-app-start" android:value="true" />
4547
</application>
4648
```
4749

@@ -50,3 +52,17 @@ In `AndroidManifest.xml`:
5052
The `io.sentry.traces.profiling.sample-rate` setting is _relative_ to the `io.sentry.traces.sample-rate` setting.
5153

5254
</Note>
55+
56+
## App Start Profiling
57+
58+
When app start profiling is enabled, the whole app start process is profiled.
59+
This includes all methods from any `ContentProvider`, the `Application` class, and the first Activity, until the <PlatformLink to="/performance/instrumentation/automatic-instrumentation/">first automatic Activity transaction</PlatformLink> is finished.
60+
App start profiling can be enabled with the manifest option `io.sentry.traces.profiling.enable-app-start` as shown above, and it will respect the `io.sentry.traces.sample-rate` and the `io.sentry.traces.profiling.sample-rate`.
61+
If you prefer to use a <PlatformLink to="/configuration/sampling/#setting-a-sampling-function">sampling function</PlatformLink>, the SDK sets the `isForNextAppStart` field on the `TransactionContext` to specify it will be used for the next app start profiling.
62+
63+
<Note>
64+
65+
The SDK won't run app start profiling the very first time the app runs, as the SDK won't have read the options by the time the profile should run.
66+
The SDK will set the `isForNextAppStart` flag in `TransactionContext` if app start profiling is enabled.
67+
68+
</Note>

0 commit comments

Comments
 (0)