-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Assumes that INVALID parent span id is actually null in TraceContext
- without this change we're returning an INVALID parent span even though we define in the javadocs of TraceContext that we would return null - with this change we're verifying whether the INVALID parent is set in which case we're returning null fixes gh-687
- Loading branch information
1 parent
43835f1
commit 7980543
Showing
2 changed files
with
76 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
...ng-bridge-otel/src/test/java/io/micrometer/tracing/otel/bridge/OtelTraceContextTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/** | ||
* 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()) | ||
.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_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"); | ||
} | ||
|
||
} |