Skip to content

Commit 11f34f9

Browse files
authored
add instructions to setup Android continuous profiling (#11872)
* add instructions to setup Android continuous profiling
1 parent 934a188 commit 11f34f9

File tree

5 files changed

+131
-7
lines changed

5 files changed

+131
-7
lines changed

docs/platforms/android/configuration/options.mdx

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,13 +305,13 @@ Set this boolean to `false` to disable tracing for `OPTIONS` requests. This opti
305305

306306
<ConfigKey name="profiles-sample-rate">
307307

308-
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.
308+
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 transaction profiling. Don't set it if you want to use <PlatformLink to="/profiling/#continuous-profiling">Continuous Profiling</PlatformLink>.
309309

310310
</ConfigKey>
311311

312312
<ConfigKey name="profiles-sampler">
313313

314-
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.
314+
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 transaction profiling. Don't set it if you want to use <PlatformLink to="/profiling/#continuous-profiling">Continuous Profiling</PlatformLink>.
315315

316316
</ConfigKey>
317317

@@ -320,3 +320,35 @@ A function responsible for determining the percentage chance a given profile wil
320320
A boolean value that determines whether the app start process will be profiled. When true, the startup process, including ContentProviders, Application and first Activity creation, will be profiled. Note that <PlatformIdentifier name="profiles-sample-rate" /> or <PlatformIdentifier name="profiles-sampler" /> must be defined.
321321

322322
</ConfigKey>
323+
324+
## Continuous Profiling Options
325+
326+
<Alert>
327+
328+
This feature is experimental and may have bugs.
329+
330+
</Alert>
331+
332+
<ConfigKey name="profile-session-sample-rate">
333+
334+
A number between `0` and `1`, controlling the percentage chance the session will be profiled. `0` represents 0% while `1` represents 100%. The default is null (disabled). <PlatformIdentifier name="profiles-sampler" /> and <PlatformIdentifier name="profiles-sample-rate" /> must not be set to enable <PlatformLink to="/profiling/#continuous-profiling">Continuous Profiling</PlatformLink>.
335+
336+
</ConfigKey>
337+
338+
<ConfigKey name="profile-lifecycle">
339+
340+
Whether the profiling lifecycle is controlled manually or based on the trace lifecycle. Possible values are:
341+
342+
- `manual`: **default** Profiler must be started and stopped through `Sentry.startProfiler()` and `Sentry.stopProfiler()` APIs
343+
- `trace`: Profiler is started and stopped automatically whenever a sampled trace starts and finishes
344+
345+
</ConfigKey>
346+
347+
<ConfigKey name="start-profiler-on-app-start">
348+
349+
A boolean value that determines whether the app start process will be profiled. When true, the startup process, including ContentProviders, Application and first Activity creation, will be profiled. Note that <PlatformIdentifier name="profile-session-sample-rate" /> must be defined.
350+
351+
- If profileLifecycle is set to `manual`: profiling is started automatically on startup and stopProfiler must be called manually whenever the app startup is deemed to be completed
352+
- If profileLifecycle is set to `trace`: profiling is started automatically on startup, and will automatically be stopped when the root span that is associated with app startup ends
353+
354+
</ConfigKey>

docs/platforms/android/index.mdx

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,20 @@ The wizard will prompt you to log in to Sentry. It'll then automatically do the
7575
Configuration is done via the application `AndroidManifest.xml`. Here's an example config which should get you started:
7676

7777

78-
```xml {filename:AndroidManifest.xml} {"onboardingOptions": {"performance": "6-7", "profiling": "8-9", "session-replay": "10-12"}}
78+
```xml {filename:AndroidManifest.xml} {"onboardingOptions": {"performance": "6-7", "profiling": "8-13", "session-replay": "14-16"}}
7979
<application>
8080
<!-- Required: set your sentry.io project identifier (DSN) -->
8181
<meta-data android:name="io.sentry.dsn" android:value="___PUBLIC_DSN___" />
8282
<!-- Add data like request headers, user ip address and device name, see https://docs.sentry.io/platforms/android/data-management/data-collected/ for more info -->
8383
<meta-data android:name="io.sentry.send-default-pii" android:value="true" />
84-
<!-- enable the performance API by setting a sample-rate, adjust in production env -->
84+
<!-- Enable the performance API by setting a sample-rate, adjust in production env -->
8585
<meta-data android:name="io.sentry.traces.sample-rate" android:value="1.0" />
86-
<!-- enable profiling when starting transactions, adjust in production env -->
87-
<meta-data android:name="io.sentry.traces.profiling.sample-rate" android:value="1.0" />
86+
<!-- Enable profiling, adjust in production env. See https://docs.sentry.io/platforms/android/profiling for more info -->
87+
<meta-data android:name="io.sentry.traces.profiling.session-sample-rate" android:value="1.0" />
88+
<!-- Set profiling lifecycle, can be `manual` or `trace` -->
89+
<meta-data android:name="io.sentry.traces.profiling.lifecycle" android:value="manual" />
90+
<!-- Enable profiling on app start -->
91+
<meta-data android:name="io.sentry.traces.profiling.start-on-app-start" android:value="true" />
8892
<!-- record session replays for 100% of errors and 10% of sessions -->
8993
<meta-data android:name="io.sentry.session-replay.on-error-sample-rate" android:value="1.0" />
9094
<meta-data android:name="io.sentry.session-replay.session-sample-rate" android:value="0.1" />
@@ -104,11 +108,15 @@ import io.sentry.Sentry;
104108
public class MyActivity extends AppCompatActivity {
105109
protected void onCreate(Bundle savedInstanceState) {
106110
super.onCreate(savedInstanceState);
111+
// Any code running after this line will be
112+
//profiled until Sentry.stopProfiler() is called.
113+
Sentry.startProfiler();
107114
try {
108115
throw new Exception("This is a test.");
109116
} catch (Exception e) {
110117
Sentry.captureException(e);
111118
}
119+
Sentry.stopProfiler();
112120
}
113121
}
114122
```
@@ -121,11 +129,15 @@ import io.sentry.Sentry
121129
class MyActivity : AppCompatActivity() {
122130
override fun onCreate(savedInstanceState: Bundle?) {
123131
super.onCreate(savedInstanceState)
132+
// Any code running after this line will be
133+
//profiled until Sentry.stopProfiler() is called.
134+
Sentry.startProfiler()
124135
try {
125136
throw Exception("This is a test.")
126137
} catch (e: Exception) {
127138
Sentry.captureException(e)
128139
}
140+
Sentry.stopProfiler()
129141
}
130142
}
131143
```

docs/platforms/android/profiling/index.mdx

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,82 @@ The SDK won't run app start profiling the very first time the app runs, as the S
6868
The SDK will set the `isForNextAppStart` flag in `TransactionContext` if app start profiling is enabled.
6969

7070
</Alert>
71+
72+
## Continuous Profiling
73+
74+
<Alert>
75+
76+
This feature is experimental and may have bugs.
77+
78+
</Alert>
79+
80+
_(New in version 8.5.0)_
81+
82+
The current profiling implementation stops the profiler automatically after 30 seconds (unless you manually stop it earlier). Naturally, this limitation makes it difficult to get full coverage of your app's execution. We now offer an experimental continuous mode, where profiling data is periodically uploaded while running, with no limit to how long the profiler may run.
83+
84+
Previously, profiles only ran in tandem with performance transactions that were started either automatically or manually with `Sentry.startTransaction`. Now, you can start and stop the profiler directly with `Sentry.startProfiler` and `Sentry.stopProfiler`. You can also start a profile at app launch by setting `SentryOptions.startProfilerOnAppStart = true` in your call to `SentryAndroid.init`.
85+
86+
Continuous profiling requires only the `SentryOptions.profileSessionSampleRate` option to be set when you start the SDK to opt in. If you had previously set `SentryOptions.profilesSampleRate` or `SentryOptions.profilesSampler` to use transaction-based profiling, then remove those lines of code from your configuration.
87+
88+
There are two ways to start the profiler: manually, calling `Sentry.startProfiler` and `Sentry.stopProfiler` to start and stop the profiler, or automatically whenever a sampled trace starts and finishes. The latter behaviour is very similar to the current profiling implementation. You can control this behaviour setting `SentryOptions.profileLifecycle` to `manual` or `trace` in your call to `SentryAndroid.init`.
89+
90+
<Alert title="Note">
91+
92+
Continuous profiling has implications for your org's billing structure. This feature is only available for subscription plans that enrolled after June 5, 2024.
93+
94+
</Alert>
95+
96+
```xml {filename:AndroidManifest.xml}
97+
<application>
98+
<meta-data android:name="io.sentry.traces.profiling.session-sample-rate" android:value="1.0" />
99+
<meta-data android:name="io.sentry.traces.profiling.lifecycle" android:value="manual" />
100+
<meta-data android:name="io.sentry.traces.profiling.start-on-app-start" android:value="true" />
101+
</application>
102+
```
103+
104+
Or, if you are manually instrumenting Sentry:
105+
106+
107+
```java {tabTitle:Java}
108+
import io.sentry.android.core.SentryAndroid;
109+
110+
SentryAndroid.init(this, options -> {
111+
options.setDsn("___PUBLIC_DSN___");
112+
// Currently under experimental options:
113+
options.getExperimental().setProfileSessionSampleRate(1.0);
114+
// In manual mode, you need to start and stop the profiler manually using Sentry.startProfiler and Sentry.stopProfiler
115+
// In trace mode, the profiler will start and stop automatically whenever a sampled trace starts and finishes
116+
options.getExperimental().setProfileLifecycle(ProfileLifecycle.MANUAL);
117+
// Start profiling automatically as early as possible, to capture app startup
118+
// If profileLifecycle is set to `manual`: stopProfiler must be called manually when the app startup is completed
119+
// If profileLifecycle is set to `trace`: profiling will automatically be stopped when the app start root span ends
120+
options.getExperimental().setStartProfilerOnAppStart(true);
121+
});
122+
// Start profiling
123+
Sentry.startProfiler();
124+
125+
// After all profiling is done, stop the profiler. Profiles can last indefinitely if not stopped.
126+
Sentry.stopProfiler();
127+
```
128+
129+
```kotlin {tabTitle:Kotlin}
130+
import io.sentry.android.core.SentryAndroid
131+
132+
SentryAndroid.init(this) { options ->
133+
options.dsn = "___PUBLIC_DSN___"
134+
// Currently under experimental options:
135+
options.experimental.profileSessionSampleRate = 1.0
136+
// In manual mode, you need to start and stop the profiler manually using Sentry.startProfiler and Sentry.stopProfiler
137+
// In trace mode, the profiler will start and stop automatically whenever a sampled trace starts and finishes
138+
options.experimental.profileLifecycle = ProfileLifecycle.MANUAL
139+
// Start profiling automatically as early as possible, to capture app startup
140+
// If profileLifecycle is set to `manual`: stopProfiler must be called manually when the app startup is completed
141+
// If profileLifecycle is set to `trace`: profiling will automatically be stopped when the app start root span ends
142+
options.experimental.startProfilerOnAppStart = true
143+
}
144+
// Start profiling
145+
Sentry.startProfiler()
146+
147+
// After all profiling is done, stop the profiler. Profiles can last indefinitely if not stopped.
148+
Sentry.stopProfiler()
149+
```

docs/platforms/apple/common/profiling/index.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,6 @@ Previously, profiles only ran in tandem with performance transactions that were
113113
114114
Continuous profiling mode is enabled by default, requiring no changes to `SentryOptions` when you start the SDK to opt in. If you had previously set `SentryOptions.profilesSampleRate` or `SentryOptions.profilesSampler` to use transaction-based profiling, then remove those lines of code from your configuration.
115115
116-
These new APIs do not offer any sampling functionality—every call to start the profiler will start it, and the same goes for launch profiles if you've configured that. If you are interested in reducing the amount of profiles that run, you must take care to do it at the callsites.
116+
These new APIs do not offer any sampling functionality—every call to start the profiler will start it, and the same goes for launch profiles if you've configured that. If you are interested in reducing the amount of profiles that run, you must take care to do it at the call sites.
117117
118118
Continuous profiling has implications for your org's billing structure. This feature is only available for subscription plans that enrolled after June 5, 2024.

docs/product/explore/profiling/getting-started.mdx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ Profiling depends on Sentry's performance monitoring product being enabled befor
3535
<Include name="feature-stage-beta.mdx" />
3636

3737
- Mobile
38+
- [Android](/platforms/android/profiling/#continuous-profiling)
3839
- [iOS and macOS](/platforms/apple/guides/ios/profiling/#continuous-profiling)
3940
- Standalone and server apps
4041
- [Node.js](/platforms/javascript/guides/node/profiling/#enable-continuous-profiling)

0 commit comments

Comments
 (0)