Skip to content

feat(android): sync Scope.Environment to the Java SDK - #6036

Merged
ric-oliv merged 3 commits into
mainfrom
ai/triagent-5387
Dec 1, 2026
Merged

ric-oliv merged 3 commits into
mainfrom
ai/triagent-5387

Conversation

@jamescrosswell

Copy link
Copy Markdown
Collaborator

Summary

AndroidScopeObserver.SetEnvironment was a no-op because sentry-java had no scope-level environment. sentry-java 8.63.0 added Scope.setEnvironment (getsentry/sentry-java#5772), so the observer now forwards the environment through Sentry.configureScope, matching what the Cocoa and native observers do since #5365.

The Init half of the issue was already in place: SentrySdk.InitSentryAndroidSdk sets o.Environment = defaults.Environment.

Notes

  • The concrete io.sentry.Scope binding doesn't implement IScope in C# (it lost members to the ISentryClient removals in Metadata.xml), so the test JavaCasts it to IScope.
  • The test covers the callback, not the observer end to end against an initialised Java SDK. The Android device suite passes locally on an emulator.

Closes #5387

🤖 Generated with Claude Code

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>
@github-actions github-actions Bot added the risk: medium PR risk score: medium label Nov 26, 2026
@github-actions

github-actions Bot commented Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

PR risk: medium

For commit b7ac449.

Main concerns: reach, host performance, data sent to Sentry.

This label is a hint for reviewers, not an approval.

@sentry-junior sentry-junior Bot left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 to IScope, ScopeCallbackTests is gone, and AndroidScopeObserverTests now covers the observer end to end. Worth updating so reviewers aren't misled.

This is an automated preliminary review.

@codecov

codecov Bot commented Nov 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 78.48%. Comparing base (5b87764) to head (b7ac449).
⚠️ Report is 14 commits behind head on main.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 26, 2026
@jamescrosswell
jamescrosswell marked this pull request as ready for review November 26, 2026 22:14
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 26, 2026
Comment on lines +8 to +19
[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");
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();
        }
    }
}
#endif

It 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>
@github-actions github-actions Bot added risk: medium PR risk score: medium and removed risk: medium PR risk score: medium labels Nov 30, 2026

@ric-oliv ric-oliv left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@ric-oliv
ric-oliv merged commit 82268e7 into main Dec 1, 2026
60 checks passed
@ric-oliv
ric-oliv deleted the ai/triagent-5387 branch December 1, 2026 14:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

risk: medium PR risk score: medium

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Sync Environment via IScopeObserver in Hybrid SDK scenarios (sentry-java)

2 participants