-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce LocalRootSpan (replacing ServerSpan) (open-telemetry#5896)
* Introduce LocalRootSpan (replacing ServerSpan) * fix tests * bridge old ServerSpan class
- Loading branch information
Showing
24 changed files
with
299 additions
and
85 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
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
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
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
57 changes: 57 additions & 0 deletions
57
...on-api/src/main/java/io/opentelemetry/instrumentation/api/instrumenter/LocalRootSpan.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,57 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.instrumenter; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.SpanContext; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.ContextKey; | ||
import javax.annotation.Nullable; | ||
|
||
/** | ||
* A local root span is a span that either does not have a parent span (it is the root span of a | ||
* trace), or its parent span is a remote span (context was propagated from another application). | ||
*/ | ||
public final class LocalRootSpan { | ||
|
||
private static final ContextKey<Span> KEY = | ||
ContextKey.named("opentelemetry-traces-local-root-span"); | ||
|
||
/** | ||
* Returns the local root span from the current context or {@linkplain Span#getInvalid() invalid | ||
* span} if there is no local root span. | ||
*/ | ||
public static Span current() { | ||
return fromContext(Context.current()); | ||
} | ||
|
||
/** | ||
* Returns the local root span from the given context or {@linkplain Span#getInvalid() invalid | ||
* span} if there is no local root span in the context. | ||
*/ | ||
public static Span fromContext(Context context) { | ||
Span span = fromContextOrNull(context); | ||
return span == null ? Span.getInvalid() : span; | ||
} | ||
|
||
/** | ||
* Returns the local root span from the given context or {@code null} if there is no local root | ||
* span in the context. | ||
*/ | ||
@Nullable | ||
public static Span fromContextOrNull(Context context) { | ||
return context.get(KEY); | ||
} | ||
|
||
static boolean isLocalRoot(Context parentContext) { | ||
SpanContext spanContext = Span.fromContext(parentContext).getSpanContext(); | ||
return !spanContext.isValid() || spanContext.isRemote(); | ||
} | ||
|
||
static Context store(Context context, Span span) { | ||
return context.with(KEY, span); | ||
} | ||
} |
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
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
85 changes: 85 additions & 0 deletions
85
...pi/src/test/java/io/opentelemetry/instrumentation/api/instrumenter/LocalRootSpanTest.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,85 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.api.instrumenter; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNull; | ||
import static org.junit.jupiter.api.Assertions.assertSame; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.api.trace.SpanContext; | ||
import io.opentelemetry.api.trace.TraceFlags; | ||
import io.opentelemetry.api.trace.TraceState; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class LocalRootSpanTest { | ||
|
||
@Test | ||
void spanWithoutParentIsLocalRoot() { | ||
Context parentContext = Context.root(); | ||
assertTrue(LocalRootSpan.isLocalRoot(parentContext)); | ||
} | ||
|
||
@Test | ||
void spanWithInvalidParentIsLocalRoot() { | ||
Context parentContext = Context.root().with(Span.getInvalid()); | ||
assertTrue(LocalRootSpan.isLocalRoot(parentContext)); | ||
} | ||
|
||
@Test | ||
void spanWithRemoteParentIsLocalRoot() { | ||
Context parentContext = | ||
Context.root() | ||
.with( | ||
Span.wrap( | ||
SpanContext.createFromRemoteParent( | ||
"00000000000000000000000000000001", | ||
"0000000000000002", | ||
TraceFlags.getSampled(), | ||
TraceState.getDefault()))); | ||
assertTrue(LocalRootSpan.isLocalRoot(parentContext)); | ||
} | ||
|
||
@Test | ||
void spanWithValidLocalParentIsNotLocalRoot() { | ||
Context parentContext = | ||
Context.root() | ||
.with( | ||
Span.wrap( | ||
SpanContext.create( | ||
"00000000000000000000000000000001", | ||
"0000000000000002", | ||
TraceFlags.getSampled(), | ||
TraceState.getDefault()))); | ||
assertFalse(LocalRootSpan.isLocalRoot(parentContext)); | ||
} | ||
|
||
@Test | ||
void shouldGetLocalRootSpan() { | ||
Span span = Span.getInvalid(); | ||
Context context = LocalRootSpan.store(Context.root(), span); | ||
|
||
try (Scope ignored = context.makeCurrent()) { | ||
assertSame(span, LocalRootSpan.current()); | ||
} | ||
assertSame(span, LocalRootSpan.fromContext(context)); | ||
assertSame(span, LocalRootSpan.fromContextOrNull(context)); | ||
} | ||
|
||
@Test | ||
void shouldNotGetLocalRootSpanIfThereIsNone() { | ||
Context context = Context.root(); | ||
|
||
try (Scope ignored = context.makeCurrent()) { | ||
assertFalse(LocalRootSpan.current().getSpanContext().isValid()); | ||
} | ||
assertFalse(LocalRootSpan.fromContext(context).getSpanContext().isValid()); | ||
assertNull(LocalRootSpan.fromContextOrNull(context)); | ||
} | ||
} |
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
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
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
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
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
Oops, something went wrong.