Title: Add `scope.SetEnvironment` · Issue #5769 · getsentry/sentry-java · GitHub
Open Graph Title: Add `scope.SetEnvironment` · Issue #5769 · getsentry/sentry-java
X Title: Add `scope.SetEnvironment` · Issue #5769 · getsentry/sentry-java
Description: Proposal: Allow setting Environment on the Scope Unblocks getsentry/sentry-dotnet#5387 getsentry/sentry-unity#2764 Summary Add getEnvironment() / setEnvironment(String) to sentry-java's IScope, so other SDKs (hybrid SDKs like .NET, Godot...
Open Graph Description: Proposal: Allow setting Environment on the Scope Unblocks getsentry/sentry-dotnet#5387 getsentry/sentry-unity#2764 Summary Add getEnvironment() / setEnvironment(String) to sentry-java's IScope, so ...
X Description: Proposal: Allow setting Environment on the Scope Unblocks getsentry/sentry-dotnet#5387 getsentry/sentry-unity#2764 Summary Add getEnvironment() / setEnvironment(String) to sentry-java's IScope,...
Opengraph URL: https://github.com/getsentry/sentry-java/issues/5769
X: @github
Domain: github.com
IPv4 address: 140.82.114.4
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Add `scope.SetEnvironment`","articleBody":"# Proposal: Allow setting `Environment` on the Scope\n\nUnblocks\n- https://github.com/getsentry/sentry-dotnet/issues/5387\n- https://github.com/getsentry/sentry-unity/pull/2764\n\n## Summary\n\nAdd `getEnvironment()` / `setEnvironment(String)` to sentry-java's `IScope`, so other SDKs (hybrid SDKs like .NET, Godot, Unity, Unreal, React Native?) and end users can set the `Environment` on the scope at runtime. This lifts the restriction of having to provide it strictly at init via `SentryOptions.setEnvironment(...)`.\n\nThis brings `sentry-java` to parity with `sentry-cocoa` and `sentry-native`. These already expose a scope-/runtime-level environment setter.\n\n## Motivation\n\nToday `environment` in sentry-java lives only on `SentryOptions`, fixed at init time. Any hybrid SDKs layered on top either manage their own `environment` or provide a way for users to modify it at runtime. This creates the need to sync it into the Java layer at runtime. That way events, and persisted (background/ANR/NDK) events, carry the correct value. \n\nBoth reference SDKs already support this:\n\n| SDK | API | Level | Getter? |\n|--------|--------------------------------------------------------------|--------------|---------|\n| cocoa | `- (void)setEnvironment:(NSString *)environment` on `Scope` | scope | no |\n| native | `sentry_set_environment(const char *environment)` (+ `_n`) | global scope | no |\n| java | **(proposed)** `IScope.setEnvironment(String)` + `getEnvironment()` | scope | yes |\n\n## Proposed API (sentry-java)\n\n```java\npublic interface IScope {\n // ...\n @Nullable String getEnvironment();\n\n /**\n * Sets the Scope's environment. Takes precedence over SentryOptions#getEnvironment()\n * when applied to events. Mainly used by hybrid SDKs to sync their environment.\n */\n void setEnvironment(@Nullable String environment);\n}\n```\n\nReached by users/hybrid SDKs the same way as other scope mutations:\n\n```java\nSentry.configureScope(scope -> scope.setEnvironment(\"staging\"));\n```\n\nUnlike cocoa, we could also add a **getter**, consistent with other Java scope fields like `getTransactionName`, and `getUser` e.t.c. and required by the combined-scope view.\n\n### API References\n\n**sentry-cocoa**\n- [`SentryScope.h`](https://github.com/getsentry/sentry-cocoa/blob/8693a8f07525287fcc3714cdbc505cf0b73b4d84/Sources/Sentry/Public/SentryScope.h#L99) - `(void)setEnvironment:(NSString *_Nullable)environment;`\n- [`SentryScope.m`](https://github.com/getsentry/sentry-cocoa/blob/8693a8f07525287fcc3714cdbc505cf0b73b4d84/Sources/Sentry/SentryScope.m#L385) - sets `environmentString`, then notifies scope observers.\n- [`SentryScope::applyToEvent`](https://github.com/getsentry/sentry-cocoa/blob/8693a8f07525287fcc3714cdbc505cf0b73b4d84/Sources/Sentry/SentryScope.m#L638-L643) - odering & backfill\n- Usage: `SentrySDK.configureScope { $0.setEnvironment(\"staging\") }`\n\n**sentry-native**\n- [`sentry.h`](https://github.com/getsentry/sentry-native/blob/2723a0fb5707ae8596e90ae041d397d8ce4e9ceb/include/sentry.h#L2272-L2278) - `SENTRY_API void sentry_set_environment(const char *environment);`\n- [`sentry_core.c`](https://github.com/getsentry/sentry-native/blob/2723a0fb5707ae8596e90ae041d397d8ce4e9ceb/src/sentry_core.c#L935-L951) - overwrites `scope->environment`\n- [`sentry__scope_apply_to_event`](https://github.com/getsentry/sentry-native/blob/2723a0fb5707ae8596e90ae041d397d8ce4e9ceb/src/sentry_scope.c#L449) \n- Usage: `sentry_set_environment(\"staging\");`\n\n## Precedence of application\n\nEffective order from top to bottom: \"environment set on the event itself\" > scope > options**\n\n- `SentryClient.applyScope`: applies scope environment to the event only when the event has none.\n- [`MainEventProcessor.setEnvironment`](https://github.com/getsentry/sentry-java/blob/20be537e26bae44a7ffdb1b207ea3f631a711a15/sentry/src/main/java/io/sentry/MainEventProcessor.java#L154-L158): later fallback to `options.getEnvironment()` when still unset\n\nThis matches the precedence used by `cocoa` and `native`.\n\n## Known gap / suggested follow-up\n\nThe Android **AEI/ANR backfill** processor reads environment only from the *options* cache, not the *scope* cache:\n\n- [`ApplicationExitInfoEventProcessor.setEnvironment`](https://github.com/getsentry/sentry-java/blob/20be537e26bae44a7ffdb1b207ea3f631a711a15/sentry-android-core/src/main/java/io/sentry/android/core/ApplicationExitInfoEventProcessor.java#L461-L466) reads `PersistingOptionsObserver.ENVIRONMENT_FILENAME`, falling back to `options.getEnvironment()`:\n\nSo a scope-set environment is persisted (via the new `PersistingScopeObserver` file) but is **not** picked up when backfilling exit-info events. \nThis could be resolved by reading the scope file first?","author":{"url":"https://github.com/bitsandfoxes","@type":"Person","name":"bitsandfoxes"},"datePublished":"2026-07-16T11:15:48Z","url":"https://github.com/getsentry/sentry-java/issues/5769"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e73675bd-406a-c37f-6df2-838d20b28e01 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | C602:347215:42B2F2:5BB4AD:6B0451B0 |
| html-safe-nonce | 0a0d5c04c6e653f0da1c812985e75c8c816c2832ebbdc4dc3e4d1bced3b0b972 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDNjAyOjM0NzIxNTo0MkIyRjI6NUJCNEFEOjZCMDQ1MUIwIiwidmlzaXRvcl9pZCI6Ijg5NTE1ODQ2ODI3OTgyNDAxNzYiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | b9e8b2b503de595e8bc5d85f0305516f685dc4ced250a7045ce0dd7ee073dcc2 |
| hovercard-subject-tag | issue:4901434810 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/getsentry/sentry-java/5769/issue_layout |
| twitter:image | https://opengraph.githubassets.com/781b30fc14494052a8daefc17a4658a5dd81ddb7c69b8f0a4b65e01bb8d61999/getsentry/sentry-java/issues/5769 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/781b30fc14494052a8daefc17a4658a5dd81ddb7c69b8f0a4b65e01bb8d61999/getsentry/sentry-java/issues/5769 |
| og:image:alt | Proposal: Allow setting Environment on the Scope Unblocks getsentry/sentry-dotnet#5387 getsentry/sentry-unity#2764 Summary Add getEnvironment() / setEnvironment(String) to sentry-java's IScope, so ... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | bitsandfoxes |
| hostname | github.com |
| expected-hostname | github.com |
| None | 910ca0e260cfec6aff11407d8dc6a0d3a4cd381a2ec9ada56627b063426892a0 |
| turbo-cache-control | no-preview |
| go-import | github.com/getsentry/sentry-java git https://github.com/getsentry/sentry-java.git |
| octolytics-dimension-user_id | 1396951 |
| octolytics-dimension-user_login | getsentry |
| octolytics-dimension-repository_id | 3368190 |
| octolytics-dimension-repository_nwo | getsentry/sentry-java |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 3368190 |
| octolytics-dimension-repository_network_root_nwo | getsentry/sentry-java |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | e086f9b7e88322eb2b41885a6ad425b215737fa5 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
| Server | github.com |
| Content-Type | text/html; charset=utf-8 |
| Cache-Control | max-age=0, private, must-revalidate |
Viewport: width=device-width