Skip to content
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 send 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.

Instead, send 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 send it from an Effect API:

```kotlin
@Composable
fun LoginScreen() {
val firstComposedAt = remember { Instant.now() }
Comment thread
0xadam-brown marked this conversation as resolved.

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 send custom Sentry spans or other telemetry from Jetpack Compose code, use Compose Effect APIs such as
`LaunchedEffect`, `DisposableEffect`, or `SideEffect` instead of sending 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