Skip to content
Merged
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
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
### Features

- Create onCreate and onStart spans for all Activities ([#4025](https://github.com/getsentry/sentry-java/pull/4025))
- Add split apks info to the `App` context ([#3193](https://github.com/getsentry/sentry-java/pull/3193)))
- Add split apks info to the `App` context ([#3193](https://github.com/getsentry/sentry-java/pull/3193))
- Expose new `withSentryObservableEffect` method overload that accepts `SentryNavigationListener` as a parameter ([#4143](https://github.com/getsentry/sentry-java/pull/4143))
- This allows sharing the same `SentryNavigationListener` instance across fragments and composables to preserve the trace

### Fixes

Expand Down
1 change: 1 addition & 0 deletions sentry-compose/api/android/sentry-compose.api
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public final class io/sentry/compose/SentryModifier {
}

public final class io/sentry/compose/SentryNavigationIntegrationKt {
public static final fun withSentryObservableEffect (Landroidx/navigation/NavHostController;Lio/sentry/android/navigation/SentryNavigationListener;Landroidx/compose/runtime/Composer;I)Landroidx/navigation/NavHostController;
public static final fun withSentryObservableEffect (Landroidx/navigation/NavHostController;ZZLandroidx/compose/runtime/Composer;II)Landroidx/navigation/NavHostController;
}

Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,22 @@ internal class SentryLifecycleObserver(
* A [DisposableEffect] that captures a [Breadcrumb] and starts an [ITransaction] and sends
* them to Sentry for every navigation event when being attached to the respective [NavHostController].
*
* @param enableNavigationBreadcrumbs Whether the integration should capture breadcrumbs for
* navigation events.
* @param enableNavigationTracing Whether the integration should start a new [ITransaction]
* with [SentryOptions.idleTimeout] for navigation events.
* @param navListener An instance of a [SentryNavigationListener] that is shared with other sentry integrations, like
* the fragment navigation integration.
*/
@Composable
@NonRestartableComposable
public fun NavHostController.withSentryObservableEffect(
enableNavigationBreadcrumbs: Boolean = true,
enableNavigationTracing: Boolean = true
navListener: SentryNavigationListener
): NavHostController {
val enableBreadcrumbsSnapshot by rememberUpdatedState(enableNavigationBreadcrumbs)
val enableTracingSnapshot by rememberUpdatedState(enableNavigationTracing)
val navListenerSnapshot by rememberUpdatedState(navListener)

// As described in https://developer.android.com/codelabs/jetpack-compose-advanced-state-side-effects#6
val lifecycle = LocalLifecycleOwner.current.lifecycle
DisposableEffect(lifecycle, this) {
val observer = SentryLifecycleObserver(
this@withSentryObservableEffect,
navListener = SentryNavigationListener(
enableNavigationBreadcrumbs = enableBreadcrumbsSnapshot,
enableNavigationTracing = enableTracingSnapshot,
traceOriginAppendix = TRACE_ORIGIN_APPENDIX
)
navListener = navListenerSnapshot
)

lifecycle.addObserver(observer)
Expand All @@ -84,6 +76,34 @@ public fun NavHostController.withSentryObservableEffect(
return this
}

/**
* A [DisposableEffect] that captures a [Breadcrumb] and starts an [ITransaction] and sends
* them to Sentry for every navigation event when being attached to the respective [NavHostController].
* This version of withSentryObservableEffect should be used if you are working purely with Compose.
*
* @param enableNavigationBreadcrumbs Whether the integration should capture breadcrumbs for
* navigation events.
* @param enableNavigationTracing Whether the integration should start a new [ITransaction]
* with [SentryOptions.idleTimeout] for navigation events.
*/
@Composable
@NonRestartableComposable
public fun NavHostController.withSentryObservableEffect(
enableNavigationBreadcrumbs: Boolean = true,
enableNavigationTracing: Boolean = true
): NavHostController {
val enableBreadcrumbsSnapshot by rememberUpdatedState(enableNavigationBreadcrumbs)
val enableTracingSnapshot by rememberUpdatedState(enableNavigationTracing)

return withSentryObservableEffect(
navListener = SentryNavigationListener(
enableNavigationBreadcrumbs = enableBreadcrumbsSnapshot,
enableNavigationTracing = enableTracingSnapshot,
traceOriginAppendix = TRACE_ORIGIN_APPENDIX
)
)
}

/**
* A [DisposableEffect] that captures a [Breadcrumb] and starts an [ITransaction] and sends
* them to Sentry for every navigation event when being attached to the respective [NavHostController].
Expand Down
Loading