avoid null reference exceptions when setting special span tags (#977)

This commit is contained in:
Lucas Pimentel-Ordyna 2020-10-09 09:52:27 -04:00 committed by GitHub
parent 4411fd6887
commit 1193a08bde
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 9 deletions

View File

@ -31,6 +31,11 @@ namespace Datadog.Trace.ExtensionMethods
{
if (value == null) { throw new ArgumentNullException(nameof(value)); }
if (value.Length == 0)
{
return null;
}
if (value.Length == 1)
{
if (value[0] == 'T' || value[0] == 't' ||

View File

@ -171,7 +171,7 @@ namespace Datadog.Trace
#pragma warning disable CS0618 // Type or member is obsolete
case Trace.Tags.ForceKeep:
case Trace.Tags.ManualKeep:
if (value.ToBoolean() ?? false)
if (value?.ToBoolean() == true)
{
// user-friendly tag to set UserKeep priority
Context.TraceContext.SamplingPriority = SamplingPriority.UserKeep;
@ -180,7 +180,7 @@ namespace Datadog.Trace
break;
case Trace.Tags.ForceDrop:
case Trace.Tags.ManualDrop:
if (value.ToBoolean() ?? false)
if (value?.ToBoolean() == true)
{
// user-friendly tag to set UserReject priority
Context.TraceContext.SamplingPriority = SamplingPriority.UserReject;
@ -189,18 +189,23 @@ namespace Datadog.Trace
break;
#pragma warning restore CS0618 // Type or member is obsolete
case Trace.Tags.Analytics:
// value is a string and can represent a bool ("true") or a double ("0.5"),
// so try to parse both.
// note that "1" and "0" can parse as either type,
// but they mean the same thing in this case, so it's fine.
bool? boolean = value.ToBoolean();
if (string.IsNullOrEmpty(value))
{
// remove metric
SetMetric(Trace.Tags.Analytics, null);
return this;
}
if (boolean == true)
// value is a string and can represent a bool ("true") or a double ("0.5"),
// so try to parse both. note that "1" and "0" will parse as boolean, which is fine.
bool? analyticsSamplingRate = value.ToBoolean();
if (analyticsSamplingRate == true)
{
// always sample
SetMetric(Trace.Tags.Analytics, 1.0);
}
else if (boolean == false)
else if (analyticsSamplingRate == false)
{
// never sample
SetMetric(Trace.Tags.Analytics, 0.0);