Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Default Environment setting if none provided. #550

Merged
9 changes: 9 additions & 0 deletions src/Sentry/SentryClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,15 @@ private SentryId DoSendEvent(SentryEvent @event, Scope? scope)
}
}

// Each event should have an 'Environment' set. If none was set, then should default
// to 'production'.
if (string.IsNullOrWhiteSpace(@event.Environment))
{
@event.Environment = string.IsNullOrWhiteSpace(_options.Environment)
? "production"
: _options.Environment;
PureKrome marked this conversation as resolved.
Show resolved Hide resolved
}

SentryEvent? processedEvent = @event;

foreach (var processor in scope.GetAllEventProcessors())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ public void Environment_NotOnOptions_ValueFromEnvVar()
});
}

// NOTE: Options take priority over EnvVar's.
PureKrome marked this conversation as resolved.
Show resolved Hide resolved
[Fact]
public void Environment_BothOnOptionsAndEnvVar_ValueFromOption()
{
Expand Down
41 changes: 41 additions & 0 deletions test/Sentry.Tests/SentryClientTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,47 @@ public void CaptureEvent_DisposedClient_ThrowsObjectDisposedException()
_ = Assert.Throws<ObjectDisposedException>(() => sut.CaptureEvent(null));
}

[Theory]
[InlineData(null, "production")] // Missing: will get default value.
[InlineData("", "production")] // Missing: will get default value.
[InlineData(" ", "production")] // Missing: will get default value.
[InlineData("a", "a")] // Provided: nothing will change.
[InlineData("production", "production")] // Provided: nothing will change. (value happens to be the default)
[InlineData("aBcDe F !@#$ gHi", "aBcDe F !@#$ gHi")] // Provided: nothing will change. (Case check)
public void CaptureEvent_Environment_HasCorrectEnvironmentValue(string environment, string expectedEnvironment)
{
//_fixture.SentryOptions.Environment = environment;

var @event = new SentryEvent
{
Environment = environment
};

var sut = _fixture.GetSut();
_ = sut.CaptureEvent(@event);

Assert.Equal(expectedEnvironment, @event.Environment);
}

[Theory]
[InlineData(null, "production")] // Missing: will get default value.
[InlineData("", "production")] // Missing: will get default value.
[InlineData(" ", "production")] // Missing: will get default value.
[InlineData("a", "a")] // Provided: nothing will change.
[InlineData("production", "production")] // Provided: nothing will change. (value happens to be the default)
[InlineData("aBcDe F !@#$ gHi", "aBcDe F !@#$ gHi")] // Provided: nothing will change. (Case check)
public void CaptureEvent_OptionsEnvironment_HasCorrectEnvironmentValue(string environment, string expectedEnvironment)
{
_fixture.SentryOptions.Environment = environment;

var @event = new SentryEvent();

var sut = _fixture.GetSut();
_ = sut.CaptureEvent(@event);

Assert.Equal(expectedEnvironment, @event.Environment);
}

[Fact]
public void Dispose_Worker_DisposeCalled()
{
Expand Down