Skip to content

Commit

Permalink
Fix using WithSpan annotation in java6 class (#2699)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit committed Apr 2, 2021
1 parent 4d07aab commit 87950d1
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
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;
Expand All @@ -30,7 +31,7 @@ public static void onEnter(
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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand Down Expand Up @@ -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 {
}
}
}
}
}
}

0 comments on commit 87950d1

Please sign in to comment.