|
9 | 9 | import static org.assertj.core.api.Assertions.assertThat; |
10 | 10 |
|
11 | 11 | import io.opentelemetry.api.GlobalOpenTelemetry; |
| 12 | +import io.opentelemetry.api.common.AttributeKey; |
| 13 | +import io.opentelemetry.api.common.Attributes; |
| 14 | +import io.opentelemetry.api.trace.SpanKind; |
12 | 15 | import io.opentelemetry.api.trace.Tracer; |
| 16 | +import io.opentelemetry.context.Context; |
13 | 17 | import io.opentelemetry.sdk.trace.ReadableSpan; |
14 | 18 | import io.opentelemetry.sdk.trace.SdkTracerProvider; |
| 19 | +import io.opentelemetry.sdk.trace.data.LinkData; |
15 | 20 | import io.opentelemetry.sdk.trace.data.SpanData; |
| 21 | +import io.opentelemetry.sdk.trace.samplers.Sampler; |
| 22 | +import io.opentelemetry.sdk.trace.samplers.SamplingResult; |
| 23 | +import java.util.List; |
16 | 24 | import org.junit.jupiter.api.BeforeEach; |
17 | 25 | import org.junit.jupiter.api.Test; |
18 | 26 |
|
@@ -97,4 +105,62 @@ void withStartTimestamp() { |
97 | 105 | SpanData spanData = ((ReadableSpan) spanShim.getSpan()).toSpanData(); |
98 | 106 | assertThat(spanData.getStartEpochNanos()).isEqualTo(micros * 1000L); |
99 | 107 | } |
| 108 | + |
| 109 | + @Test |
| 110 | + void setAttributes_beforeSpanStart() { |
| 111 | + SdkTracerProvider tracerSdkFactory = |
| 112 | + SdkTracerProvider.builder().setSampler(SamplingPrioritySampler.INSTANCE).build(); |
| 113 | + Tracer tracer = tracerSdkFactory.get("SpanShimTest"); |
| 114 | + TelemetryInfo telemetryInfo = |
| 115 | + new TelemetryInfo(tracer, OpenTracingPropagators.builder().build()); |
| 116 | + |
| 117 | + SpanBuilderShim spanBuilder1 = new SpanBuilderShim(telemetryInfo, SPAN_NAME); |
| 118 | + SpanBuilderShim spanBuilder2 = new SpanBuilderShim(telemetryInfo, SPAN_NAME); |
| 119 | + SpanShim span1 = |
| 120 | + (SpanShim) |
| 121 | + spanBuilder1 |
| 122 | + .withTag( |
| 123 | + SamplingPrioritySampler.SAMPLING_PRIORITY_TAG, |
| 124 | + SamplingPrioritySampler.UNSAMPLED_VALUE) |
| 125 | + .start(); |
| 126 | + SpanShim span2 = |
| 127 | + (SpanShim) |
| 128 | + spanBuilder2 |
| 129 | + .withTag( |
| 130 | + SamplingPrioritySampler.SAMPLING_PRIORITY_TAG, |
| 131 | + SamplingPrioritySampler.SAMPLED_VALUE) |
| 132 | + .start(); |
| 133 | + assertThat(span1.getSpan().isRecording()).isFalse(); |
| 134 | + assertThat(span2.getSpan().isRecording()).isTrue(); |
| 135 | + assertThat(span1.getSpan().getSpanContext().isSampled()).isFalse(); |
| 136 | + assertThat(span2.getSpan().getSpanContext().isSampled()).isTrue(); |
| 137 | + } |
| 138 | + |
| 139 | + static final class SamplingPrioritySampler implements Sampler { |
| 140 | + static final SamplingPrioritySampler INSTANCE = new SamplingPrioritySampler(); |
| 141 | + static final String SAMPLING_PRIORITY_TAG = "sampling.priority"; |
| 142 | + static final long UNSAMPLED_VALUE = 0; |
| 143 | + static final long SAMPLED_VALUE = 1; |
| 144 | + |
| 145 | + @Override |
| 146 | + public String getDescription() { |
| 147 | + return "SamplingPrioritySampler"; |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public SamplingResult shouldSample( |
| 152 | + Context parentContext, |
| 153 | + String traceId, |
| 154 | + String name, |
| 155 | + SpanKind spanKind, |
| 156 | + Attributes attributes, |
| 157 | + List<LinkData> parentLinks) { |
| 158 | + |
| 159 | + if (attributes.get(AttributeKey.longKey(SAMPLING_PRIORITY_TAG)) == UNSAMPLED_VALUE) { |
| 160 | + return SamplingResult.drop(); |
| 161 | + } |
| 162 | + |
| 163 | + return SamplingResult.recordAndSample(); |
| 164 | + } |
| 165 | + } |
100 | 166 | } |
0 commit comments