Skip to content

Commit

Permalink
Convert the last two decorators to tracers and delete BaseDecorator (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
trask committed Nov 28, 2020
1 parent 66b4088 commit b683c2f
Show file tree
Hide file tree
Showing 35 changed files with 179 additions and 816 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package io.opentelemetry.instrumentation.api.tracer;

import static io.opentelemetry.api.OpenTelemetry.getGlobalPropagators;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.api.trace.Span.Kind;
Expand All @@ -13,12 +15,23 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.context.ContextKey;
import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.instrumentation.api.InstrumentationVersion;
import io.opentelemetry.instrumentation.api.context.ContextPropagationDebug;
import java.lang.reflect.Method;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class BaseTracer {
private static final Logger log = LoggerFactory.getLogger(HttpServerTracer.class);

private static final boolean FAIL_ON_CONTEXT_LEAK =
Boolean.getBoolean("otel.internal.failOnContextLeak");

// Keeps track of the server span for the current trace.
// TODO(anuraaga): Should probably be renamed to local root key since it could be a consumer span
// or other non-server root.
Expand Down Expand Up @@ -143,6 +156,46 @@ public void addThrowable(Span span, Throwable throwable) {
span.recordException(throwable);
}

public static <C> Context extract(C carrier, TextMapPropagator.Getter<C> getter) {
if (ContextPropagationDebug.isThreadPropagationDebuggerEnabled()) {
debugContextLeak();
}
// Using Context.ROOT here may be quite unexpected, but the reason is simple.
// We want either span context extracted from the carrier or invalid one.
// We DO NOT want any span context potentially lingering in the current context.
return getGlobalPropagators().getTextMapPropagator().extract(Context.root(), carrier, getter);
}

private static void debugContextLeak() {
Context current = Context.current();
if (current != Context.root()) {
log.error("Unexpected non-root current context found when extracting remote context!");
Span currentSpan = Span.fromContextOrNull(current);
if (currentSpan != null) {
log.error("It contains this span: {}", currentSpan);
}
List<StackTraceElement[]> locations = ContextPropagationDebug.getLocations(current);
if (locations != null) {
StringBuilder sb = new StringBuilder();
Iterator<StackTraceElement[]> i = locations.iterator();
while (i.hasNext()) {
for (StackTraceElement ste : i.next()) {
sb.append("\n");
sb.append(ste);
}
if (i.hasNext()) {
sb.append("\nwhich was propagated from:");
}
}
log.error("a context leak was detected. it was propagated from:{}", sb);
}

if (FAIL_ON_CONTEXT_LEAK) {
throw new IllegalStateException("Context leak detected");
}
}
}

/** Returns span of type SERVER from the current context or <code>null</code> if not found. */
public static Span getCurrentServerSpan() {
return getCurrentServerSpan(Context.current());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import io.opentelemetry.context.Scope;
import io.opentelemetry.context.propagation.TextMapPropagator;
import io.opentelemetry.context.propagation.TextMapPropagator.Setter;
import io.opentelemetry.instrumentation.api.decorator.HttpStatusConverter;
import io.opentelemetry.instrumentation.api.tracer.utils.NetPeerUtils;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down
Loading

0 comments on commit b683c2f

Please sign in to comment.