Skip to content

chore(android): Add docs for using the Android SDK within a shared environment #11089

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

Merged
merged 3 commits into from
Aug 27, 2024
Merged
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
60 changes: 60 additions & 0 deletions docs/platforms/android/configuration/shared-environments.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
title: Using Sentry within an SDK
description: "Learn how to use the Sentry SDK within a shared environment, e.g. another SDK."
sidebar_order: 2000
---

<Note>

Using the Sentry SDK within another SDK is [discouraged](https://docs.sentry.io/platforms/). This can lead to unexpected behaviour and potential data leakage. If you need to use Sentry within another SDK, please follow the best practices outlined below.

</Note>

<Note>

When setting up Sentry inside a library, the consuming app could use the Sentry SDK as well, thus you should **not use `Sentry.init()`**, as this will pollute the global state.

</Note>

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any specific use cases like Browser Extensions in JS, just so readers won't confuse it with using RN and Android for example.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, but I rephrased it a bit to make it more clear - thanks!

In order to not conflict with other Sentry instances, you should use the `Hub` API to create a new instance of Sentry.
The Hub API works the same way as the global Sentry instance, but it is not global and can be used within your component. If you want to capture uncaught exceptions, you can use the `UncaughtExceptionHandlerIntegration` to capture them. As this will capture all uncaught exceptions within an app, you should use the `BeforeSendCallback` to only accept events that are relevant for your SDK.

```kotlin
import io.sentry.Hub
import io.sentry.SentryOptions
import io.sentry.SentryOptions.BeforeSendCallback
import io.sentry.UncaughtExceptionHandlerIntegration

val options = SentryOptions().apply {
dsn = "___PUBLIC_DSN___"
isEnableUncaughtExceptionHandler = true
setBeforeSend { event, _ ->
// as uncaught exceptions are captured globally,
// you need to only accept events which are relevant
if (isRelevantForMySdk(event.throwable)) {
return@setBeforeSend event
}
// drop the event
return@setBeforeSend null
}

}

val hub = Hub(options)

val integration = UncaughtExceptionHandlerIntegration()
options.addIntegration(integration)
integration.register(hub, options)
```

Once the Hub is configured, you can use it to capture events:

```kotlin
hub.captureException(IllegalStateException("Example Exception"))
```

If your SDK can be opened and closed multiple times, you should also close the Hub when you are done with it:

```kotlin
hub.close()
```
Loading