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

Assumes that INVALID parent span id is actually null in TraceContext #720

Merged
merged 2 commits into from
May 24, 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 @@ -15,16 +15,16 @@
*/
package io.micrometer.tracing.otel.bridge;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

import io.micrometer.common.lang.Nullable;
import io.micrometer.tracing.TraceContext;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.SpanContext;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.trace.ReadableSpan;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicReference;

/**
* OpenTelemetry implementation of a {@link TraceContext}.
*
Expand Down Expand Up @@ -116,7 +116,11 @@ public String parentId() {
: this.span;
if (spanContextSpanOrSpan instanceof ReadableSpan) {
ReadableSpan readableSpan = (ReadableSpan) spanContextSpanOrSpan;
return readableSpan.toSpanData().getParentSpanId();
String parentSpanId = readableSpan.toSpanData().getParentSpanId();
if (Objects.equals(Span.getInvalid().getSpanContext().getSpanId(), parentSpanId)) {
return null;
}
return parentSpanId;
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/**
* Copyright 2024 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
* in compliance with the License. You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software distributed under the License
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
* or implied. See the License for the specific language governing permissions and limitations under
* the License.
*/
package io.micrometer.tracing.otel.bridge;

import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Tracer;
import io.opentelemetry.context.Context;
import io.opentelemetry.sdk.OpenTelemetrySdk;
import io.opentelemetry.sdk.OpenTelemetrySdkBuilder;
import io.opentelemetry.sdk.trace.SdkTracerProvider;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.BDDAssertions.then;

class OtelTraceContextTests {

SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
.setSampler(io.opentelemetry.sdk.trace.samplers.Sampler.alwaysOn())
Copy link
Member

Choose a reason for hiding this comment

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

Since the result in the original issue was dependent on the sampling decision, should we double the tests and test sampled and not sampled scenarios as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You're right. The problem is that with sampler e.g. always off we can't get the parent id from OTel even though we should.

 @Test
    void should_return_parentid_when_spans_not_sampled() {
        SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
            .setSampler(io.opentelemetry.sdk.trace.samplers.Sampler.alwaysOff())
            .build();

        OpenTelemetrySdkBuilder openTelemetrySdkBuilder = OpenTelemetrySdk.builder().setTracerProvider(sdkTracerProvider);

        try (OpenTelemetrySdk openTelemetrySdk = openTelemetrySdkBuilder.build()) {
            Tracer otelTracer = tracer(openTelemetrySdk);
            Span parentSpan = otelTracer.spanBuilder("parent").startSpan();
            Span span = otelTracer.spanBuilder("foo")
                .setParent(parentSpan.storeInContext(Context.current()))
                .startSpan();

            OtelTraceContext otelTraceContext = new OtelTraceContext(span);

            then(otelTraceContext.parentId()).isEqualTo(parentSpan.getSpanContext().getSpanId());
        }

    }

This will not pass. I need to figure out how to solve this

Copy link
Member

Choose a reason for hiding this comment

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

we can't get the parent id from OTel even though we should

According to OTel we should never be able to get the parentId "instrumentation time", only in the processors/exporters by looking at the span tree. I think in that case it might be ok to return and assert null as we did earlier since that's the best I think we can do.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This would work differently for Brave and OTel then I guess?

.build();

OpenTelemetrySdkBuilder openTelemetrySdkBuilder = OpenTelemetrySdk.builder().setTracerProvider(sdkTracerProvider);

@Test
void should_return_null_when_parent_invalid() {

try (OpenTelemetrySdk openTelemetrySdk = openTelemetrySdkBuilder.build()) {
Tracer otelTracer = tracer(openTelemetrySdk);
Span span = otelTracer.spanBuilder("foo").startSpan();

OtelTraceContext otelTraceContext = new OtelTraceContext(span);

then(otelTraceContext.parentId()).isNull();
}

}

@Test
void should_return_null_when_spans_not_sampled() { // works differently than Brave
SdkTracerProvider sdkTracerProvider = SdkTracerProvider.builder()
.setSampler(io.opentelemetry.sdk.trace.samplers.Sampler.alwaysOff())
.build();

OpenTelemetrySdkBuilder openTelemetrySdkBuilder = OpenTelemetrySdk.builder()
.setTracerProvider(sdkTracerProvider);

try (OpenTelemetrySdk openTelemetrySdk = openTelemetrySdkBuilder.build()) {
Tracer otelTracer = tracer(openTelemetrySdk);
Span parentSpan = otelTracer.spanBuilder("parent").startSpan();
Span span = otelTracer.spanBuilder("foo")
.setParent(parentSpan.storeInContext(Context.current()))
.startSpan();

OtelTraceContext otelTraceContext = new OtelTraceContext(span);

then(otelTraceContext.parentId()).isNull();
}

}

@Test
void should_return_parentid_when_parent_valid() {
try (OpenTelemetrySdk openTelemetrySdk = openTelemetrySdkBuilder.build()) {
Tracer otelTracer = tracer(openTelemetrySdk);
Span parentSpan = otelTracer.spanBuilder("parent").startSpan();
Span span = otelTracer.spanBuilder("foo")
.setParent(parentSpan.storeInContext(Context.current()))
.startSpan();

OtelTraceContext otelTraceContext = new OtelTraceContext(span);

then(otelTraceContext.parentId()).isEqualTo(parentSpan.getSpanContext().getSpanId());
}

}

private static Tracer tracer(OpenTelemetrySdk openTelemetrySdk) {
return openTelemetrySdk.getTracer("io.micrometer.micrometer-tracing");
}

}