Skip to content

Commit 3940dd2

Browse files
Set OT Shim's SpanBuilder attributes before the Span is created. (open-telemetry#3984)
* Set OT Shim's SpanBuilder attributes before the Span is created. * Update opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java Co-authored-by: Shawn Song <mr.song.shawn@gmail.com> Co-authored-by: Shawn Song <mr.song.shawn@gmail.com>
1 parent 3ce2737 commit 3940dd2

2 files changed

Lines changed: 72 additions & 3 deletions

File tree

opentracing-shim/src/main/java/io/opentelemetry/opentracingshim/SpanBuilderShim.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,16 @@ public Span start() {
214214
builder.setStartTimestamp(startTimestampMicros, TimeUnit.MICROSECONDS);
215215
}
216216

217-
io.opentelemetry.api.trace.Span span = builder.startSpan();
218-
217+
// Attributes passed to the OT SpanBuilder MUST
218+
// be set before the OTel Span is created,
219+
// so those attributes are available to the Sampling API.
219220
for (int i = 0; i < this.spanBuilderAttributeKeys.size(); i++) {
220221
AttributeKey key = this.spanBuilderAttributeKeys.get(i);
221222
Object value = this.spanBuilderAttributeValues.get(i);
222-
span.setAttribute(key, value);
223+
builder.setAttribute(key, value);
223224
}
225+
226+
io.opentelemetry.api.trace.Span span = builder.startSpan();
224227
if (error) {
225228
span.setStatus(StatusCode.ERROR);
226229
}

opentracing-shim/src/test/java/io/opentelemetry/opentracingshim/SpanBuilderShimTest.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,18 @@
99
import static org.assertj.core.api.Assertions.assertThat;
1010

1111
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;
1215
import io.opentelemetry.api.trace.Tracer;
16+
import io.opentelemetry.context.Context;
1317
import io.opentelemetry.sdk.trace.ReadableSpan;
1418
import io.opentelemetry.sdk.trace.SdkTracerProvider;
19+
import io.opentelemetry.sdk.trace.data.LinkData;
1520
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;
1624
import org.junit.jupiter.api.BeforeEach;
1725
import org.junit.jupiter.api.Test;
1826

@@ -97,4 +105,62 @@ void withStartTimestamp() {
97105
SpanData spanData = ((ReadableSpan) spanShim.getSpan()).toSpanData();
98106
assertThat(spanData.getStartEpochNanos()).isEqualTo(micros * 1000L);
99107
}
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+
}
100166
}

0 commit comments

Comments
 (0)