Skip to content

Commit

Permalink
Use SENTRY_ENVIRONMENT over ASPNETCORE_ENVIRONMENT (#97)
Browse files Browse the repository at this point in the history
* fix: Look for SENTRY_ENV on ASP.NET Core
  • Loading branch information
bruno-garcia authored Sep 7, 2018
1 parent ad76da9 commit 6bd0f42
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
22 changes: 7 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,7 @@ void Main()
using (SentrySdk.Init(o =>
{
o.Dsn = new Dsn("dsn");
o.Http(h =>
{
h.Proxy = new WebProxy("https://localhost:3128");
});
o.Proxy = new WebProxy("https://localhost:3128");
}))
{
// App code
Expand Down Expand Up @@ -132,6 +129,7 @@ The SDK is configurable, many of the settings are demonstrated through the sampl
* Send PII data (Personal Identifiable Information, requires opt-in)
* Read [diagnostics activity data]("https://github.com/dotnet/corefx/blob/master/src/System.Diagnostics.DiagnosticSource/src/ActivityUserGuide.md)
* BeforeSend: Callback to modify/reject event before sending
* BeforeBreadcrumb: Callback to modify/reject a breadcrumb
* LogEventFilter: Filter events by inspecting log data
* Maximum number of breadcrumbs to store
* Event queue depth
Expand Down Expand Up @@ -182,7 +180,7 @@ An example using `IHub` for testability is [SentryLogger](https://github.com/get

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:

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

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

* .NET Framework 4.6.2 on Windows (AppVeyor)
* .NET Framework 4.7.2 on Windows
* Mono 5.12 macOS and Linux (Travis-CI)
* .NET Core 2.0 Windows (AppVeyor), macOS and Linux (Travis-CI)
* .NET Core 2.1 Windows (AppVeyor), macOS and Linux (Travis-CI)

* .NET Core 2.0 Windows, macOS and Linux
* .NET Core 2.1 Windows, macOS and Linux

### Legacy frameworks

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+.

## Get involved
Join the discussion in our
[tracking issue](https://github.com/getsentry/sentry-dotnet/issues/1) and let us
know what you think of the new API and new features.
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+.

## Resources
* [![Gitter chat](https://img.shields.io/gitter/room/getsentry/dotnet.svg)](https://gitter.im/getsentry/dotnet)
Expand Down
2 changes: 2 additions & 0 deletions src/Sentry.AspNetCore/SentryAspNetCoreOptionsSetup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.Extensions.Logging.Configuration;
using Microsoft.Extensions.Options;
using Sentry.Extensions.Logging;
using Sentry.Internal;

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

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

options.AddLogEntryFilter((category, level, eventId, exception)
Expand Down
7 changes: 5 additions & 2 deletions src/Sentry/Internal/EnvironmentLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,14 @@ namespace Sentry.Internal
{
internal static class EnvironmentLocator
{
private static readonly Lazy<string> Environment = new Lazy<string>(Locate);

/// <summary>
/// Attempts to locate the environment the app is running in
/// </summary>
/// <returns>The Environment name or null, if it couldn't be located.</returns>
public static string GetCurrent()
=> Environment.GetEnvironmentVariable(Constants.EnvironmentEnvironmentVariable);
public static string Current => Environment.Value;

internal static string Locate() => System.Environment.GetEnvironmentVariable(Constants.EnvironmentEnvironmentVariable);
}
}
4 changes: 1 addition & 3 deletions src/Sentry/Internal/MainSentryEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ namespace Sentry.Internal
internal class MainSentryEventProcessor : ISentryEventProcessor
{
private readonly Lazy<string> _release = new Lazy<string>(ReleaseLocator.GetCurrent);
private readonly Lazy<string> _environment = new Lazy<string>(EnvironmentLocator.GetCurrent);
private readonly Lazy<Runtime> _runtime = new Lazy<Runtime>(() =>
{
var current = PlatformAbstractions.Runtime.Current;
Expand All @@ -35,7 +34,6 @@ private static readonly (string Name, string Version) NameAndVersion
private readonly ISentryStackTraceFactory _sentryStackTraceFactory;

internal string Release => _release.Value;
internal string Environment => _environment.Value;
internal Runtime Runtime => _runtime.Value;

public MainSentryEventProcessor(
Expand Down Expand Up @@ -98,7 +96,7 @@ public SentryEvent Process(SentryEvent @event)

if (@event.Environment == null)
{
@event.Environment = _options.Environment ?? Environment;
@event.Environment = _options.Environment ?? EnvironmentLocator.Locate();
}

var stackTrace = _sentryStackTraceFactory.Create(@event.Exception);
Expand Down
22 changes: 18 additions & 4 deletions test/Sentry.Tests/Internals/EnvironmentLocatorTests.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using Sentry.Internal;
using Sentry.Testing;
using Xunit;
Expand All @@ -7,27 +8,40 @@ namespace Sentry.Tests.Internals
public class EnvironmentLocatorTests
{
[Fact]
public void GetCurrent_WithEnvironmentVariable_ReturnsEnvironmentVariableValue()
public void Locate_WithEnvironmentVariable_ReturnsEnvironmentVariableValue()
{
const string expectedVersion = "the environment name";
EnvironmentVariableGuard.WithVariable(
Constants.EnvironmentEnvironmentVariable,
expectedVersion,
() =>
{
Assert.Equal(expectedVersion, EnvironmentLocator.GetCurrent());
Assert.Equal(expectedVersion, EnvironmentLocator.Locate());
});
}

[Fact]
public void GetCurrent_WithoutEnvironmentVariable_ReturnsNull()
public void Locate_WithoutEnvironmentVariable_ReturnsNull()
{
EnvironmentVariableGuard.WithVariable(
Constants.ReleaseEnvironmentVariable,
null,
() =>
{
Assert.Null(EnvironmentLocator.GetCurrent());
Assert.Null(EnvironmentLocator.Locate());
});
}

[Fact]
public void Current_CachesValue()
{
var expected = EnvironmentLocator.Current;
EnvironmentVariableGuard.WithVariable(
Constants.ReleaseEnvironmentVariable,
Guid.NewGuid().ToString(),
() =>
{
Assert.Equal(expected, EnvironmentLocator.Current);
});
}
}
Expand Down

0 comments on commit 6bd0f42

Please sign in to comment.