Hide the context keys in the BaseTracer. (#2105)

* Hide the context keys in the BaseTracer.
Provide the appropriate methods to get access.

* key needs to be static

* fix formatting

* more formatting, sigh

* remove an errant semicolon
This commit is contained in:
John Watson 2021-01-24 23:49:16 -08:00 committed by GitHub
parent 8b470bf378
commit 588f8847ed
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 22 additions and 28 deletions

View File

@ -33,12 +33,11 @@ public abstract class BaseTracer {
// 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.
public static final ContextKey<Span> CONTEXT_SERVER_SPAN_KEY =
private static final ContextKey<Span> CONTEXT_SERVER_SPAN_KEY =
ContextKey.named("opentelemetry-trace-server-span-key");
// Keeps track of the client span in a subtree corresponding to a client request.
// Visible for testing
public static final ContextKey<Span> CONTEXT_CLIENT_SPAN_KEY =
private static final ContextKey<Span> CONTEXT_CLIENT_SPAN_KEY =
ContextKey.named("opentelemetry-trace-auto-client-span-key");
protected final Tracer tracer;
@ -65,16 +64,18 @@ public abstract class BaseTracer {
return tracer.spanBuilder(spanName).setSpanKind(kind).startSpan();
}
// TODO (trask) make the key private
protected final boolean inClientSpan(Context parentContext) {
return parentContext.get(CONTEXT_CLIENT_SPAN_KEY) != null;
}
// TODO (trask) make the key private
protected final Context withClientSpan(Context parentContext, Span span) {
return parentContext.with(span).with(CONTEXT_CLIENT_SPAN_KEY, span);
}
protected final Context withServerSpan(Context parentContext, Span span) {
return parentContext.with(span).with(CONTEXT_SERVER_SPAN_KEY, span);
}
public Scope startScope(Span span) {
return Context.current().with(span).makeCurrent();
}

View File

@ -28,7 +28,7 @@ public abstract class DatabaseClientTracer<CONNECTION, QUERY> extends BaseTracer
}
public boolean shouldStartSpan(Context parentContext) {
return parentContext.get(CONTEXT_CLIENT_SPAN_KEY) == null;
return !inClientSpan(parentContext);
}
public Context startSpan(Context parentContext, CONNECTION connection, QUERY query) {
@ -48,7 +48,7 @@ public abstract class DatabaseClientTracer<CONNECTION, QUERY> extends BaseTracer
}
onStatement(span, normalizedQuery);
return parentContext.with(span).with(CONTEXT_CLIENT_SPAN_KEY, span);
return withClientSpan(parentContext, span);
}
@Override
@ -56,11 +56,6 @@ public abstract class DatabaseClientTracer<CONNECTION, QUERY> extends BaseTracer
return Span.current();
}
public Span getClientSpan() {
Context context = Context.current();
return context.get(CONTEXT_CLIENT_SPAN_KEY);
}
public void end(Context context) {
Span.fromContext(context).end();
}

View File

@ -81,7 +81,7 @@ public abstract class HttpServerTracer<REQUEST, RESPONSE, CONNECTION, STORAGE> e
onRequest(span, request);
onConnectionAndRequest(span, connection, request);
Context context = parentContext.with(CONTEXT_SERVER_SPAN_KEY, span).with(span);
Context context = withServerSpan(parentContext, span);
attachServerContext(context, storage);
return context;
@ -137,7 +137,7 @@ public abstract class HttpServerTracer<REQUEST, RESPONSE, CONNECTION, STORAGE> e
public Span getServerSpan(STORAGE storage) {
Context attachedContext = getServerContext(storage);
return attachedContext == null ? null : attachedContext.get(CONTEXT_SERVER_SPAN_KEY);
return attachedContext == null ? null : getCurrentServerSpan(attachedContext);
}
/**

View File

@ -24,7 +24,6 @@
package io.opentelemetry.javaagent.instrumentation.apachecamel.decorators;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.tracer.BaseTracer;
import io.opentelemetry.javaagent.instrumentation.apachecamel.CamelDirection;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
@ -87,7 +86,7 @@ class HttpSpanDecorator extends BaseSpanDecorator {
span.setAttribute(SemanticAttributes.HTTP_METHOD, getHttpMethod(exchange, endpoint));
Span serverSpan = Context.current().get(BaseTracer.CONTEXT_SERVER_SPAN_KEY);
Span serverSpan = BaseTracer.getCurrentServerSpan();
if (shouldUpdateServerSpanName(serverSpan, camelDirection)) {
updateServerSpanName(serverSpan, exchange, endpoint);
}

View File

@ -39,7 +39,7 @@ public class ApacheHttpAsyncClientTracer
.setSpanKind(CLIENT)
.setParent(parentContext)
.startSpan();
return parentContext.with(span).with(CONTEXT_CLIENT_SPAN_KEY, span);
return withClientSpan(parentContext, span);
}
@Override

View File

@ -159,9 +159,8 @@ public class AwsLambdaTracer extends BaseTracer {
/** Creates new scoped context with the given span. */
@Override
public Scope startScope(Span span) {
// TODO we could do this in one go, but TracingContextUtils.CONTEXT_SPAN_KEY is private
io.opentelemetry.context.Context newContext =
io.opentelemetry.context.Context.current().with(CONTEXT_SERVER_SPAN_KEY, span).with(span);
withServerSpan(io.opentelemetry.context.Context.current(), span);
return newContext.makeCurrent();
}

