Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 24 additions & 12 deletions src/Sentry/Scope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,30 +144,32 @@ public SentryUser User
/// <inheritdoc />
public string? Distribution { get; set; }

private string? _environment;

/// <inheritdoc />
public string? Environment
/// <remarks>Setting this to <c>null</c> reverts to the environment resolved from the options.</remarks>
[AllowNull]
public string Environment
{
get;
get => _environment ?? Options.SettingLocator.GetEnvironment();
set
{
if (field == value)
{
return;
}

if (value is null)
{
Options.LogDebug("Environment cannot be null. Reverting to default value from the options.");
field = Options.Environment;
value = Options.SettingLocator.GetEnvironment();
}
else

if (_environment == value)
{
field = value;
return;
}

_environment = value;

if (Options is { EnableScopeSync: true, ScopeObserver: { } observer })
{
observer.SetEnvironment(field);
observer.SetEnvironment(value);
}
}
}
Expand Down Expand Up @@ -515,7 +517,17 @@ public void Apply(IEventLike other)

other.Release ??= Release;
other.Distribution ??= Distribution;
other.Environment ??= Environment;
if (other is Scope otherScope)
{
if (otherScope._environment is null && _environment is not null)
{
otherScope.Environment = _environment;
}
}
else
{
other.Environment ??= Environment;
}
other.TransactionName ??= TransactionName;
other.Level ??= Level;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
QueryString:
},
User: {},
Environment: production,
TransactionName: GET Ctrl.Actn,
Tags: {
route.action: Actn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ namespace Sentry
public System.Collections.Generic.IReadOnlyCollection<Sentry.Breadcrumb> Breadcrumbs { get; }
public Sentry.SentryContexts Contexts { get; set; }
public string? Distribution { get; set; }
public string? Environment { get; set; }
public string Environment { get; set; }
public System.Collections.Generic.IReadOnlyDictionary<string, object?> Extra { get; }
public System.Collections.Generic.IReadOnlyList<string> Fingerprint { get; set; }
public Sentry.SentryLevel? Level { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ namespace Sentry
public System.Collections.Generic.IReadOnlyCollection<Sentry.Breadcrumb> Breadcrumbs { get; }
public Sentry.SentryContexts Contexts { get; set; }
public string? Distribution { get; set; }
public string? Environment { get; set; }
public string Environment { get; set; }
public System.Collections.Generic.IReadOnlyDictionary<string, object?> Extra { get; }
public System.Collections.Generic.IReadOnlyList<string> Fingerprint { get; set; }
public Sentry.SentryLevel? Level { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ namespace Sentry
public System.Collections.Generic.IReadOnlyCollection<Sentry.Breadcrumb> Breadcrumbs { get; }
public Sentry.SentryContexts Contexts { get; set; }
public string? Distribution { get; set; }
public string? Environment { get; set; }
public string Environment { get; set; }
public System.Collections.Generic.IReadOnlyDictionary<string, object?> Extra { get; }
public System.Collections.Generic.IReadOnlyList<string> Fingerprint { get; set; }
public Sentry.SentryLevel? Level { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ namespace Sentry
public System.Collections.Generic.IReadOnlyCollection<Sentry.Breadcrumb> Breadcrumbs { get; }
public Sentry.SentryContexts Contexts { get; set; }
public string? Distribution { get; set; }
public string? Environment { get; set; }
public string Environment { get; set; }
public System.Collections.Generic.IReadOnlyDictionary<string, object?> Extra { get; }
public System.Collections.Generic.IReadOnlyList<string> Fingerprint { get; set; }
public Sentry.SentryLevel? Level { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,7 @@ namespace Sentry
public System.Collections.Generic.IReadOnlyCollection<Sentry.Breadcrumb> Breadcrumbs { get; }
public Sentry.SentryContexts Contexts { get; set; }
public string? Distribution { get; set; }
public string? Environment { get; set; }
public string Environment { get; set; }
public System.Collections.Generic.IReadOnlyDictionary<string, object?> Extra { get; }
public System.Collections.Generic.IReadOnlyList<string> Fingerprint { get; set; }
public Sentry.SentryLevel? Level { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions test/Sentry.Tests/Protocol/ScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -958,15 +958,15 @@ public void Apply_Sdk_SourceNone_TargetSingle_DoesNotModifyTarget()
}

[Fact]
public void Apply_Environment_Null()
public void Apply_Environment_Null_TargetUsesOptionsEnvironment()
{
var sut = _fixture.GetSut();
sut.Environment = null;

var target = _fixture.GetSut();
sut.Apply(target);

Assert.Null(target.Environment);
Assert.Equal(target.Options.SettingLocator.GetEnvironment(), target.Environment);
}

[Fact]
Expand Down
39 changes: 39 additions & 0 deletions test/Sentry.Tests/ScopeTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -770,6 +770,45 @@ public void SetEnvironment_Null_ObserverReceivesOptionEnvironment()
observer.Received(1).SetEnvironment(Arg.Is(optionsEnvironment));
}

[Fact]
public void Environment_NotSet_ReturnsOptionsEnvironment()
{
var scope = new Scope(new SentryOptions { Environment = "staging" });

scope.Environment.Should().Be("staging");
}

[Fact]
public void Environment_NotSetInOptions_ReturnsDefaultEnvironment()
{
var scope = new Scope(new SentryOptions());

scope.Environment.Should().NotBeNullOrWhiteSpace();
}

[Fact]
public void Apply_EnvironmentNotSetOnScope_EventGetsOptionsEnvironment()
{
var scope = new Scope(new SentryOptions { Environment = "staging" });
var evt = new SentryEvent();

scope.Apply(evt);

evt.Environment.Should().Be("staging");
}

[Fact]
public void Apply_EnvironmentSetOnTarget_TargetEnvironmentPreserved()
{
var options = new SentryOptions { Environment = "production" };
var source = new Scope(options) { Environment = "staging" };
var target = new Scope(options) { Environment = "development" };

source.Apply(target);

target.Environment.Should().Be("development");
}

[Fact]
public void SetEnvironment_SameValue_ObserverNotifiedOnce()
{
Expand Down
Loading