Fix using WithSpan annotation in java6 class (#2699)

This commit is contained in:
Lauri Tulmin 2021-04-02 19:34:27 +03:00 committed by GitHub
parent 4d07aab66d
commit 87950d1d32
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 67 additions and 1 deletions

View File

@ -8,6 +8,7 @@ dependencies {
compileOnly project(path: ':opentelemetry-ext-annotations-shaded-for-instrumenting', configuration: 'shadow')
testImplementation deps.opentelemetryExtAnnotations
testImplementation "net.bytebuddy:byte-buddy:${versions.bytebuddy}"
}
test {

View File

@ -11,6 +11,7 @@ import application.io.opentelemetry.extension.annotations.WithSpan;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge;
import java.lang.reflect.Method;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.implementation.bytecode.assign.Assigner;
@ -30,7 +31,7 @@ public class WithSpanAdvice {
WithSpan applicationAnnotation = method.getAnnotation(WithSpan.class);
SpanKind kind = tracer().extractSpanKind(applicationAnnotation);
Context current = Context.current();
Context current = Java8BytecodeBridge.currentContext();
// don't create a nested span if you're not supposed to.
if (tracer().shouldStartSpan(current, kind)) {

View File

@ -3,6 +3,9 @@
* SPDX-License-Identifier: Apache-2.0
*/
import io.opentelemetry.extension.annotations.WithSpan
import io.opentelemetry.instrumentation.test.utils.TraceUtils
import java.lang.reflect.Modifier
import java.util.concurrent.CompletableFuture
import static io.opentelemetry.api.trace.SpanKind.CLIENT
@ -12,6 +15,14 @@ import static io.opentelemetry.api.trace.SpanKind.SERVER
import io.opentelemetry.api.trace.SpanKind
import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.test.annotation.TracedWithSpan
import net.bytebuddy.ByteBuddy
import net.bytebuddy.ClassFileVersion
import net.bytebuddy.asm.MemberAttributeExtension
import net.bytebuddy.description.annotation.AnnotationDescription
import net.bytebuddy.implementation.MethodDelegation
import net.bytebuddy.implementation.bind.annotation.RuntimeType
import net.bytebuddy.implementation.bind.annotation.This
import net.bytebuddy.matcher.ElementMatchers
/**
* This test verifies that auto instrumentation supports {@link io.opentelemetry.extension.annotations.WithSpan} contrib annotation.
@ -370,4 +381,57 @@ class WithSpanInstrumentationTest extends AgentInstrumentationSpecification {
}
}
}
def "instrument java6 class"() {
setup:
/*
class GeneratedJava6TestClass implements Runnable {
@WithSpan
public void run() {
TraceUtils.runUnderTrace("intercept", {})
}
}
*/
Class<?> generatedClass = new ByteBuddy(ClassFileVersion.JAVA_V6)
.subclass(Object)
.name("GeneratedJava6TestClass")
.implement(Runnable)
.defineMethod("run", void.class, Modifier.PUBLIC).intercept(MethodDelegation.to(new Object() {
@RuntimeType
void intercept(@This Object o) {
TraceUtils.runUnderTrace("intercept", {})
}
}))
.visit(new MemberAttributeExtension.ForMethod()
.annotateMethod(AnnotationDescription.Builder.ofType(WithSpan).build())
.on(ElementMatchers.named("run")))
.make()
.load(getClass().getClassLoader())
.getLoaded()
Runnable runnable = (Runnable) generatedClass.getConstructor().newInstance()
runnable.run()
expect:
assertTraces(1) {
trace(0, 2) {
span(0) {
name "GeneratedJava6TestClass.run"
kind SpanKind.INTERNAL
hasNoParent()
errored false
attributes {
}
}
span(1) {
name "intercept"
kind SpanKind.INTERNAL
childOf(span(0))
errored false
attributes {
}
}
}
}
}
}