View File

@ -37,7 +37,7 @@ public class KubernetesClientTracer extends HttpClientTracer<Request, Request, R
.setAttribute("namespace", digest.getResourceMeta().getNamespace())
.setAttribute("name", digest.getResourceMeta().getName())
.startSpan();
Context context = parentContext.with(span).with(CONTEXT_CLIENT_SPAN_KEY, span);
Context context = withClientSpan(parentContext, span);
GlobalOpenTelemetry.getPropagators()
.getTextMapPropagator()
.inject(context, request, getSetter());

View File

@ -31,8 +31,7 @@ public class WithSpanTracer extends BaseTracer {
// don't create a nested SERVER span
return false;
}
if (kind == io.opentelemetry.api.trace.Span.Kind.CLIENT
&& context.get(CONTEXT_CLIENT_SPAN_KEY) != null) {
if (kind == io.opentelemetry.api.trace.Span.Kind.CLIENT && inClientSpan(context)) {
// don't create a nested CLIENT span
return false;
}
@ -47,10 +46,10 @@ public class WithSpanTracer extends BaseTracer {
io.opentelemetry.api.trace.Span span =
startSpan(spanNameForMethodWithAnnotation(applicationAnnotation, method), kind);
if (kind == io.opentelemetry.api.trace.Span.Kind.SERVER) {
context = context.with(CONTEXT_SERVER_SPAN_KEY, span);
return withServerSpan(context, span);
}
if (kind == io.opentelemetry.api.trace.Span.Kind.CLIENT) {
context = context.with(CONTEXT_CLIENT_SPAN_KEY, span);
return withClientSpan(context, span);
}
return context.with(span);
}

View File

@ -52,7 +52,8 @@ public class HandlerAdapterAdvice {
}
if (context != null) {
Span serverSpan = context.get(BaseTracer.CONTEXT_SERVER_SPAN_KEY);
Span serverSpan = BaseTracer.getCurrentServerSpan(context);
PathPattern bestPattern =
exchange.getAttribute(HandlerMapping.BEST_MATCHING_PATTERN_ATTRIBUTE);
if (serverSpan != null && bestPattern != null) {

View File

@ -43,7 +43,7 @@ public class RouteOnSuccessOrError implements BiConsumer<HandlerFunction<?>, Thr
span.setAttribute("spring-webflux.request.predicate", predicateString);
}
Span serverSpan = context.get(BaseTracer.CONTEXT_SERVER_SPAN_KEY);
Span serverSpan = BaseTracer.getCurrentServerSpan(context);
if (serverSpan != null) {
serverSpan.updateName(ServletContextPath.prepend(context, parseRoute(predicateString)));
}

View File

@ -29,7 +29,7 @@ public class TwilioTracer extends BaseTracer {
}
public boolean shouldStartSpan(Context parentContext) {
return parentContext.get(CONTEXT_CLIENT_SPAN_KEY) == null;
return !inClientSpan(parentContext);
}
public Context startSpan(Context parentContext, Object serviceExecutor, String methodName) {
@ -39,7 +39,7 @@ public class TwilioTracer extends BaseTracer {
.setSpanKind(CLIENT)
.setParent(parentContext)
.startSpan();
return parentContext.with(span).with(CONTEXT_CLIENT_SPAN_KEY, span);
return withClientSpan(parentContext, span);
}
/** Decorate trace based on service execution metadata. */