Skip to content

Commit

Permalink
Fix ottracepropagation for short span ids (#6734)
Browse files Browse the repository at this point in the history
  • Loading branch information
viveksing authored Sep 30, 2024
1 parent 2dbb8de commit 46b28eb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ final class Common {
static final int MAX_TRACE_ID_LENGTH = TraceId.getLength();
static final int MIN_TRACE_ID_LENGTH = MAX_TRACE_ID_LENGTH / 2;

static final int MAX_SPAN_ID_LENGTH = SpanId.getLength();

private Common() {}

static SpanContext buildSpanContext(
Expand All @@ -44,7 +46,7 @@ static SpanContext buildSpanContext(

return SpanContext.createFromRemoteParent(
StringUtils.padLeft(traceId, MAX_TRACE_ID_LENGTH),
spanId,
StringUtils.padLeft(spanId, MAX_SPAN_ID_LENGTH),
traceFlags,
TraceState.getDefault());
} catch (RuntimeException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@

package io.opentelemetry.extension.trace.propagation;

import static io.opentelemetry.extension.trace.propagation.Common.MAX_SPAN_ID_LENGTH;
import static io.opentelemetry.extension.trace.propagation.Common.MAX_TRACE_ID_LENGTH;

import io.opentelemetry.api.baggage.Baggage;
import io.opentelemetry.api.baggage.BaggageBuilder;
import io.opentelemetry.api.internal.StringUtils;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.api.trace.SpanId;
import io.opentelemetry.api.trace.TraceId;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapGetter;
Expand Down Expand Up @@ -96,7 +98,13 @@ public <C> Context extract(Context context, @Nullable C carrier, TextMapGetter<C
incomingTraceId == null
? TraceId.getInvalid()
: StringUtils.padLeft(incomingTraceId, MAX_TRACE_ID_LENGTH);
String spanId = getter.get(carrier, SPAN_ID_HEADER);

String incomingSpanId = getter.get(carrier, SPAN_ID_HEADER);
String spanId =
incomingSpanId == null
? SpanId.getInvalid()
: StringUtils.padLeft(incomingSpanId, MAX_SPAN_ID_LENGTH);

String sampled = getter.get(carrier, SAMPLED_HEADER);
SpanContext spanContext = buildSpanContext(traceId, spanId, sampled);
if (!spanContext.isValid()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class OtTracePropagatorTest {
private static final String SHORT_TRACE_ID = "ff00000000000000";
private static final String SHORT_TRACE_ID_FULL = "0000000000000000ff00000000000000";
private static final String SPAN_ID = "ff00000000000041";
private static final String SHORT_SPAN_ID = "f00000000000041";
private static final String SHORT_SPAN_ID_FULL = "0f00000000000041";
private static final TextMapSetter<Map<String, String>> setter = Map::put;
private static final TextMapGetter<Map<String, String>> getter =
new TextMapGetter<Map<String, String>>() {
Expand Down Expand Up @@ -261,6 +263,45 @@ void extract_NotSampledContext_Short_TraceId() {
SHORT_TRACE_ID_FULL, SPAN_ID, TraceFlags.getDefault(), TraceState.getDefault()));
}

@Test
void extract_SampledContext_Int_Short_SPanId() {
Map<String, String> carrier = new LinkedHashMap<>();
carrier.put(OtTracePropagator.TRACE_ID_HEADER, TRACE_ID);
carrier.put(OtTracePropagator.SPAN_ID_HEADER, SHORT_SPAN_ID);
carrier.put(OtTracePropagator.SAMPLED_HEADER, Common.TRUE_INT);

assertThat(getSpanContext(propagator.extract(Context.current(), carrier, getter)))
.isEqualTo(
SpanContext.createFromRemoteParent(
TRACE_ID, SHORT_SPAN_ID_FULL, TraceFlags.getSampled(), TraceState.getDefault()));
}

@Test
void extract_SampledContext_Bool_Short_SpanId() {
Map<String, String> carrier = new LinkedHashMap<>();
carrier.put(OtTracePropagator.TRACE_ID_HEADER, TRACE_ID);
carrier.put(OtTracePropagator.SPAN_ID_HEADER, SHORT_SPAN_ID);
carrier.put(OtTracePropagator.SAMPLED_HEADER, "true");

assertThat(getSpanContext(propagator.extract(Context.current(), carrier, getter)))
.isEqualTo(
SpanContext.createFromRemoteParent(
TRACE_ID, SHORT_SPAN_ID_FULL, TraceFlags.getSampled(), TraceState.getDefault()));
}

@Test
void extract_NotSampledContext_Short_SpanId() {
Map<String, String> carrier = new LinkedHashMap<>();
carrier.put(OtTracePropagator.TRACE_ID_HEADER, TRACE_ID);
carrier.put(OtTracePropagator.SPAN_ID_HEADER, SHORT_SPAN_ID);
carrier.put(OtTracePropagator.SAMPLED_HEADER, Common.FALSE_INT);

assertThat(getSpanContext(propagator.extract(Context.current(), carrier, getter)))
.isEqualTo(
SpanContext.createFromRemoteParent(
TRACE_ID, SHORT_SPAN_ID_FULL, TraceFlags.getDefault(), TraceState.getDefault()));
}

@Test
void extract_InvalidTraceId() {
Map<String, String> invalidHeaders = new LinkedHashMap<>();
Expand Down

0 comments on commit 46b28eb

Please sign in to comment.