-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
docs(android): add Compose effect guidance #19308
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -356,3 +356,39 @@ SentryAndroid.init(this) { options -> | |||||
| }) | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| ## Custom Telemetry in Compose | ||||||
|
|
||||||
| Composable functions can run many times during recomposition. Don't emit custom Sentry spans, messages, | ||||||
| breadcrumbs, or other telemetry directly from a composable body, as it can duplicate telemetry or attach it | ||||||
| to the wrong UI lifecycle moment. | ||||||
|
|
||||||
| Prefer emitting telemetry from: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| - `LaunchedEffect`, `DisposableEffect`, or `SideEffect` (more info in [Google's developer docs](https://developer.android.com/develop/ui/compose/side-effects)) | ||||||
| - Event callbacks such as `onClick` when the telemetry corresponds to a user action | ||||||
| - APIs that run after composition, such as draw-time or layout-time lambdas, when that timing is what you want to measure | ||||||
|
|
||||||
| For example, avoid capturing a message directly from the body: | ||||||
|
|
||||||
| ```kotlin | ||||||
| @Composable | ||||||
| fun LoginScreen() { | ||||||
| Sentry.captureMessage("Login screen shown") | ||||||
| // ... | ||||||
| } | ||||||
| ``` | ||||||
|
|
||||||
| Instead, capture state in the body as needed but emit it from an Effect API: | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
|
||||||
| ```kotlin | ||||||
| @Composable | ||||||
| fun LoginScreen() { | ||||||
| val firstComposedAt = remember { Instant.now() } | ||||||
|
|
||||||
| LaunchedEffect(Unit) { | ||||||
| Sentry.captureMessage("Login screen shown: $firstComposedAt") | ||||||
| } | ||||||
| // ... | ||||||
| } | ||||||
| ``` | ||||||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -14,6 +14,14 @@ To capture transactions and spans customized to your organization's needs, you m | |||||
|
|
||||||
| <PlatformContent includePath="performance/enable-manual-instrumentation" /> | ||||||
|
|
||||||
| <Alert level="info" title="Using custom instrumentation in Jetpack Compose?"> | ||||||
|
|
||||||
| If you emit custom Sentry spans or other telemetry from Jetpack Compose code, use Compose Effect APIs such as | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| `LaunchedEffect`, `DisposableEffect`, or `SideEffect` instead of emitting from a composable body. Composable bodies | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| can run repeatedly during recomposition. See <PlatformLink to="/integrations/jetpack-compose/#custom-telemetry-in-compose">Jetpack Compose</PlatformLink>. | ||||||
|
|
||||||
| </Alert> | ||||||
|
|
||||||
| <PlatformContent includePath="performance/add-spans-example" /> | ||||||
|
|
||||||
| <PlatformContent includePath="performance/create-transaction-bound-to-scope" /> | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Technically both are correct, just trying to keep the vocabulary simple to accommodate users with limited English.