|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +using System.Diagnostics; |
| 5 | +using BenchmarkDotNet.Attributes; |
| 6 | +using OpenTelemetry; |
| 7 | +using OpenTelemetry.Trace; |
| 8 | + |
| 9 | +namespace Benchmarks.Trace; |
| 10 | + |
| 11 | +#pragma warning disable CA1001 // Types that own disposable fields should be disposable - handled by GlobalCleanup |
| 12 | +[MemoryDiagnoser] |
| 13 | +public class SamplingResultBenchmarks |
| 14 | +#pragma warning restore CA1001 // Types that own disposable fields should be disposable - handled by GlobalCleanup |
| 15 | +{ |
| 16 | + private static readonly KeyValuePair<string, object>[] SamplingAttributes = |
| 17 | + [ |
| 18 | + new("sampling.priority", 1), |
| 19 | + new("sampling.rule", "always"), |
| 20 | + ]; |
| 21 | + |
| 22 | + private ActivitySource? sourceNoAttributes; |
| 23 | + private ActivitySource? sourceWithAttributeArray; |
| 24 | + private ActivitySource? sourceWithAttributeList; |
| 25 | + private ActivitySource? sourceDrop; |
| 26 | + private ActivitySource? sourceParentBased; |
| 27 | + |
| 28 | + private ActivityContext sampledRemoteParent; |
| 29 | + |
| 30 | + private TracerProvider? providerNoAttributes; |
| 31 | + private TracerProvider? providerWithAttributeArray; |
| 32 | + private TracerProvider? providerWithAttributeList; |
| 33 | + private TracerProvider? providerDrop; |
| 34 | + private TracerProvider? providerParentBased; |
| 35 | + |
| 36 | + [GlobalSetup] |
| 37 | + public void Setup() |
| 38 | + { |
| 39 | + this.sourceNoAttributes = new ActivitySource("SamplingResult.NoAttributes"); |
| 40 | + this.sourceWithAttributeArray = new ActivitySource("SamplingResult.WithAttributeArray"); |
| 41 | + this.sourceWithAttributeList = new ActivitySource("SamplingResult.WithAttributeList"); |
| 42 | + this.sourceDrop = new ActivitySource("SamplingResult.Drop"); |
| 43 | + this.sourceParentBased = new ActivitySource("SamplingResult.ParentBased"); |
| 44 | + |
| 45 | + this.sampledRemoteParent = new ActivityContext( |
| 46 | + ActivityTraceId.CreateRandom(), |
| 47 | + ActivitySpanId.CreateRandom(), |
| 48 | + ActivityTraceFlags.Recorded, |
| 49 | + traceState: null, |
| 50 | + isRemote: true); |
| 51 | + |
| 52 | + // Sampler returns RecordAndSample with no attributes - the common case. |
| 53 | + this.providerNoAttributes = Sdk.CreateTracerProviderBuilder() |
| 54 | + .AddSource(this.sourceNoAttributes.Name) |
| 55 | + .SetSampler(new DelegateSampler(_ => new SamplingResult(SamplingDecision.RecordAndSample))) |
| 56 | + .Build(); |
| 57 | + |
| 58 | + // Sampler returns attributes as a T[] - exercises the array fast-path. |
| 59 | + this.providerWithAttributeArray = Sdk.CreateTracerProviderBuilder() |
| 60 | + .AddSource(this.sourceWithAttributeArray.Name) |
| 61 | + .SetSampler(new DelegateSampler(_ => new SamplingResult(SamplingDecision.RecordAndSample, SamplingAttributes))) |
| 62 | + .Build(); |
| 63 | + |
| 64 | + // Sampler returns attributes as a List<T> - exercises the IEnumerable fallback path. |
| 65 | + this.providerWithAttributeList = Sdk.CreateTracerProviderBuilder() |
| 66 | + .AddSource(this.sourceWithAttributeList.Name) |
| 67 | + .SetSampler(new DelegateSampler(_ => new SamplingResult(SamplingDecision.RecordAndSample, [.. SamplingAttributes]))) |
| 68 | + .Build(); |
| 69 | + |
| 70 | + // Sampler drops the span - attribute loop is never entered. |
| 71 | + this.providerDrop = Sdk.CreateTracerProviderBuilder() |
| 72 | + .AddSource(this.sourceDrop.Name) |
| 73 | + .SetSampler(new DelegateSampler(_ => new SamplingResult(SamplingDecision.Drop))) |
| 74 | + .Build(); |
| 75 | + |
| 76 | + // ParentBasedSampler with AlwaysOnSampler root - realistic production default. |
| 77 | + this.providerParentBased = Sdk.CreateTracerProviderBuilder() |
| 78 | + .AddSource(this.sourceParentBased.Name) |
| 79 | + .SetSampler(new ParentBasedSampler(new AlwaysOnSampler())) |
| 80 | + .Build(); |
| 81 | + } |
| 82 | + |
| 83 | + [GlobalCleanup] |
| 84 | + public void Cleanup() |
| 85 | + { |
| 86 | + this.sourceNoAttributes?.Dispose(); |
| 87 | + this.sourceWithAttributeArray?.Dispose(); |
| 88 | + this.sourceWithAttributeList?.Dispose(); |
| 89 | + this.sourceDrop?.Dispose(); |
| 90 | + this.sourceParentBased?.Dispose(); |
| 91 | + |
| 92 | + this.providerNoAttributes?.Dispose(); |
| 93 | + this.providerWithAttributeArray?.Dispose(); |
| 94 | + this.providerWithAttributeList?.Dispose(); |
| 95 | + this.providerDrop?.Dispose(); |
| 96 | + this.providerParentBased?.Dispose(); |
| 97 | + } |
| 98 | + |
| 99 | + [Benchmark(Baseline = true)] |
| 100 | + public void NoAttributes() |
| 101 | + { |
| 102 | + using var activity = this.sourceNoAttributes!.StartActivity("Benchmark", ActivityKind.Server, this.sampledRemoteParent); |
| 103 | + } |
| 104 | + |
| 105 | + [Benchmark] |
| 106 | + public void WithAttributeArray() |
| 107 | + { |
| 108 | + using var activity = this.sourceWithAttributeArray!.StartActivity("Benchmark", ActivityKind.Server, this.sampledRemoteParent); |
| 109 | + } |
| 110 | + |
| 111 | + [Benchmark] |
| 112 | + public void WithAttributeList() |
| 113 | + { |
| 114 | + using var activity = this.sourceWithAttributeList!.StartActivity("Benchmark", ActivityKind.Server, this.sampledRemoteParent); |
| 115 | + } |
| 116 | + |
| 117 | + [Benchmark] |
| 118 | + public void Drop() |
| 119 | + { |
| 120 | + using var activity = this.sourceDrop!.StartActivity("Benchmark", ActivityKind.Server, this.sampledRemoteParent); |
| 121 | + } |
| 122 | + |
| 123 | + [Benchmark] |
| 124 | + public void ParentBasedSampled() |
| 125 | + { |
| 126 | + using var activity = this.sourceParentBased!.StartActivity("Benchmark", ActivityKind.Server, this.sampledRemoteParent); |
| 127 | + } |
| 128 | + |
| 129 | + private sealed class DelegateSampler(Func<SamplingParameters, SamplingResult> sample) : Sampler |
| 130 | + { |
| 131 | + public override SamplingResult ShouldSample(in SamplingParameters samplingParameters) |
| 132 | + => sample(samplingParameters); |
| 133 | + } |
| 134 | +} |
0 commit comments