Repository navigation
feat(android): sync Scope.Environment to the Java SDK - #6036
Conversation
AndroidScopeObserver.SetEnvironment now forwards the environment to the Java scope via Sentry.configureScope, using the Scope.setEnvironment API added in sentry-java 8.63.0. Co-Authored-By: Claude <noreply@anthropic.com>
PR risk: mediumFor commit Main concerns: reach, host performance, data sent to Sentry. This label is a hint for reviewers, not an approval. |
There was a problem hiding this comment.
Good to approve, with optional comments
Forwards Scope.Environment to sentry-java through Sentry.configureScope, and together with the existing o.Environment = defaults.Environment in SentrySdk.InitSentryAndroidSdk it covers both halves of #5387. It fixes the cause, writes to the same default Java scope as the existing SetTag/SetUser calls, and the new device test exercises the observer against an initialised Java SDK, so it fails if SetEnvironment goes back to a no-op.
- Optional: the PR description's Notes are out of date after the latest commits. The test no longer
JavaCasts toIScope,ScopeCallbackTestsis gone, andAndroidScopeObserverTestsnow covers the observer end to end. Worth updating so reviewers aren't misled.
This is an automated preliminary review.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #6036 +/- ##
==========================================
+ Coverage 78.33% 78.48% +0.15%
==========================================
Files 537 537
Lines 20134 20167 +33
Branches 4055 4056 +1
==========================================
+ Hits 15771 15828 +57
+ Misses 3516 3491 -25
- Partials 847 848 +1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| [Fact] | ||
| public void Run_SetsEnvironment_UpdatesJavaScope() | ||
| { | ||
| using var javaOptions = new JavaSdk.SentryOptions(); | ||
| using var javaScope = new JavaSdk.Scope(javaOptions); | ||
| var scope = global::Android.Runtime.Extensions.JavaCast<JavaSdk.IScope>(javaScope); | ||
| using var callback = new ScopeCallback(s => s.Environment = "staging"); | ||
|
|
||
| callback.Run(scope); | ||
|
|
||
| scope.Environment.Should().Be("staging"); | ||
| } |
There was a problem hiding this comment.
What do you think about testing the observer here instead of ScopeCallback? This test still passes if SetEnvironment goes back to being a no-op, or if it forwards the wrong value, because it only checks that Run calls its delegate. Something like this fails in both cases:
#if ANDROID
using Sentry.Android;
using Sentry.Android.Callbacks;
namespace Sentry.Tests.Platforms.Android;
[Collection(nameof(SentrySdkCollection))]
public class AndroidScopeObserverTests
{
[Fact]
public void SetEnvironment_JavaSdkEnabled_SetsJavaScopeEnvironment()
{
using var javaOptions = new JavaSdk.Android.Core.SentryAndroidOptions { Dsn = ValidDsn };
JavaSdk.Sentry.Init(javaOptions);
try
{
var sut = new AndroidScopeObserver(new SentryOptions());
sut.SetEnvironment("staging");
string environment = null;
using var callback = new ScopeCallback(scope => environment = scope.Environment);
JavaSdk.Sentry.ConfigureScope(JavaSdk.ScopeType.Combined, callback);
environment.Should().Be("staging");
}
finally
{
JavaSdk.Sentry.Close();
}
}
}
#endifIt needs SentryAndroidOptions because Sentry.init throws on Android for a plain SentryOptions. Reading through the combined scope checks what events get, not which scope the write lands in. With this in place, ScopeCallbackTests could go.
…Java SDK Replaces ScopeCallbackTests, which only proved the callback ran its delegate. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Summary
AndroidScopeObserver.SetEnvironmentwas a no-op because sentry-java had no scope-level environment. sentry-java 8.63.0 addedScope.setEnvironment(getsentry/sentry-java#5772), so the observer now forwards the environment throughSentry.configureScope, matching what the Cocoa and native observers do since #5365.The Init half of the issue was already in place:
SentrySdk.InitSentryAndroidSdksetso.Environment = defaults.Environment.Notes
io.sentry.Scopebinding doesn't implementIScopein C# (it lost members to theISentryClientremovals inMetadata.xml), so the testJavaCasts it toIScope.Closes #5387
🤖 Generated with Claude Code