diff --git a/CHANGELOG.md b/CHANGELOG.md index 5cf491f3bf..b8572a4359 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ * Rename `LogEntry` to `SentryMessage`. Change type of `SentryEvent.Message` from `string` to `SentryMessage`. * Change the type of `Gpu.VendorId` from `int` to `string`. * Add support for envelopes. +* Set the Environment setting to 'production' if none was provided. (#550) @PureKrome # 3.0.0-alpha.0 diff --git a/src/Sentry/Internal/Constants.cs b/src/Sentry/Internal/Constants.cs index b183464d3b..cc3296abb8 100644 --- a/src/Sentry/Internal/Constants.cs +++ b/src/Sentry/Internal/Constants.cs @@ -20,6 +20,12 @@ internal static class Constants /// public const string EnvironmentEnvironmentVariable = "SENTRY_ENVIRONMENT"; + /// + /// Default Sentry environment setting. + /// + /// Best Sentry practice is to always try and have a value for this setting. + public const string DefaultEnvironmentSetting = "production"; + // See: https://github.com/getsentry/sentry-release-registry public const string SdkName = "sentry.dotnet"; } diff --git a/src/Sentry/Internal/MainSentryEventProcessor.cs b/src/Sentry/Internal/MainSentryEventProcessor.cs index 170557953f..536e8fb9db 100644 --- a/src/Sentry/Internal/MainSentryEventProcessor.cs +++ b/src/Sentry/Internal/MainSentryEventProcessor.cs @@ -136,9 +136,16 @@ public SentryEvent Process(SentryEvent @event) @event.Release = _options.Release ?? Release; } - if (@event.Environment == null) - { - @event.Environment = _options.Environment ?? EnvironmentLocator.Locate(); + // Recommendation: The 'Environment' setting should always be set + // with a default fallback. + if (string.IsNullOrWhiteSpace(@event.Environment)) + { + var foundEnvironment = EnvironmentLocator.Locate(); + @event.Environment = string.IsNullOrWhiteSpace(foundEnvironment) + ? string.IsNullOrWhiteSpace(_options.Environment) + ? Constants.DefaultEnvironmentSetting + : _options.Environment + : foundEnvironment; } if (@event.Exception == null) diff --git a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs index 5ae28b7d24..b24faf14f7 100644 --- a/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs +++ b/test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs @@ -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 upper case 'p', different to default value) + [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]