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

Store SpanShim/Baggage in the Context. #2982

Merged
merged 3 commits into from
Aug 3, 2021
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 @@ -5,11 +5,15 @@

package io.opentelemetry.opentracingshim;

import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentracing.Scope;
import io.opentracing.ScopeManager;
import io.opentracing.Span;

final class ScopeManagerShim extends BaseShimObject implements ScopeManager {
private static final ContextKey<SpanShim> SPAN_SHIM_KEY =
ContextKey.named("opentracing-shim-key");

public ScopeManagerShim(TelemetryInfo telemetryInfo) {
super(telemetryInfo);
Expand All @@ -18,22 +22,31 @@ public ScopeManagerShim(TelemetryInfo telemetryInfo) {
@Override
@SuppressWarnings("ReturnMissingNullable")
public Span activeSpan() {
Context context = Context.current();
io.opentelemetry.api.trace.Span span = io.opentelemetry.api.trace.Span.fromContext(context);
SpanShim spanShim = context.get(SPAN_SHIM_KEY);
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved

// As OpenTracing simply returns null when no active instance is available,
// we need to do map an invalid OpenTelemetry span to null here.
io.opentelemetry.api.trace.Span span = io.opentelemetry.api.trace.Span.current();
if (!span.getSpanContext().isValid()) {
return null;
}

// TODO: Properly include the bagagge/distributedContext.
// If there's a SpanShim for the active Span, simply return it.
if (spanShim != null && spanShim.getSpan() == span) {
return spanShim;
}

// Span was activated from outside the Shim layer unfortunately.
return new SpanShim(telemetryInfo(), span);
}

@Override
@SuppressWarnings("MustBeClosedChecker")
public Scope activate(Span span) {
io.opentelemetry.api.trace.Span actualSpan = getActualSpan(span);
return new ScopeShim(actualSpan.makeCurrent());
Context context = Context.current().with(actualSpan).with(SPAN_SHIM_KEY, (SpanShim) span);
carlosalberto marked this conversation as resolved.
Show resolved Hide resolved
return new ScopeShim(context.makeCurrent());
}

static io.opentelemetry.api.trace.Span getActualSpan(Span span) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,23 @@ void activateSpan() {
assertThat(tracerShim.scopeManager().activeSpan()).isNull();
}

@Test
void activateSpan_withoutShim() {
// Create and activate a Span without the Shim layer, in order to verify
// we keep on working as expected (even if not as efficiently).
io.opentelemetry.api.trace.Span span =
otelTesting.getOpenTelemetry().getTracer("opentracingshim").spanBuilder("one").startSpan();
try (io.opentelemetry.context.Scope scope = span.makeCurrent()) {
assertThat(tracerShim.activeSpan()).isNotNull();
assertThat(tracerShim.scopeManager().activeSpan()).isNotNull();
assertThat(((SpanShim) tracerShim.activeSpan()).getSpan()).isEqualTo(span);
assertThat(((SpanShim) tracerShim.scopeManager().activeSpan()).getSpan()).isEqualTo(span);
}

assertThat(tracerShim.activeSpan()).isNull();
assertThat(tracerShim.scopeManager().activeSpan()).isNull();
}

@Test
void extract_nullContext() {
SpanContext result =
Expand Down