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

Added SentryAspNetCoreOptions.AdjustStandardEnvironmentNameCasing to … #1057

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
### Changes

- Use SentryId for ISession.Id ([#1052](https://github.com/getsentry/sentry-dotnet/pull/1052))
- Added SentryAspNetCoreOptions.AdjustStandardEnvironmentNameCasing to control whether to transform the environment name to lower case. [#1057](https://github.com/getsentry/sentry-dotnet/pull/1057)
- Improve exception check in `CaptureEvent(...)` for the purpose of reporting errors in session ([#1058](https://github.com/getsentry/sentry-dotnet/pull/1058))

## 3.6.0-alpha.1
Expand Down
10 changes: 10 additions & 0 deletions src/Sentry.AspNetCore/SentryAspNetCoreOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ public class SentryAspNetCoreOptions : SentryLoggingOptions
/// </summary>
public TimeSpan FlushTimeout { get; set; } = TimeSpan.FromSeconds(2);

/// <summary>
/// Controls whether the casing of the standard (Production, Development and Staging) environment name supplied by <see cref="Microsoft.AspNetCore.Hosting.IHostingEnvironment" />
/// is adjusted when setting the Sentry environment. Defaults to true.
/// </summary>
/// <remarks>
/// The default .NET Core environment names include Production, Development and Staging (note Pascal casing), whereas Sentry prefers
/// to have its environment setting be all lower case.
/// </remarks>
public bool AdjustStandardEnvironmentNameCasing { get; set; } = true;

/// <summary>
/// Creates a new instance of <see cref="SentryAspNetCoreOptions"/>.
/// </summary>
Expand Down
13 changes: 9 additions & 4 deletions src/Sentry.AspNetCore/SentryAspNetCoreOptionsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ public override void Configure(SentryAspNetCoreOptions options)
// Sentry specific environment takes precedence #92.
options.Environment = locatedEnvironment;
}
else
else if (options.AdjustStandardEnvironmentNameCasing)
{
// NOTE: Sentry prefers to have its environment setting to be all lower case.
// .NET Core sets the ENV variable to 'Production' (upper case P) or
// 'Development' (upper case D) which conflicts with the Sentry recommendation.
// As such, we'll be kind and override those values, here ... if applicable.
// .NET Core sets the ENV variable to 'Production' (upper case P),
// 'Development' (upper case D) or 'Staging' (upper case S) which conflicts with
// the Sentry recommendation. As such, we'll be kind and override those values,
// here ... if applicable.
// Assumption: The Hosting Environment is always set.
// If not set by a developer, then the framework will auto set it.
// Alternatively, developers might set this to a CUSTOM value, which we
Expand All @@ -66,6 +67,10 @@ public override void Configure(SentryAspNetCoreOptions options)
options.Environment = _hostingEnvironment.EnvironmentName;
}
}
else
{
options.Environment = _hostingEnvironment.EnvironmentName;
}
}

options.AddLogEntryFilter((category, _, eventId, _)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ public void Filters_KestrelEventId1_WithException_NotFiltered()
[InlineData("", "Production", "production")] // Custom - nothing set. ASPNET_ENVIRONMENT is default PROD.
[InlineData(null, "Development", "development")] // Custom - nothing set. ASPNET_ENVIRONMENT is default DEV.
[InlineData("", "Development", "development")] // Custom - nothing set. ASPNET_ENVIRONMENT is default DEV.
[InlineData(null, "Staging", "staging")] // Custom - nothing set. ASPNET_ENVIRONMENT is default DEV.
[InlineData("", "Staging", "staging")] // Custom - nothing set. ASPNET_ENVIRONMENT is default DEV.
[InlineData(null, "Staging", "staging")] // Custom - nothing set. ASPNET_ENVIRONMENT is default STAGING.
[InlineData("", "Staging", "staging")] // Custom - nothing set. ASPNET_ENVIRONMENT is default STAGING.
[InlineData(null, "production", "production")] // Custom - nothing set. ASPNET_ENVIRONMENT is custom (notice lowercase 'p').
[InlineData(null, "development", "development")] // Custom - nothing set. ASPNET_ENVIRONMENT is custom (notice lowercase 'd').
[InlineData(null, "staging", "staging")] // Custom - nothing set. ASPNET_ENVIRONMENT is custom (notice lowercase 's').
Expand All @@ -73,6 +73,35 @@ public void Filters_Environment_CustomOrASPNETEnvironment_Set(string environment
Assert.Equal(expectedEnvironment, _target.Environment);
}

[Theory]
[InlineData("Foo", "Production", true, "Foo")] // Custom - set. Don't modify.
[InlineData(null, "Foo", true, "Foo")] // Custom - nothing set. ASPNET_ENVIRONMENT is custom. Don't modify.
[InlineData(null, "Production", true, "production")] // Custom - nothing set. Adjust standard casing - true. ASPNET_ENVIRONMENT is default PROD.
[InlineData(null, "Development", true, "development")] // Custom - nothing set. Adjust standard casing - true. ASPNET_ENVIRONMENT is default DEV.
[InlineData(null, "Staging", true, "staging")] // Custom - nothing set. Adjust standard casing - true. ASPNET_ENVIRONMENT is default STAGING.
[InlineData(null, "Production", false, "Production")] // Custom - nothing set. Adjust standard casing - false. ASPNET_ENVIRONMENT is default PROD.
[InlineData(null, "Development", false, "Development")] // Custom - nothing set. Adjust standard casing - false. ASPNET_ENVIRONMENT is default DEV.
[InlineData(null, "Staging", false, "Staging")] // Custom - nothing set. Adjust standard casing - false. ASPNET_ENVIRONMENT is default STAGING.
public void Filters_Environment_AdjustStandardEnvironmentNameCasing_AffectsSentryEnvironment(string environment, string hostingEnvironmentSetting, bool adjustStandardEnvironmentNameCasingSetting, string expectedEnvironment)
{
// Arrange.
var hostingEnvironment = Substitute.For<IHostingEnvironment>();
hostingEnvironment.EnvironmentName = hostingEnvironmentSetting;

var sut = new SentryAspNetCoreOptionsSetup(
Substitute.For<ILoggerProviderConfiguration<SentryAspNetCoreLoggerProvider>>(),
hostingEnvironment);

_target.Environment = environment;
_target.AdjustStandardEnvironmentNameCasing = adjustStandardEnvironmentNameCasingSetting;

// Act.
sut.Configure(_target);

// Assert.
Assert.Equal(expectedEnvironment, _target.Environment);
}

[Theory]
[InlineData("foo")] // Random setting.
[InlineData("Production")] // Custom setting which is the same as ASPNET_ENVIRONMENT. But because this is manually set, don't change it.
Expand Down