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.
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.
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 configureScope
at any point in time to modify data stored on the scope. This is useful for doing things like modifying the context.
When you call a global function such as captureEvent
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.
The most useful operation when working with scopes is the configureScope
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);
});
To learn what useful information can be associated with scopes see the context documentation.
We also support pushing and configuring a scope within a single call. This is typically called withScope
, pushScope
or implemented as a function parameter on the capture methods, depending on the SDK. It's very helpful if you only want to send data for one specific event.
In the following example we use withScope
to attach a level
and a tag
to only one specific error:
import io.sentry.Sentry;
import io.sentry.SentryLevel;
Sentry.withScope(scope -> {
scope.setTag("my-tag", "my value");
scope.setLevel(SentryLevel.WARNING);
// will be tagged with my-tag="my value"
Sentry.captureException(new Exception("my error"));
});
// will not be tagged with my-tag
Sentry.captureException(new Exception("my error"));
Important
In the Java SDK with-scope
does not work reliably in globalHubMode
as the scope
gets pushed on the stack global to the hub. In globalHubMode
use the callback parameter of the capture methods detailed below.
While this example looks similar to configureScope
, it is actually very different. Calls to configureScope
change the current active scope; all successive calls to configureScope
will maintain previously set changes unless they are explicitly unset.
On the other hand, withScope
creates a clone of the current scope, and the changes made will stay isolated within the withScope
callback function. This allows you to more easily isolate pieces of context information to specific locations in your code or even call clear
to briefly remove all context information.
In the following example we use the scope callback parameter that is available for all capture
methods to attach a level
and a tag
to only one specific error:
import io.sentry.Sentry;
import io.sentry.SentryLevel;
// will be tagged with my-tag="my value"
Sentry.captureException(new Exception("my error"), scope -> {
scope.setTag("my-tag", "my value");
scope.setLevel(SentryLevel.WARNING);
});
// will not be tagged with my-tag
Sentry.captureException(new Exception("my error"));
Before the callback is invoked the SDK creates a clone of the current scope, and the changes made will stay isolated within the callback function. This allows you to more easily isolate pieces of context information to specific locations in your code or even call clear
to briefly remove all context information.
Important
Any exceptions that occur within the callback function for configuring a local scope will not be caught, and all errors that occur will be silently ignored and not reported.
Sentry's SDK for Java stores the scope and the context in a thread-local variable. To make sure that a coroutine has access to the correct Sentry context, an instance of SentryContext
must be provided when launching a coroutine.
<dependency>
<groupId>io.sentry</groupId>
<artifactId>sentry-kotlin-extensions</artifactId>
<version>7.8.0</version>
</dependency>
import io.sentry.kotlin.SentryContext
import io.sentry.Sentry
launch(SentryContext()) {
// tag set in parent coroutine is visible to child coroutine
Sentry.setTag("parent-tag", "value")
launch() {
// tag set in child coroutine is not visible in parent coroutine
Sentry.setTag("child-tag", "value")
}
}
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").
- Package:
- maven:io.sentry:sentry
- Version:
- 7.8.0
- Repository:
- https://github.com/getsentry/sentry-java
- API Documentation:
- https://javadoc.io/doc/io.sentry