Skip to content
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
36 changes: 36 additions & 0 deletions docs/platforms/android/integrations/jetpack-compose/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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:

- `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:

```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
Expand Up @@ -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
`LaunchedEffect`, `DisposableEffect`, or `SideEffect` instead of emitting from a composable body. Composable bodies
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" />
Expand Down
Loading