sentry-java / rules
Cursor rule1.4k starsChanged 7 months agoMITView on GitHub
Java SDK Hubs and Scopes
Trending
Quiet this year.about the whole repository
Who's talking about getsentry/sentry-java
- getsentry/sentry“[Mobile Replay] Capture Network request/response bodies”GitHub · Feb 5, 2025 · 48 reactions
- quarkusio/quarkus“Sentry tracing with automatic instrumentation”GitHub · Aug 31, 2021 · 19 reactions
- getsentry/sentry-dart“Support Source Context”GitHub · Oct 25, 2020 · 18 reactions
- getsentry/sentry-dotnet“OpenTelemetry Support”GitHub · Nov 25, 2022 · 11 reactions
- getsentry/sentry-dotnet“Feature: Session Replay support for .NET MAUI”GitHub · Jan 30, 2023 · 8 reactions
The biggest 5 of 18 mentions linking to the repository that our crawl found. Each line is their own headline.
See all 18
Talked about on GitHub (18 times).
On GitHub · 18
- Feb 5, 2025[Mobile Replay] Capture Network request/response bodies — getsentry/sentry48 reactions · 13 comments
- Aug 31, 2021Sentry tracing with automatic instrumentation — quarkusio/quarkus19 reactions · 20 comments
- Oct 25, 2020Support Source Context — getsentry/sentry-dart18 reactions · 24 comments
- Nov 25, 2022OpenTelemetry Support — getsentry/sentry-dotnet11 reactions · 4 comments
- Jan 30, 2023Feature: Session Replay support for .NET MAUI — getsentry/sentry-dotnet8 reactions · 9 comments
13 more on GitHub
- Mar 19, 2025Add UI Profiling Support (converted to project) — getsentry/sentry-react-native5 reactions · 3 comments
- Jan 20, 2021Proposal (Performance): Some convenience API to wrap a Promise with a Span/Transaction — getsentry/sentry-javascript5 reactions · 2 comments
- May 1, 2025feat: Initial Android support — getsentry/sentry-godot4 reactions · 3 comments
- Mar 25, 2024feat(replay): Add Mobile Replay Alpha — getsentry/sentry-react-native4 reactions · 8 comments
- Feb 8, 2023Add support for Sentry — spring-io/start.spring.io4 reactions · 6 comments
- May 4, 2022Figure out how to implement hints — getsentry/sentry-javascript4 reactions · 5 comments
- Dec 10, 2020Add basic support for attachments — getsentry/sentry-cocoa4 reactions
- Jul 17, 2025Bump io.sentry.android.gradle from 4.10.0 to 5.8.0 — woocommerce/woocommerce-android3 reactions · 2 comments
- Jul 23, 2024feat: User Feedback widget/form — getsentry/sentry-cocoa3 reactions · 5 comments
- May 1, 2024Remove `enableTracing` (and update docs accordingly) — getsentry/team-sdks3 reactions · 7 comments
- Feb 1, 2024[Epic] Mobile Session Replay — getsentry/sentry3 reactions · 2 comments
- Nov 20, 2023Tracing: W3C Trace Context Support — getsentry/team-sdks3 reactions · 9 comments
- Jan 12, 2023Allow more granular auto installation — getsentry/sentry-android-gradle-plugin3 reactions · 3 comments
Where files are headingNew Cursor rule files have more rules about planning and tracking tasks, and fewer about code style.
Of the Cursor rule files we read, the 1,533 first seen in the last six months, against the older ones: the kind of rule that gained most share, and the kind that lost most. About files like this in general, not this one, and an estimate, since a kind is a pattern in the words.
In this repository17 other files it gives its agents.
- AGENTS.mdAGENTS.md
- CLAUDE.mdCLAUDE.md
- .cursor/rules/api.mdcCursor rule
- .cursor/rules/continuous_profiling_jvm.mdcCursor rule
- .cursor/rules/deduplication.mdcCursor rule
- .cursor/rules/e2e_tests.mdcCursor rule
- .cursor/rules/feature_flags.mdcCursor rule
- .cursor/rules/metrics.mdcCursor rule
- .cursor/rules/new_module.mdcCursor rule
- .cursor/rules/offline.mdcCursor rule
- .cursor/rules/opentelemetry.mdcCursor rule
- .cursor/rules/options.mdcCursor rule
- .cursor/rules/queues.mdcCursor rule
- btrace-perfettoSkill
- check-code-attributionSkill
- create-java-prSkill
- testSkill
The file
We split this scopes.mdc into the rules it gives an agent
6 rules. About 0 are common, 1 is distinctive, and 5 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
5
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Distinctive: line 24
Instead we now fork scopes for a given lifecycle and then restore the previous scopes.
- Very specific: line 67
When the scopes are forked, they are stored into the ThreadLocal and a ISentryLifecycleToken is returned.
- Very specific: line 68
When the Scopes are no longer needed, e.g.
- Very specific: line 93
If globalHubMode is enabled, the SDK avoids forking scopes _implicitly_.
- Very specific: line 112
If globalHubMode is disabled, the SDK forks scopes freely, e.g.
- Very specific: line 120
When using API like Sentry.setTag the SDK adds that tag to the default Scope.
≈ “Common” is an estimate: at least 1 in 5 of the 3,030 Cursor rule files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
---
alwaysApply: false
description: Java SDK Hubs and Scopes
---
# Java SDK Hubs and Scopes
## `Scopes`
`Scopes` implements `IScopes` and manages three `Scope` instances, `global`, `isolation` and `current` scope.
For some data, all three `Scope` instances are combined, for others, a certain one is used exclusively and for some we look at each scope in a certain order and use the data of the first scope that has the data set. This logic is contained in `CombinedScopeView`.
Data itself is stored on `Scope` instances.
`Scopes` also has a `parent` field, linking the `Scopes` it was forked off of and a `creator` String, explaining why it was forked.
## `Hub`
Up until major version 7 of the Java SDK the `IHub` interface was a central part of the SDK.
In major version 8 we replaced the `IHub` interface with `IScopes`. `IHub` has been deprecated.
While there is some bridging code in place to allow for easier migration, we are planning to remove it in an upcoming major.
## Scope Types
We have introduced some new Scope types in the SDK, allowing for better control over what data is attached where.
Previously there was a stack of scopes that was pushed and popped.
Instead we now fork scopes for a given lifecycle and then restore the previous scopes.
Since Hub is gone, it is also never cloned anymore.
Separation of data now happens through the different scope types while making it easier to manipulate exactly what you need without having to attach data at the right time to have it apply where wanted.
### Global Scope
Global scope is attached to all events created by the SDK.
It can also be modified before Sentry.init has been called.
It can be manipulated using `Sentry.configureScope(ScopeType.GLOBAL, (scope) -> { ... })`.
Global scope can be retrieved from `Scopes` via `getGlobalScope`. It can also be retrieved directly via `Sentry.getGlobalScope`.
### Isolation Scope
Isolation scope can be used e.g. to attach data to all events that come up while handling an incoming request.
It can also be used for other isolation purposes.
It can be manipulated using `Sentry.configureScope(ScopeType.ISOLATION, (scope) -> { ... })`.
The SDK automatically forks isolation scope in certain cases like incoming requests, CRON jobs, Spring `@Async` and more.
Isolation scope can be retrieved from `Scopes` via `getIsolationScope`.
### Current scope
Current scope is forked often and data added to it is only added to events that are created while this scope is active.
Data is also passed on to newly forked child scopes but not to parents.
Current scope can be retrieved from `Scopes` via `getScope`.
### Combined Scope
This is a special scope type that combines global, isolation and current scope.
Refer to `CombinedScopeView` for each field of interest to see whether values from the three individual scopes are merged,
whether a specific one is used or whether we're simply using the first one that has a value.
Also see the section about `defaultScopeType` further down.
## Storage of `Scopes`
`Scopes` are stored in a `ThreadLocal` by default (NOTE: this is different for OpenTelemetry, see opentelemetry.mdc).
This happens through `Sentry.scopesStorage` and `DefaultScopesStorage`.
The lifetime of `Scopes` in the thread local is managed by `ISentryLifecycleToken`.
When the scopes are forked, they are stored into the `ThreadLocal` and a `ISentryLifecycleToken` is returned.
When the `Scopes` are no longer needed, e.g. because a request is finished, `ISentryLifecycleToken.close` can be called to restore the previous state of the `ThreadLocal`.
## Old versions of the Java SDK
There were several implementations of the `IHub` interface:
- `Hub` managed a stack of `Scope` instances, which were pushed and popped.
- A `Hub` could be cloned, meaning there could be multiple stacks of scopes active, e.g. for two separate requests being handled in a server application.
### Migrating to major version 8 of the SDK
`IHub` has been replaced by `IScopes`
`HubAdapter` has been replaced by `ScopesAdapter`
`Hub.clone` should be replaced by using `pushScope` or `pushIsolationScope`
`Sentry.getCurrentHub` has been replaced by `Sentry.getCurrentScopes`
`Sentry.popScope` has been deprecated. Instead `close` should be called on the `ISentryLifecycleToken` returned e.g. by `pushScope`. This can also be done in a `try-with-resource` block.
## `globalHubMode`
The SDK has a `globalHubMode` option which affects forking behaviour of the SDK.
Android has `globalHubMode` enabled by default.
For JVM Desktop applications, `globalHubMode` can be used.
For JVM Backend applications (servers) we discourage enabling `globalHubMode` since it will cause scopes to bleed into each other. This can e.g. mean that state from request A leaks into request B and events sent to Sentry contain a mix of both request A and B potentially rendering the data useless.
### Enabled
If `globalHubMode` is enabled, the SDK avoids forking scopes _implicitly_.
This means, retrieving current scopes on a thread where specific scopes do not exist yet for the thread, the root scopes are not forked but returned directly.
`Sentry.pushScope`, `Sentry.pushIsolationScope` and `Sentry.popScope` are no-ops.
They are unbalanced API: the caller may never restore the previous scopes, or restore them on a different thread, which would corrupt the globally shared scopes.
`Sentry.withScope` and `Sentry.withIsolationScope` do fork, also when `globalHubMode` is enabled.
They are balanced by construction and always restore the previous scopes before returning, so they cannot corrupt the shared scopes.
This matches the cross SDK spec ("fork the current scope, invoke callback, discard the fork when done") and what the SDK did before major version 8.
The suppression of forking via `globalHubMode` only applies when using `Sentry` static API or `ScopesAdapter`.
In case the `Scopes` instance is accessed directly, forking will happen as if `globalHubMode` is disabled.
`Sentry.setCurrentScopes`, and thus `Scopes.makeCurrent`, does have an effect when `globalHubMode` is enabled, but only for scopes that descend from the current `rootScopes`.
`Sentry.getCurrentScopes` ignores all other scopes, because scopes left over from a closed or re-initialized SDK would otherwise be read back from a thread local that the SDK cannot clean up.
`ScopesAdapter` makes use of the static `Sentry` API internally. It allows us to access the correct scopes for the current context without passing it along explicitly. It also makes testing easier.
### Disabled
If `globalHubMode` is disabled, the SDK forks scopes freely, e.g. when:
- `Sentry.getCurrentScopes()` is executed on a Thread where no specific scopes for that thread have been stored yet. In this case the SDK will fork `rootScopes` (stored in a `Sentry` static property).
- `withScope` or `withIsolationScope` are executed
- `pushScope` or `pushIsolationScope` are executed
## `defaultScopeType`
The `defaultScopeType` controls which `Scope` instance is being used for writing to and reading from as a default value.
When using API like `Sentry.setTag` the SDK adds that tag to the default `Scope`.
This also ensures, customers who migrate to the latest SDK version and already have `Sentry.configureScope` invocations in place, will now write to the default `Scope` instance that was chosen.
The default value for `defaultScopeType` is `ISOLATION` scope for JVM and `CURRENT` scope for Android.
Which fields are written/read from/to `defaultScopeType` is controlled in `CombinedScopeView`.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

