Skip to content

Commit 84b3778

Browse files
authored
[SDK] Add a test covering exception thrown in custom sampler (#4072)
1 parent 9e00b8f commit 84b3778

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

test/OpenTelemetry.Tests/Trace/SamplersTest.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,5 +252,30 @@ public void SamplersDoesNotImpactTraceStateWhenUsingNull(SamplingDecision sampli
252252
Assert.Equal(parentTraceState, activity.TraceStateString);
253253
}
254254
}
255+
256+
[Fact]
257+
public void SamplerExceptionBubblesUpTest()
258+
{
259+
// Note: This test verifies there is NO try/catch around sampling
260+
// and it will throw. For the discussion behind this see:
261+
// https://github.com/open-telemetry/opentelemetry-dotnet/pull/4072
262+
263+
var activitySourceName = Utils.GetCurrentMethodName();
264+
using var activitySource = new ActivitySource(activitySourceName);
265+
using var tracerProvider = Sdk.CreateTracerProviderBuilder()
266+
.AddSource(activitySourceName)
267+
.SetSampler(new ThrowingSampler())
268+
.Build();
269+
270+
Assert.Throws<InvalidOperationException>(() => activitySource.StartActivity("ThrowingSampler"));
271+
}
272+
273+
private sealed class ThrowingSampler : Sampler
274+
{
275+
public override SamplingResult ShouldSample(in SamplingParameters samplingParameters)
276+
{
277+
throw new InvalidOperationException("ThrowingSampler");
278+
}
279+
}
255280
}
256281
}

0 commit comments

Comments
 (0)