Call shouldStart for Spring handler span (#3793)

* Call shouldStart for Spring handler span

* Update server span name even if shouldStart returns false
This commit is contained in:
Trask Stalnaker 2021-08-09 09:22:43 -07:00 committed by GitHub
parent 3c3e0f2beb
commit 842ab42136
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 15 deletions

View File

@ -16,11 +16,9 @@ import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming;
import io.opentelemetry.instrumentation.api.tracer.ServerSpan;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
@ -66,21 +64,27 @@ public class HandlerAdapterInstrumentation implements TypeInstrumentation {
// skip creating handler span for grails, grails instrumentation will take care of it
return;
}
Context parentContext = Java8BytecodeBridge.currentContext();
Span serverSpan = ServerSpan.fromContextOrNull(parentContext);
// TODO (trask) is it important to check serverSpan != null here?
if (serverSpan != null) {
// Name the parent span based on the matching pattern
ServerSpanNaming.updateServerSpanName(
parentContext,
CONTROLLER,
SpringWebMvcServerSpanNaming.getServerSpanNameSupplier(parentContext, request));
// Now create a span for handler/controller execution.
context = handlerInstrumenter().start(parentContext, handler);
if (context != null) {
scope = context.makeCurrent();
}
// don't start a new top-level span
if (!Java8BytecodeBridge.spanFromContext(parentContext).getSpanContext().isValid()) {
return;
}
// Name the parent span based on the matching pattern
ServerSpanNaming.updateServerSpanName(
parentContext,
CONTROLLER,
SpringWebMvcServerSpanNaming.getServerSpanNameSupplier(parentContext, request));
if (!handlerInstrumenter().shouldStart(parentContext, handler)) {
return;
}
// Now create a span for handler/controller execution.
context = handlerInstrumenter().start(parentContext, handler);
scope = context.makeCurrent();
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)