Skip to content

Commit 6bd0f42

Browse files
authored
Use SENTRY_ENVIRONMENT over ASPNETCORE_ENVIRONMENT (#97)
* fix: Look for SENTRY_ENV on ASP.NET Core
1 parent ad76da9 commit 6bd0f42

File tree

5 files changed

+33
-24
lines changed

5 files changed

+33
-24
lines changed

README.md

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,7 @@ void Main()
6464
using (SentrySdk.Init(o =>
6565
{
6666
o.Dsn = new Dsn("dsn");
67-
o.Http(h =>
68-
{
69-
h.Proxy = new WebProxy("https://localhost:3128");
70-
});
67+
o.Proxy = new WebProxy("https://localhost:3128");
7168
}))
7269
{
7370
// App code
@@ -132,6 +129,7 @@ The SDK is configurable, many of the settings are demonstrated through the sampl
132129
* Send PII data (Personal Identifiable Information, requires opt-in)
133130
* Read [diagnostics activity data]("https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/ActivityUserGuide.md)
134131
* BeforeSend: Callback to modify/reject event before sending
132+
* BeforeBreadcrumb: Callback to modify/reject a breadcrumb
135133
* LogEventFilter: Filter events by inspecting log data
136134
* Maximum number of breadcrumbs to store
137135
* Event queue depth
@@ -182,7 +180,7 @@ An example using `IHub` for testability is [SentryLogger](https://github.com/get
182180

183181
The packages target **.NET Standard 2.0**. That means [it is compatible with](https://docs.microsoft.com/en-us/dotnet/standard/net-standard) the following versions or newer:
184182

185-
* .NET Framework 4.6.1
183+
* .NET Framework 4.6.1 (4.7.2 advised)
186184
* .NET Core 2.0
187185
* Mono 5.4
188186
* Xamarin.Android 8.0
@@ -192,20 +190,14 @@ The packages target **.NET Standard 2.0**. That means [it is compatible with](ht
192190

193191
Of those, we've tested (we run our unit/integration tests) against:
194192

195-
* .NET Framework 4.6.2 on Windows (AppVeyor)
193+
* .NET Framework 4.7.2 on Windows
196194
* Mono 5.12 macOS and Linux (Travis-CI)
197-
* .NET Core 2.0 Windows (AppVeyor), macOS and Linux (Travis-CI)
198-
* .NET Core 2.1 Windows (AppVeyor), macOS and Linux (Travis-CI)
199-
195+
* .NET Core 2.0 Windows, macOS and Linux
196+
* .NET Core 2.1 Windows, macOS and Linux
200197

201198
### Legacy frameworks
202199

203-
Sentry's [Raven SDK](https://github.com/getsentry/raven-csharp/), battle tested with over 300.000 downloads on NuGet has support to .NET Framework 3.5+.
204-
205-
## Get involved
206-
Join the discussion in our
207-
[tracking issue](https://github.com/getsentry/sentry-dotnet/issues/1) and let us
208-
know what you think of the new API and new features.
200+
Sentry's [Raven SDK](https://github.com/getsentry/raven-csharp/), battle tested with over 400.000 downloads on NuGet has support to .NET Framework 3.5+.
209201

210202
## Resources
211203
* [![Gitter chat](https://img.shields.io/gitter/room/getsentry/dotnet.svg)](https://gitter.im/getsentry/dotnet)

src/Sentry.AspNetCore/SentryAspNetCoreOptionsSetup.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using Microsoft.Extensions.Logging.Configuration;
44
using Microsoft.Extensions.Options;
55
using Sentry.Extensions.Logging;
6+
using Sentry.Internal;
67

78
namespace Sentry.AspNetCore
89
{
@@ -22,6 +23,7 @@ public override void Configure(SentryAspNetCoreOptions options)
2223

2324
options.Environment
2425
= options.Environment // Don't override user defined value
26+
?? EnvironmentLocator.Locate() // Sentry specific environment takes precedence #92
2527
?? _hostingEnvironment?.EnvironmentName;
2628

2729
options.AddLogEntryFilter((category, level, eventId, exception)

src/Sentry/Internal/EnvironmentLocator.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ namespace Sentry.Internal
44
{
55
internal static class EnvironmentLocator
66
{
7+
private static readonly Lazy<string> Environment = new Lazy<string>(Locate);
8+
79
/// <summary>
810
/// Attempts to locate the environment the app is running in
911
/// </summary>
1012
/// <returns>The Environment name or null, if it couldn't be located.</returns>
11-
public static string GetCurrent()
12-
=> Environment.GetEnvironmentVariable(Constants.EnvironmentEnvironmentVariable);
13+
public static string Current => Environment.Value;
14+
15+
internal static string Locate() => System.Environment.GetEnvironmentVariable(Constants.EnvironmentEnvironmentVariable);
1316
}
1417
}

src/Sentry/Internal/MainSentryEventProcessor.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ namespace Sentry.Internal
1414
internal class MainSentryEventProcessor : ISentryEventProcessor
1515
{
1616
private readonly Lazy<string> _release = new Lazy<string>(ReleaseLocator.GetCurrent);
17-
private readonly Lazy<string> _environment = new Lazy<string>(EnvironmentLocator.GetCurrent);
1817
private readonly Lazy<Runtime> _runtime = new Lazy<Runtime>(() =>
1918
{
2019
var current = PlatformAbstractions.Runtime.Current;
@@ -35,7 +34,6 @@ private static readonly (string Name, string Version) NameAndVersion
3534
private readonly ISentryStackTraceFactory _sentryStackTraceFactory;
3635

3736
internal string Release => _release.Value;
38-
internal string Environment => _environment.Value;
3937
internal Runtime Runtime => _runtime.Value;
4038

4139
public MainSentryEventProcessor(
@@ -98,7 +96,7 @@ public SentryEvent Process(SentryEvent @event)
9896

9997
if (@event.Environment == null)
10098
{
101-
@event.Environment = _options.Environment ?? Environment;
99+
@event.Environment = _options.Environment ?? EnvironmentLocator.Locate();
102100
}
103101

104102
var stackTrace = _sentryStackTraceFactory.Create(@event.Exception);

test/Sentry.Tests/Internals/EnvironmentLocatorTests.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using Sentry.Internal;
23
using Sentry.Testing;
34
using Xunit;
@@ -7,27 +8,40 @@ namespace Sentry.Tests.Internals
78
public class EnvironmentLocatorTests
89
{
910
[Fact]
10-
public void GetCurrent_WithEnvironmentVariable_ReturnsEnvironmentVariableValue()
11+
public void Locate_WithEnvironmentVariable_ReturnsEnvironmentVariableValue()
1112
{
1213
const string expectedVersion = "the environment name";
1314
EnvironmentVariableGuard.WithVariable(
1415
Constants.EnvironmentEnvironmentVariable,
1516
expectedVersion,
1617
() =>
1718
{
18-
Assert.Equal(expectedVersion, EnvironmentLocator.GetCurrent());
19+
Assert.Equal(expectedVersion, EnvironmentLocator.Locate());
1920
});
2021
}
2122

2223
[Fact]
23-
public void GetCurrent_WithoutEnvironmentVariable_ReturnsNull()
24+
public void Locate_WithoutEnvironmentVariable_ReturnsNull()
2425
{
2526
EnvironmentVariableGuard.WithVariable(
2627
Constants.ReleaseEnvironmentVariable,
2728
null,
2829
() =>
2930
{
30-
Assert.Null(EnvironmentLocator.GetCurrent());
31+
Assert.Null(EnvironmentLocator.Locate());
32+
});
33+
}
34+
35+
[Fact]
36+
public void Current_CachesValue()
37+
{
38+
var expected = EnvironmentLocator.Current;
39+
EnvironmentVariableGuard.WithVariable(
40+
Constants.ReleaseEnvironmentVariable,
41+
Guid.NewGuid().ToString(),
42+
() =>
43+
{
44+
Assert.Equal(expected, EnvironmentLocator.Current);
3145
});
3246
}
3347
}

0 commit comments

Comments
 (0)