Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix using WithSpan annotation in java6 class #2699

Merged
merged 2 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Comment on lines +386 to +410
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, I would have found a (random old) library that was compiled to Java 6 and tested instrumentation of that library, but this is better 👍


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 {
}
}
}
}
}
}