Using Sentry Within an SDK
Learn how to use the Sentry SDK within a shared environment, such as another SDK.
Using the Sentry SDK within another SDK is discouraged. This can lead to unexpected behaviour and potential data leakage. The SentrySDK uses global state and can therefore not be initialized multiple times in parallel. When using the SentrySDK in your SDK, this will collide with apps using Sentry and your SDK. If you need to use Sentry within another SDK, please follow the best practices outlined below.
When setting up Sentry inside a library, the consuming app could use the Sentry SDK as well, thus you should not use SentrySDK.start()
, as this will pollute the global state.
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 similarly as the global Sentry instance, but it is not global and can be used within your component.
import Sentry
let options = Options()
options.dsn = "https://examplePublicKey@o0.ingest.sentry.io/0example-org / example-project"
let client = SentryClient(options: options)
// You don't need to pass a scope. The hub will create a new scope for you automatically.
// Furthermore, the SDK enriches it with some contexts automatically, such as app, OS and
// device context.
let hub = SentryHub(client: client, andScope: nil)
hub.capture(message: "Hello from your SDK!")
If your SDK can be opened and closed multiple times, you should also close the Hub
when you are done:
hub.close()
Capturing crashes, watchdog terminations or app hangs aren't supported by the Hub
API, because they depend on the global Sentry instance.
To symbolicate stacktraces of your events, Sentry requires dSYMs (debug information files). Depending on your setup, you might not be able to get these, but if you can, visit Uploading Debug Symbols for more information on how to upload them.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) or suggesting an update ("yeah, this would be better").