Skip to content

Commit

Permalink
Merge pull request #49 from getsentry/feat/environment-options
Browse files Browse the repository at this point in the history
Add Environment to SentryOptions
  • Loading branch information
bruno-garcia authored Jul 18, 2018
2 parents 5c819fb + 607de0b commit b770762
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 16 deletions.
1 change: 1 addition & 0 deletions src/Sentry/Internal/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ internal static class Constants
{
public const string DsnEnvironmentVariable = "SENTRY_DSN";
public const string ReleaseEnvironmentVariable = "SENTRY_RELEASE";
public const string EnvironmentEnvironmentVariable = "SENTRY_ENVIRONMENT";

// https://docs.sentry.io/clientdev/overview/#usage-for-end-users
public const string DisableSdkDsnValue = "";
Expand Down
14 changes: 14 additions & 0 deletions src/Sentry/Internal/EnvironmentLocator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System;

namespace Sentry.Internal
{
internal static class EnvironmentLocator
{
/// <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);
}
}
35 changes: 22 additions & 13 deletions src/Sentry/Internal/MainSentryEventProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,29 @@ namespace Sentry.Internal
{
internal class MainSentryEventProcessor : ISentryEventProcessor
{
internal static readonly Lazy<string> Release = new Lazy<string>(ReleaseLocator.GetCurrent);
internal static readonly Lazy<Runtime> CurrentRuntime = new Lazy<Runtime>(() =>
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;
return current != null
? new Runtime
{
Name = current.Name,
Version = current.Version,
RawDescription = current.Raw
}
: null;
var current = PlatformAbstractions.Runtime.Current;
return current != null
? new Runtime
{
Name = current.Name,
Version = current.Version,
RawDescription = current.Raw
}
: null;
});

private static readonly (string Name, string Version) NameAndVersion
= typeof(ISentryClient).Assembly.GetNameAndVersion();

private readonly SentryOptions _options;

internal string Release => _release.Value;
internal string Environment => _environment.Value;
internal Runtime Runtime => _runtime.Value;
public MainSentryEventProcessor(SentryOptions options)
{
Debug.Assert(options != null);
Expand All @@ -41,7 +45,7 @@ public SentryEvent Process(SentryEvent @event)
{
if (!@event.Contexts.ContainsKey(Runtime.Type))
{
@event.Contexts[Runtime.Type] = CurrentRuntime.Value;
@event.Contexts[Runtime.Type] = Runtime;
}

if (!@event.Contexts.ContainsKey(OperatingSystem.Type))
Expand Down Expand Up @@ -70,7 +74,12 @@ public SentryEvent Process(SentryEvent @event)

if (@event.Release == null)
{
@event.Release = _options.Release ?? Release.Value;
@event.Release = _options.Release ?? Release;
}

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

if (@event.Exception != null)
Expand Down
2 changes: 1 addition & 1 deletion src/Sentry/Internal/ReleaseLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ namespace Sentry.Internal
internal static class ReleaseLocator
{
/// <summary>
/// Attemps to locate the application release
/// Attempts to locate the application release
/// </summary>
/// <returns>The app release or null, if it couldn't be located.</returns>
public static string GetCurrent()
Expand Down
21 changes: 20 additions & 1 deletion src/Sentry/SentryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,34 @@ public class SentryOptions : IScopeOptions
/// </summary>
/// <example>
/// 721e41770371db95eee98ca2707686226b993eda
/// 14.1.16.32451
/// </example>
/// <remarks>
/// This value will generally be something along the lines of the git SHA for the given project.
/// If not explicitly defined via configuration. It will attempt o read it from:
/// If not explicitly defined via configuration or environment variable (SENTRY_RELEASE).
/// It will attempt o read it from:
/// <see cref="System.Reflection.AssemblyInformationalVersionAttribute"/>
///
/// Don't rely on discovery if your release is: '1.0.0' or '0.0.0'. Since those are
/// default values for new projects, they are not considered valid by the discovery process.
/// </remarks>
/// <seealso href="https://docs.sentry.io/learn/releases/"/>
public string Release { get; set; }

/// <summary>
/// The environment the application is running
/// </summary>
/// <remarks>
/// This value can also be set via environment variable: SENTRY_ENVIRONMENT
/// In some cases you don't need to set this manually since integrations, when possible, automatically fill this value.
/// For ASP.NET Core which can read from IHostingEnvironment
/// </remarks>
/// <example>
/// Production, Staging
/// </example>
/// <seealso href="https://docs.sentry.io/learn/environments/"/>
public string Environment { get; set; }

/// <summary>
/// The Data Source Name of a given project in Sentry.
/// </summary>
Expand Down
34 changes: 34 additions & 0 deletions test/Sentry.Tests/Internals/EnvironmentLocatorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using Sentry.Internal;
using Sentry.Tests.Helpers;
using Xunit;

namespace Sentry.Tests.Internals
{
public class EnvironmentLocatorTests
{
[Fact]
public void GetCurrent_WithEnvironmentVariable_ReturnsEnvironmentVariableValue()
{
const string expectedVersion = "the environment name";
EnvironmentVariableGuard.WithVariable(
Constants.EnvironmentEnvironmentVariable,
expectedVersion,
() =>
{
Assert.Equal(expectedVersion, EnvironmentLocator.GetCurrent());
});
}

[Fact]
public void GetCurrent_WithoutEnvironmentVariable_ReturnsNull()
{
EnvironmentVariableGuard.WithVariable(
Constants.ReleaseEnvironmentVariable,
null,
() =>
{
Assert.Null(EnvironmentLocator.GetCurrent());
});
}
}
}
32 changes: 31 additions & 1 deletion test/Sentry.Tests/Internals/MainSentryEventProcessorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Sentry.Internal;
using Sentry.Protocol;
using Sentry.Reflection;
using Sentry.Tests.Helpers;
using Xunit;

namespace Sentry.Tests.Internals
Expand Down Expand Up @@ -34,7 +35,36 @@ public void Process_NoReleaseOnOptions_SameAsCachedVersion()

Sut.Process(evt);

Assert.Equal(MainSentryEventProcessor.Release.Value, evt.Release);
Assert.Equal(Sut.Release, evt.Release);
}

[Fact]
public void Process_EnvironmentOnOptions_SetToEvent()
{
const string expected = "Production";
SentryOptions.Environment = expected;
var evt = new SentryEvent();

Sut.Process(evt);

Assert.Equal(expected, evt.Environment);
}

[Fact]
public void Process_NoEnvironmentOnOptions_SameAsEnvironmentVariable()
{
const string expected = "Staging";
var evt = new SentryEvent();

EnvironmentVariableGuard.WithVariable(
Constants.EnvironmentEnvironmentVariable,
expected,
() =>
{
Sut.Process(evt);
});

Assert.Equal(expected, evt.Environment);
}

[Fact]
Expand Down

0 comments on commit b770762

Please sign in to comment.