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

fix: Allow a DSN string.Empty to be overwritten by the environment #3147

Merged
merged 8 commits into from
Feb 14, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Fixes

- The SDK can be disabled by setting `options.Dsn = "";` By convention, the SDK allows the DSN set to `string.Empty` to be overwritten by the environment. ([#3147](https://github.com/getsentry/sentry-dotnet/pull/3147))

### Dependencies

- Bump CLI from v2.28.0 to v2.28.5 ([#3145](https://github.com/getsentry/sentry-dotnet/pull/3145))
Expand Down
27 changes: 21 additions & 6 deletions src/Sentry/Internal/SettingLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,32 @@ public SettingLocator(SentryOptions options)
public string GetDsn()
{
// For DSN only
// An empty string set on the option should NOT be converted to null because it is used
// to indicate the the SDK is disabled.
_options.Dsn ??= GetEnvironmentVariable(Constants.DsnEnvironmentVariable)
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved
?? AssemblyForAttributes?.GetCustomAttribute<DsnAttribute>()?.Dsn;
if (_options.Dsn is null)

if (!string.IsNullOrEmpty(_options.Dsn))
{
return _options.Dsn;
}

var dsn = GetEnvironmentVariable(Constants.DsnEnvironmentVariable)
?? AssemblyForAttributes?.GetCustomAttribute<DsnAttribute>()?.Dsn;

// If there has been no DSN provided (`null`) and none has been found in the environment we consider this a
// failed configuration and throw
// By conventions, skip this if the DSN is not `null` i.e. `string.Empty`
if (_options.Dsn is null && dsn is null)
{
throw new ArgumentNullException("You must supply a DSN to use Sentry." +
"To disable Sentry, pass an empty string: \"\"." +
"See https://docs.sentry.io/platforms/dotnet/configuration/options/#dsn");
}
return _options.Dsn;

// Overwriting the `string.Empty` with the DSN found in the environment
if (dsn is not null)
{
_options.Dsn = dsn;
}

return _options.Dsn!;
bitsandfoxes marked this conversation as resolved.
Show resolved Hide resolved
}

public string GetEnvironment() => GetEnvironment(true)!;
Expand Down
61 changes: 56 additions & 5 deletions test/Sentry.Tests/Internals/SettingLocatorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace Sentry.Tests.Internals;
public class SettingLocatorTests
{
private const string DisableSdkDsnValue = SentryConstants.DisableSdkDsnValue;
private const string DsnEnvironmentVariable = Internal.Constants.DsnEnvironmentVariable;
private const string EnvironmentEnvironmentVariable = Internal.Constants.EnvironmentEnvironmentVariable;
private const string ReleaseEnvironmentVariable = Internal.Constants.ReleaseEnvironmentVariable;
private const string DsnEnvironmentVariable = Constants.DsnEnvironmentVariable;
private const string EnvironmentEnvironmentVariable = Constants.EnvironmentEnvironmentVariable;
private const string ReleaseEnvironmentVariable = Constants.ReleaseEnvironmentVariable;

private static Assembly GetAssemblyWithDsn(string dsn) =>
AssemblyCreationHelper.CreateAssemblyWithDsnAttribute(dsn);
Expand Down Expand Up @@ -82,6 +82,17 @@ public void CanUseOtherAssembly()
Assert.Same(expected, actual);
}

[Fact]
public void GetDsn_WithEmptyString_DoesNotThrow()
{
var options = new SentryOptions { Dsn = DisableSdkDsnValue };

var dsn = options.SettingLocator.GetDsn();

Assert.Equal(DisableSdkDsnValue, dsn);
Assert.Equal(DisableSdkDsnValue, options.Dsn);
}

[Fact]
public void GetDsn_WithDsnInEnvironmentVariable_ReturnsAndSetsDsn()
{
Expand Down Expand Up @@ -123,7 +134,7 @@ public void GetDsn_WithDsnInBothEnvironmentVariableAndAttribute_ReturnsAndSetsDs
}

[Fact]
public void GetDsn_WithOptionAlreadySet_IgnoresEnvironmentVariable()
public void GetDsn_DsnIsNonEmptyString_IgnoresEnvironmentVariable()
{
const string validDsn1 = ValidDsn + "1";
const string validDsn2 = ValidDsn + "2";
Expand All @@ -138,7 +149,7 @@ public void GetDsn_WithOptionAlreadySet_IgnoresEnvironmentVariable()
}

[Fact]
public void GetDsn_WithOptionAlreadySet_IgnoresAttribute()
public void GetDsn_DsnIsNonEmptyString_IgnoresAttribute()
{
const string validDsn1 = ValidDsn + "1";
const string validDsn2 = ValidDsn + "2";
Expand Down Expand Up @@ -197,6 +208,46 @@ public void GetDsn_WithDisabledDsnInEnvironmentVariableButValidDsnInAttribute_Re
Assert.Equal(DisableSdkDsnValue, options.Dsn);
}

[Fact]
public void GetDsn_DsnIsStringEmptyButEnvironmentValid_ReturnsAndSetsEnvironmentDsn()
{
var options = new SentryOptions { Dsn = string.Empty };
options.FakeSettings().EnvironmentVariables[DsnEnvironmentVariable] = ValidDsn;

var dsn = options.SettingLocator.GetDsn();

Assert.Equal(ValidDsn, dsn);
Assert.Equal(ValidDsn, options.Dsn);
}

[Fact]
public void GetDsn_DsnIsStringEmptyButAttributeValid_ReturnsAndSetsAttributeDsn()
{
var options = new SentryOptions { Dsn = string.Empty };
options.FakeSettings().AssemblyForAttributes = GetAssemblyWithDsn(ValidDsn);

var dsn = options.SettingLocator.GetDsn();

Assert.Equal(ValidDsn, dsn);
Assert.Equal(ValidDsn, options.Dsn);
}

[Fact]
public void GetDsn_DsnIsStringEmptyButEnvironmentAndAttributeValid_ReturnsAndSetsEnvironmentDsn()
{
const string validDsn1 = ValidDsn + "1";
const string validDsn2 = ValidDsn + "2";

var options = new SentryOptions { Dsn = string.Empty };
options.FakeSettings().EnvironmentVariables[DsnEnvironmentVariable] = validDsn1;
options.FakeSettings().AssemblyForAttributes = GetAssemblyWithDsn(validDsn2);

var dsn = options.SettingLocator.GetDsn();

Assert.Equal(validDsn1, dsn);
Assert.Equal(validDsn1, options.Dsn);
}

[Fact]
public void GetEnvironment_WithEnvironmentInEnvironmentVariable_ReturnsAndSetsEnvironment()
{
Expand Down