Scopes and Hubs
When an event is captured and sent to Sentry, SDKs will merge that event data with extra information from the current scope. SDKs will typically automatically manage the scopes for you in the framework integrations and you don't need to think about them. However, you should know what a scope is and how you can use it for your advantage.
What's a Scope, What's a Hub
You can think of the hub as the central point that our SDKs use to route an
event to Sentry. When you call init()
a hub is created and a client and a
blank scope are created on it. That hub is then associated with the current
thread and will internally hold a stack of scopes.
The scope will hold useful information that should be sent along with the event. For instance contexts or breadcrumbs are stored on the scope. When a scope is pushed, it inherits all data from the parent scope and when it pops all modifications are reverted.
The default SDK integrations will push and pop scopes intelligently. For instance web framework integrations will create and destroy scopes around your routes or controllers.
How the Scope and Hub Work
As you start using an SDK, a scope and hub are automatically created for you out
of the box. It's unlikely that you'll interact with the hub directly unless you're
writing an integration or you want to create or destroy scopes. Scopes, on the
other hand are more user facing. You can call configure-scope
at any point in
time to to modify data stored on the scope. This is useful for doing things like
modifying the context.
Effectively this means that when you spawn a task in .NET and the execution flow is not suppressed all the context you have bound to the scope in Sentry will flow along. If however you suppress the flow, you get new scope data.
When you call a global function such as capture_event
internally Sentry
discovers the current hub and asks it to capture an event. Internally the hub will
then merge the event with the topmost scope's data.
Configuring the Scope
The most useful operation when working with scopes is the configure-scope
function. It can be used to reconfigure the current scope.
You can, for instance, add custom tags or inform Sentry about the currently authenticated user.
import io.sentry.Sentry;
import io.sentry.protocol.User;
Sentry.configureScope(scope -> {
scope.setTag("my-tag", "my value");
User user = new User();
user.setId("42");
user.setEmail("john.doe@example.com");
scope.setUser(user);
});
You can also apply this configuration when unsetting a user at logout:
import io.sentry.Sentry;
Sentry.configureScope(scope -> {
scope.setUser(null);
});
Scope Synchronization
If you want to set context data in the Java layer, then receive that context data when a native (C/C++) crash happens, you need to synchronize the Java Scope
with the native Scope
. This feature was introduced in version 3.0.0
of the Android SDK and requires an opt-in:
AndroidManifest.xml
<application>
<meta-data android:name="io.sentry.ndk.scope-sync.enable" android:value="true" />
</application>
To learn what useful information can be associated with scopes see the context documentation.
Our documentation is open source and available on GitHub. Your contributions are welcome, whether fixing a typo (drat!) to suggesting an update ("yeah, this would be better").
- Package:
- maven:io.sentry:sentry-android
- Version:
- 5.7.4
- Repository:
- https://github.com/getsentry/sentry-java