Skip to content

Commit

Permalink
[SDK] Add a test covering exception thrown in custom sampler (#4072)
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeBlanch authored Jan 23, 2023
1 parent 9e00b8f commit 84b3778
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions test/OpenTelemetry.Tests/Trace/SamplersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,5 +252,30 @@ public void SamplersDoesNotImpactTraceStateWhenUsingNull(SamplingDecision sampli
Assert.Equal(parentTraceState, activity.TraceStateString);
}
}

[Fact]
public void SamplerExceptionBubblesUpTest()
{
// Note: This test verifies there is NO try/catch around sampling
// and it will throw. For the discussion behind this see:
// https://github.com/open-telemetry/opentelemetry-dotnet/pull/4072

var activitySourceName = Utils.GetCurrentMethodName();
using var activitySource = new ActivitySource(activitySourceName);
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
.AddSource(activitySourceName)
.SetSampler(new ThrowingSampler())
.Build();

Assert.Throws<InvalidOperationException>(() => activitySource.StartActivity("ThrowingSampler"));
}

private sealed class ThrowingSampler : Sampler
{
public override SamplingResult ShouldSample(in SamplingParameters samplingParameters)
{
throw new InvalidOperationException("ThrowingSampler");
}
}
}
}

0 comments on commit 84b3778

Please sign in to comment.