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 1 commit
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
Loading