sentry-java icon indicating copy to clipboard operation
sentry-java copied to clipboard

Allow using a custom Hub with SentryTraced for Jetpack Compose

Open vanshg opened this issue 2 years ago • 5 comments

Problem Statement

I would like to provide my own Hub when using the SentryTraced composable.

Currently, the default SentryTraced composable relies on the top-level static Sentry object for fetching the current transaction (by way of the getRootSpan() function), which can be limiting for users who wish to utilize a custom hub. This limitation can lead to potential issues when attempting to manage multiple Sentry/Hub instances or when needing to apply custom configurations to individual instances.

Solution Brainstorm

Some potential solutions could be:

  • Allowing a Hub to be passed in to the composable
  • Allowing a Transaction to be passed in to the composable

vanshg avatar Apr 21 '23 18:04 vanshg

@vanshg thanks for trying out the new tracing feature! To me this totally makes sense to allow passing a hub instance, @markushi what do you think?

romtsn avatar Apr 21 '23 18:04 romtsn

Hello again! Any update on this?

vanshg avatar Apr 27 '23 19:04 vanshg

Hey @vanshg, thanks for reaching out! I guess expanding SentryTraced with a hub argument should be easy enough to cover your use case

public fun SentryTraced(
    tag: String,
    modifier: Modifier = Modifier,
    hub = Sentry.getCurrentHub(),
    enableUserInteractionTracing: Boolean = true,
    content: @Composable BoxScope.() -> Unit
)

Just out of curiosity: Could you elaborate a bit why you'd want to use a custom hub?

markushi avatar May 03 '23 06:05 markushi

On a second thought, https://developer.android.com/jetpack/compose/compositionlocal would be a more "Compose" way of doing this. We even do that right now (see source) we would just need to expose the APIs, as they're private right now.

markushi avatar May 03 '23 06:05 markushi

Just out of curiosity: Could you elaborate a bit why you'd want to use a custom hub?

We develop an SDK and we track crashes via our own Hub with our own DSN, so as not to intrude on the application developer's usage of Sentry from the top-level/static Sentry

https://developer.android.com/jetpack/compose/compositionlocal would be a more "Compose" way of doing this

I wouldn't be opposed to a composition local approach. But I wonder how the interface for that would work? If you're setting the composition local inside the SentryTraced composable, how could I override it from outside? Is there a way to have a default composition local value if one hasn't been set? (I haven't worked directly with creating my own composition locals, so pardon any ignorance)

vanshg avatar May 03 '23 13:05 vanshg