Skip to content

Commit a7f2cba

Browse files
committed
Do not link current span when propagation headers are missing
PropagatorBasedSpanLinksExtractor used the caller-provided parentContext as the fallback for TextMapPropagator.extract, which meant that when a message carried no propagation headers the extractor would link the new span to the ambient parent (e.g. a CONSUMER receive span). Use Context.root() internally so that missing headers result in no link being added, and remove the now-redundant Context.root() overrides and stale comments in the batch extractors for kafka-clients, kafka-connect and pulsar. Document the invariant on SpanLinksExtractor.
1 parent bc5dc82 commit a7f2cba

5 files changed

Lines changed: 12 additions & 7 deletions

File tree

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/SpanLinksExtractor.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ public interface SpanLinksExtractor<REQUEST> {
1515
/**
1616
* Extracts {@link SpanContext}s that should be linked to the newly created span and adds them to
1717
* {@code spanLinks}.
18+
*
19+
* <p>A link points to a related span other than the parent (for example, the producer span of a
20+
* message being processed). Implementations should not derive link targets from the span
21+
* currently in {@code parentContext}; {@code parentContext} is provided for access to
22+
* request-scoped state attached via {@link io.opentelemetry.context.ContextKey ContextKey}s (for
23+
* example, baggage set earlier in the pipeline).
1824
*/
1925
void extract(SpanLinksBuilder spanLinks, Context parentContext, REQUEST request);
2026
}

instrumentation-api/src/main/java/io/opentelemetry/instrumentation/api/internal/PropagatorBasedSpanLinksExtractor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,9 @@ public PropagatorBasedSpanLinksExtractor(
3030

3131
@Override
3232
public void extract(SpanLinksBuilder spanLinks, Context parentContext, REQUEST request) {
33-
Context extracted = propagator.extract(parentContext, request, getter);
33+
// extract from the carrier against a root context so that, when the carrier contains no
34+
// propagation data, no link is added (rather than linking to the ambient parent span)
35+
Context extracted = propagator.extract(Context.root(), request, getter);
3436
spanLinks.addLink(Span.fromContext(extracted).getSpanContext());
3537
}
3638
}

instrumentation/kafka/kafka-clients/kafka-clients-common-0.11/library/src/main/java/io/opentelemetry/instrumentation/kafkaclients/common/v0_11/internal/KafkaBatchProcessSpanLinksExtractor.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,9 @@ public void extract(
2626
SpanLinksBuilder spanLinks, Context parentContext, KafkaReceiveRequest request) {
2727

2828
for (ConsumerRecord<?, ?> record : request.getRecords()) {
29-
// explicitly passing root to avoid situation where context propagation is turned off and the
30-
// parent (CONSUMER receive) span is linked
3129
singleRecordLinkExtractor.extract(
3230
spanLinks,
33-
Context.root(),
31+
parentContext,
3432
KafkaProcessRequest.create(record, request.getConsumerGroup(), request.getClientId()));
3533
}
3634
}

instrumentation/kafka/kafka-connect-2.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/kafkaconnect/v2_6/KafkaConnectBatchProcessSpanLinksExtractor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ final class KafkaConnectBatchProcessSpanLinksExtractor
2525
@Override
2626
public void extract(SpanLinksBuilder spanLinks, Context parentContext, KafkaConnectTask request) {
2727
for (SinkRecord record : request.getRecords()) {
28-
// explicitly passing root to avoid linking the ambient context when propagation is off
29-
singleRecordLinkExtractor.extract(spanLinks, Context.root(), record);
28+
singleRecordLinkExtractor.extract(spanLinks, parentContext, record);
3029
}
3130
}
3231
}

instrumentation/pulsar/pulsar-2.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/pulsar/v2_8/telemetry/PulsarBatchRequestSpanLinksExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void extract(
2626

2727
for (Message<?> message : request.getMessages()) {
2828
singleRecordLinkExtractor.extract(
29-
spanLinks, Context.root(), PulsarRequest.create(message, request.getUrlData()));
29+
spanLinks, parentContext, PulsarRequest.create(message, request.getUrlData()));
3030
}
3131
}
3232
}

0 commit comments

Comments
 (0)