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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

* Rename `LogEntry` to `SentryMessage`. Change type of `SentryEvent.Message` from `string` to `SentryMessage`.
* Change the type of `Gpu.VendorId` from `int` to `string`.
* Set the Environment setting to 'production' if none was provided. (#550) @PureKrome

# 3.0.0-alpha.0

Expand Down
6 changes: 6 additions & 0 deletions src/Sentry/Internal/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ internal static class Constants
/// </summary>
public const string EnvironmentEnvironmentVariable = "SENTRY_ENVIRONMENT";

/// <summary>
/// Default Sentry environment setting.
/// </summary>
/// <remarks>Best Sentry practice is to always try and have a value for this setting.</remarks>
public const string DefaultEnvironmentSetting = "Production";
PureKrome marked this conversation as resolved.
Show resolved Hide resolved

// See: https://github.com/getsentry/sentry-release-registry
public const string SdkName = "sentry.dotnet";
}
Expand Down
14 changes: 12 additions & 2 deletions src/Sentry/Internal/MainSentryEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,19 @@ public SentryEvent Process(SentryEvent @event)
@event.Release = _options.Release ?? Release;
}

if (@event.Environment == null)
if (string.IsNullOrWhiteSpace(@event.Environment))
{
@event.Environment = _options.Environment ?? EnvironmentLocator.Locate();
if (string.IsNullOrWhiteSpace(_options.Environment))
PureKrome marked this conversation as resolved.
Show resolved Hide resolved
{
var foundEnvironment = EnvironmentLocator.Locate();
@event.Environment = string.IsNullOrWhiteSpace(foundEnvironment)
? Constants.DefaultEnvironmentSetting
: foundEnvironment;
}
else
{
@event.Environment = _options.Environment;
}
}

if (@event.Exception == null)
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
30 changes: 20 additions & 10 deletions test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,35 +164,45 @@ public void Process_NoReleaseOnOptions_SameAsCachedVersion()
Assert.Equal(sut.Release, evt.Release);
}

[Fact]
public void Process_EnvironmentOnOptions_SetToEvent()
[Theory]
[InlineData(null, Constants.DefaultEnvironmentSetting)] // Missing: will get default value.
[InlineData("", Constants.DefaultEnvironmentSetting)] // Missing: will get default value.
[InlineData(" ", Constants.DefaultEnvironmentSetting)] // Missing: will get default value.
[InlineData("a", "a")] // Provided: nothing will change.
[InlineData("production", "production")] // Provided: nothing will change. (value has a lower case 'p', different to default value)
[InlineData("aBcDe F !@#$ gHi", "aBcDe F !@#$ gHi")] // Provided: nothing will change. (Case check)
public void Process_EnvironmentOnOptions_SetToEvent(string environment, string expectedEnvironment)
{
const string expected = "Production";
_fixture.SentryOptions.Environment = expected;
_fixture.SentryOptions.Environment = environment;
var sut = _fixture.GetSut();
var evt = new SentryEvent();

_ = sut.Process(evt);

Assert.Equal(expected, evt.Environment);
Assert.Equal(expectedEnvironment, evt.Environment);
}

[Fact]
public void Process_NoEnvironmentOnOptions_SameAsEnvironmentVariable()
[Theory]
[InlineData(null, Constants.DefaultEnvironmentSetting)] // Missing: will get default value.
[InlineData("", Constants.DefaultEnvironmentSetting)] // Missing: will get default value.
[InlineData(" ", Constants.DefaultEnvironmentSetting)] // Missing: will get default value.
[InlineData("a", "a")] // Provided: nothing will change.
[InlineData("production", "production")] // Provided: nothing will change. (value has a lower case 'p', different to default value)
PureKrome marked this conversation as resolved.
Show resolved Hide resolved
[InlineData("aBcDe F !@#$ gHi", "aBcDe F !@#$ gHi")] // Provided: nothing will change. (Case check)
public void Process_NoEnvironmentOnOptions_SameAsEnvironmentVariable(string environment, string expectedEnvironment)
{
const string expected = "Staging";
var sut = _fixture.GetSut();
var evt = new SentryEvent();

EnvironmentVariableGuard.WithVariable(
Constants.EnvironmentEnvironmentVariable,
expected,
environment,
() =>
{
_ = sut.Process(evt);
});

Assert.Equal(expected, evt.Environment);
Assert.Equal(expectedEnvironment, evt.Environment);
}

[Fact]
Expand Down