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
Changes from 3 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
32 changes: 25 additions & 7 deletions src/Sentry/Internal/SettingLocator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,34 @@ 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 the DSN has not been set at all require it to be on either the environment or an AssemblyAttribute. If
// neither can be found: throw
if (_options.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");
_options.Dsn = GetEnvironmentVariable(Constants.DsnEnvironmentVariable)
?? AssemblyForAttributes?.GetCustomAttribute<DsnAttribute>()?.Dsn;

if (_options.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");
}
}
// The SDK has been disabled by the user explicitly setting the DSN to `string.Empty`.
// By convention, the DSN can be overwritten in the environment
else if (_options.Dsn.Equals(string.Empty))
{
var dsn = GetEnvironmentVariable(Constants.DsnEnvironmentVariable)
?? AssemblyForAttributes?.GetCustomAttribute<DsnAttribute>()?.Dsn;

if (dsn is not null)
{
_options.Dsn = dsn;
}
}

return _options.Dsn;
}

Expand Down
Loading