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: Stopping the SDK from creating DynamicSamplingContext Exception #2592

Merged
merged 11 commits into from
Sep 5, 2023
7 changes: 2 additions & 5 deletions src/Sentry/Internal/Hub.cs
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,7 @@ public BaggageHeader GetBaggage()
}

var propagationContext = ScopeManager.GetCurrent().Key.PropagationContext;
propagationContext.DynamicSamplingContext ??= propagationContext.CreateDynamicSamplingContext(_options);
return propagationContext.DynamicSamplingContext.ToBaggageHeader();
return propagationContext.GetDynamicSamplingContext(_options).ToBaggageHeader();
}

public TransactionContext ContinueTrace(
Expand Down Expand Up @@ -352,9 +351,7 @@ private void ApplyTraceContextToEvent(SentryEvent evt, SentryPropagationContext
evt.Contexts.Trace.TraceId = propagationContext.TraceId;
evt.Contexts.Trace.SpanId = propagationContext.SpanId;
evt.Contexts.Trace.ParentSpanId = propagationContext.ParentSpanId;

propagationContext.DynamicSamplingContext ??= propagationContext.CreateDynamicSamplingContext(_options);
evt.DynamicSamplingContext = propagationContext.DynamicSamplingContext;
evt.DynamicSamplingContext = propagationContext.GetDynamicSamplingContext(_options);
}

public SentryId CaptureEvent(SentryEvent evt, Action<Scope> configureScope) =>
Expand Down
22 changes: 9 additions & 13 deletions src/Sentry/SentryPropagationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,17 @@ internal class SentryPropagationContext
public SpanId SpanId { get; }
public SpanId? ParentSpanId { get; }

private DynamicSamplingContext? _dynamicSamplingContext;
public DynamicSamplingContext? DynamicSamplingContext
internal DynamicSamplingContext? _dynamicSamplingContext;

public DynamicSamplingContext GetDynamicSamplingContext(SentryOptions options)
{
get => _dynamicSamplingContext;
set
if (_dynamicSamplingContext is null)
{
if (_dynamicSamplingContext is null)
{
_dynamicSamplingContext = value;
}
else
{
throw new Exception("Attempted to set the DynamicSamplingContext but the context exists already.");
}
options.LogDebug("Creating the Dynamic Sampling Context from the Propagation Context");
_dynamicSamplingContext = this.CreateDynamicSamplingContext(options);
}

return _dynamicSamplingContext;
}

internal SentryPropagationContext(
Expand All @@ -33,7 +29,7 @@ internal SentryPropagationContext(
TraceId = traceId;
SpanId = SpanId.Create();
ParentSpanId = parentSpanId;
DynamicSamplingContext = dynamicSamplingContext;
_dynamicSamplingContext = dynamicSamplingContext;
}

public SentryPropagationContext()
Expand Down
5 changes: 2 additions & 3 deletions test/Sentry.Tests/SentryPropagationContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void CreateFromHeaders_TraceHeaderNullButBaggageExists_CreatesPropagation

var propagationContext = SentryPropagationContext.CreateFromHeaders(null, null, baggageHeader);

Assert.Null(propagationContext.DynamicSamplingContext);
Assert.Null(propagationContext._dynamicSamplingContext);
}

[Fact]
Expand All @@ -32,7 +32,6 @@ public void CreateFromHeaders_BaggageHeaderNotNull_CreatesPropagationContextWith

var propagationContext = SentryPropagationContext.CreateFromHeaders(null, traceHeader, baggageHeader);

Assert.NotNull(propagationContext.DynamicSamplingContext);
Assert.Equal(4, propagationContext.DynamicSamplingContext.Items.Count);
Assert.Equal(4, propagationContext.GetDynamicSamplingContext(new SentryOptions()).Items.Count);
}
}
Loading