Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ottracepropagation for short span ids #6734

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add unit tests for this? Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done. added unit tests. Thanks for review.


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
Loading