Skip to content

Commit 1aad9ab

Browse files
authored
fix: Allow a DSN string.Empty to be overwritten by the environment (#3147)
1 parent a3d4c4f commit 1aad9ab

File tree

3 files changed

+82
-11
lines changed

3 files changed

+82
-11
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
### Fixes
6+
7+
- 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))
8+
59
### Dependencies
610

711
- Bump CLI from v2.28.0 to v2.28.6 ([#3145](https://github.com/getsentry/sentry-dotnet/pull/3145), [#3148](https://github.com/getsentry/sentry-dotnet/pull/3148))

src/Sentry/Internal/SettingLocator.cs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,33 @@ public SettingLocator(SentryOptions options)
3333
public string GetDsn()
3434
{
3535
// For DSN only
36-
// An empty string set on the option should NOT be converted to null because it is used
37-
// to indicate the the SDK is disabled.
38-
_options.Dsn ??= GetEnvironmentVariable(Constants.DsnEnvironmentVariable)
39-
?? AssemblyForAttributes?.GetCustomAttribute<DsnAttribute>()?.Dsn;
40-
if (_options.Dsn is null)
36+
37+
if (!string.IsNullOrEmpty(_options.Dsn))
38+
{
39+
return _options.Dsn;
40+
}
41+
42+
var dsn = GetEnvironmentVariable(Constants.DsnEnvironmentVariable)
43+
?? AssemblyForAttributes?.GetCustomAttribute<DsnAttribute>()?.Dsn;
44+
45+
// If there has been no DSN provided (`null`) and none has been found in the environment we consider this a
46+
// failed configuration and throw
47+
// By conventions, skip this if the DSN is not `null` i.e. `string.Empty`
48+
if (_options.Dsn is null && dsn is null)
4149
{
4250
throw new ArgumentNullException("You must supply a DSN to use Sentry." +
4351
"To disable Sentry, pass an empty string: \"\"." +
4452
"See https://docs.sentry.io/platforms/dotnet/configuration/options/#dsn");
4553
}
46-
return _options.Dsn;
54+
55+
// Overwriting the `string.Empty` with the DSN found in the environment
56+
if (dsn is not null)
57+
{
58+
_options.Dsn = dsn;
59+
}
60+
61+
Debug.Assert(_options.Dsn != null, "Dsn can't be null at this point based on the rules above");
62+
return _options.Dsn!;
4763
}
4864

4965
public string GetEnvironment() => GetEnvironment(true)!;

test/Sentry.Tests/Internals/SettingLocatorTests.cs

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ namespace Sentry.Tests.Internals;
55
public class SettingLocatorTests
66
{
77
private const string DisableSdkDsnValue = SentryConstants.DisableSdkDsnValue;
8-
private const string DsnEnvironmentVariable = Internal.Constants.DsnEnvironmentVariable;
9-
private const string EnvironmentEnvironmentVariable = Internal.Constants.EnvironmentEnvironmentVariable;
10-
private const string ReleaseEnvironmentVariable = Internal.Constants.ReleaseEnvironmentVariable;
8+
private const string DsnEnvironmentVariable = Constants.DsnEnvironmentVariable;
9+
private const string EnvironmentEnvironmentVariable = Constants.EnvironmentEnvironmentVariable;
10+
private const string ReleaseEnvironmentVariable = Constants.ReleaseEnvironmentVariable;
1111

1212
private static Assembly GetAssemblyWithDsn(string dsn) =>
1313
AssemblyCreationHelper.CreateAssemblyWithDsnAttribute(dsn);
@@ -82,6 +82,17 @@ public void CanUseOtherAssembly()
8282
Assert.Same(expected, actual);
8383
}
8484

85+
[Fact]
86+
public void GetDsn_WithEmptyString_DoesNotThrow()
87+
{
88+
var options = new SentryOptions { Dsn = DisableSdkDsnValue };
89+
90+
var dsn = options.SettingLocator.GetDsn();
91+
92+
Assert.Equal(DisableSdkDsnValue, dsn);
93+
Assert.Equal(DisableSdkDsnValue, options.Dsn);
94+
}
95+
8596
[Fact]
8697
public void GetDsn_WithDsnInEnvironmentVariable_ReturnsAndSetsDsn()
8798
{
@@ -123,7 +134,7 @@ public void GetDsn_WithDsnInBothEnvironmentVariableAndAttribute_ReturnsAndSetsDs
123134
}
124135

125136
[Fact]
126-
public void GetDsn_WithOptionAlreadySet_IgnoresEnvironmentVariable()
137+
public void GetDsn_DsnIsNonEmptyString_IgnoresEnvironmentVariable()
127138
{
128139
const string validDsn1 = ValidDsn + "1";
129140
const string validDsn2 = ValidDsn + "2";
@@ -138,7 +149,7 @@ public void GetDsn_WithOptionAlreadySet_IgnoresEnvironmentVariable()
138149
}
139150

140151
[Fact]
141-
public void GetDsn_WithOptionAlreadySet_IgnoresAttribute()
152+
public void GetDsn_DsnIsNonEmptyString_IgnoresAttribute()
142153
{
143154
const string validDsn1 = ValidDsn + "1";
144155
const string validDsn2 = ValidDsn + "2";
@@ -197,6 +208,46 @@ public void GetDsn_WithDisabledDsnInEnvironmentVariableButValidDsnInAttribute_Re
197208
Assert.Equal(DisableSdkDsnValue, options.Dsn);
198209
}
199210

211+
[Fact]
212+
public void GetDsn_DsnIsStringEmptyButEnvironmentValid_ReturnsAndSetsEnvironmentDsn()
213+
{
214+
var options = new SentryOptions { Dsn = string.Empty };
215+
options.FakeSettings().EnvironmentVariables[DsnEnvironmentVariable] = ValidDsn;
216+
217+
var dsn = options.SettingLocator.GetDsn();
218+
219+
Assert.Equal(ValidDsn, dsn);
220+
Assert.Equal(ValidDsn, options.Dsn);
221+
}
222+
223+
[Fact]
224+
public void GetDsn_DsnIsStringEmptyButAttributeValid_ReturnsAndSetsAttributeDsn()
225+
{
226+
var options = new SentryOptions { Dsn = string.Empty };
227+
options.FakeSettings().AssemblyForAttributes = GetAssemblyWithDsn(ValidDsn);
228+
229+
var dsn = options.SettingLocator.GetDsn();
230+
231+
Assert.Equal(ValidDsn, dsn);
232+
Assert.Equal(ValidDsn, options.Dsn);
233+
}
234+
235+
[Fact]
236+
public void GetDsn_DsnIsStringEmptyButEnvironmentAndAttributeValid_ReturnsAndSetsEnvironmentDsn()
237+
{
238+
const string validDsn1 = ValidDsn + "1";
239+
const string validDsn2 = ValidDsn + "2";
240+
241+
var options = new SentryOptions { Dsn = string.Empty };
242+
options.FakeSettings().EnvironmentVariables[DsnEnvironmentVariable] = validDsn1;
243+
options.FakeSettings().AssemblyForAttributes = GetAssemblyWithDsn(validDsn2);
244+
245+
var dsn = options.SettingLocator.GetDsn();
246+
247+
Assert.Equal(validDsn1, dsn);
248+
Assert.Equal(validDsn1, options.Dsn);
249+
}
250+
200251
[Fact]
201252
public void GetEnvironment_WithEnvironmentInEnvironmentVariable_ReturnsAndSetsEnvironment()
202253
{

0 commit comments

Comments
 (0)