Hacktoberfest 2026:維護者為十月標記出來的 issue,仍然開放、適合新手。 瀏覽 Hacktoberfest issue

Add `scope.SetEnvironment`

未關閉
#5,769 1 則留言 0 個 reaction 已指派 1 人 在 GitHub 檢視

@bitsandfoxes 已經在處理了。

開始於 2026年7月20日。

評估

這個 Issue 還沒有評估資料。

描述

Feature Java Platform: Java

Proposal: Allow setting Environment on the Scope

Unblocks

Summary

Add 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(...).

This brings sentry-java to parity with sentry-cocoa and sentry-native. These already expose a scope-/runtime-level environment setter.

Motivation

Today 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.

Both reference SDKs already support this:

SDK API Level Getter?
cocoa - (void)setEnvironment:(NSString *)environment on Scope scope no
native sentry_set_environment(const char *environment) (+ _n) global scope no
java (proposed) IScope.setEnvironment(String) + getEnvironment() scope yes

Proposed API (sentry-java)

public interface IScope {
  // ...
  @Nullable String getEnvironment();

  /**
   * Sets the Scope's environment. Takes precedence over SentryOptions#getEnvironment()
   * when applied to events. Mainly used by hybrid SDKs to sync their environment.
   */
  void setEnvironment(@Nullable String environment);
}

Reached by users/hybrid SDKs the same way as other scope mutations:

Sentry.configureScope(scope -> scope.setEnvironment("staging"));

Unlike 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.

API References

sentry-cocoa

  • SentryScope.h - (void)setEnvironment:(NSString *_Nullable)environment;
  • SentryScope.m - sets environmentString, then notifies scope observers.
  • SentryScope::applyToEvent - odering & backfill
  • Usage: SentrySDK.configureScope { $0.setEnvironment("staging") }

sentry-native

Precedence of application

Effective order from top to bottom: "environment set on the event itself" > scope > options**

  • SentryClient.applyScope: applies scope environment to the event only when the event has none.
  • MainEventProcessor.setEnvironment: later fallback to options.getEnvironment() when still unset

This matches the precedence used by cocoa and native.

Known gap / suggested follow-up

The Android AEI/ANR backfill processor reads environment only from the options cache, not the scope cache:

So a scope-set environment is persisted (via the new PersistingScopeObserver file) but is not picked up when backfilling exit-info events.
This could be resolved by reading the scope file first?

主要語言
Kotlin
星號
1.4k
分支
478
平均合併
2 天 20 小時
30 天內合併 PR
71

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

getsentry/sentry-java 的其他 Issue

查看 getsentry/sentry-java 的全部 Issue

相似的 Issue

更多 Kotlin Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。