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

Include staging in the environment naming rules from HostEnvironment #1046

Merged
merged 4 commits into from
Jun 10, 2021
Merged
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

### Fixes

- Report lowercase staging environment for ASP.NET Core ([#1046](https://github.com/getsentry/sentry-unity/pull/1046))

## 3.5.0

### Features
Expand Down
14 changes: 0 additions & 14 deletions src/Sentry.AspNetCore/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,5 @@ internal static class Constants
{
// See: https://github.com/getsentry/sentry-release-registry
public const string SdkName = "sentry.dotnet.aspnetcore";

public static string ASPNETCoreProductionEnvironmentName =>
#if NETSTANDARD2_0
"Production";
#else
Microsoft.Extensions.Hosting.Environments.Production;
#endif

public static string ASPNETCoreDevelopmentEnvironmentName =>
#if NETSTANDARD2_0
"Development";
#else
Microsoft.Extensions.Hosting.Environments.Development;
#endif
}
}
12 changes: 9 additions & 3 deletions src/Sentry.AspNetCore/SentryAspNetCoreOptionsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
using Sentry.Extensions.Logging;
using Sentry.Internal;
#if NETSTANDARD2_0
using Microsoft.AspNetCore.Hosting;
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IHostingEnvironment;
#else
using Microsoft.Extensions.Hosting;
using IHostingEnvironment = Microsoft.AspNetCore.Hosting.IWebHostEnvironment;
#endif

Expand Down Expand Up @@ -36,7 +38,7 @@ public override void Configure(SentryAspNetCoreOptions options)
}
else
{
// NOTE: Sentry prefers to have it's environment setting to be all lower case.
// 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.
Expand All @@ -46,11 +48,15 @@ public override void Configure(SentryAspNetCoreOptions options)
// need to respect (especially the case-sensitivity).
// REF: https://docs.microsoft.com/en-us/aspnet/core/fundamentals/environments

if (_hostingEnvironment.EnvironmentName.Equals(Constants.ASPNETCoreProductionEnvironmentName))
if (_hostingEnvironment.IsProduction())
{
options.Environment = Internal.Constants.ProductionEnvironmentSetting;
}
else if (_hostingEnvironment.EnvironmentName.Equals(Constants.ASPNETCoreDevelopmentEnvironmentName))
else if (_hostingEnvironment.IsStaging())
{
options.Environment = Internal.Constants.StagingEnvironmentSetting;
}
else if (_hostingEnvironment.IsDevelopment())
{
options.Environment = Internal.Constants.DevelopmentEnvironmentSetting;
}
Expand Down
2 changes: 2 additions & 0 deletions src/Sentry/Internal/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ internal static class Constants
/// <remarks>Best Sentry practice is to always try and have a value for this setting.</remarks>
public const string ProductionEnvironmentSetting = "production";

public const string StagingEnvironmentSetting = "staging";

public const string DevelopmentEnvironmentSetting = "development";

public const string DebugEnvironmentSetting = "debug";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ 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, "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').
public void Filters_Environment_CustomOrASPNETEnvironment_Set(string environment, string hostingEnvironmentSetting, string expectedEnvironment)
{
// Arrange.
Expand Down