Skip to content

Commit fad1820

Browse files
authored
Fix VerifyError with kotlin @WithSpan instrumentation (#9313)
1 parent d7a34f9 commit fad1820

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

instrumentation/kotlinx-coroutines/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/kotlinxcoroutines/instrumentationannotations/ExpandFramesClassVisitor.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,19 @@ public void visit(
4141
public MethodVisitor visitMethod(
4242
int access, String name, String descriptor, String signature, String[] exceptions) {
4343
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
44-
return new ExpandFramesMethodVisitor(mv, className, access, descriptor);
44+
return new ExpandFramesMethodVisitor(mv, className, name, access, descriptor);
4545
}
4646

4747
private static class ExpandFramesMethodVisitor extends MethodVisitor {
4848
final List<Object> currentLocals = new ArrayList<>();
4949
final List<Object> currentStack = new ArrayList<>();
5050

51-
ExpandFramesMethodVisitor(MethodVisitor mv, String className, int access, String descriptor) {
51+
ExpandFramesMethodVisitor(
52+
MethodVisitor mv, String className, String methodName, int access, String descriptor) {
5253
super(Opcodes.ASM9, mv);
53-
if (!Modifier.isStatic(access)) {
54+
if ("<init>".equals(methodName)) {
55+
currentLocals.add(Opcodes.UNINITIALIZED_THIS);
56+
} else if (!Modifier.isStatic(access)) {
5457
currentLocals.add(className);
5558
}
5659
for (Type type : Type.getArgumentTypes(descriptor)) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.kotlinxcoroutines
7+
8+
import io.opentelemetry.instrumentation.annotations.WithSpan
9+
import kotlinx.coroutines.delay
10+
11+
class ClazzWithDefaultConstructorArguments(val name: String = "Ktor") {
12+
13+
@WithSpan
14+
suspend fun sayHello(): String {
15+
delay(10)
16+
return "Hello World $name from ${ClazzWithDefaultConstructorArguments::class.simpleName}!"
17+
}
18+
}

instrumentation/kotlinx-coroutines/javaagent/src/test/kotlin/io/opentelemetry/javaagent/instrumentation/kotlinxcoroutines/KotlinCoroutinesInstrumentationTest.kt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,30 @@ class KotlinCoroutinesInstrumentationTest {
559559
delay(10)
560560
}
561561

562+
// regression test for https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/9312
563+
@Test
564+
fun `test class with default constructor argument`() {
565+
runBlocking {
566+
val classDefaultConstructorArguments = ClazzWithDefaultConstructorArguments()
567+
classDefaultConstructorArguments.sayHello()
568+
}
569+
570+
testing.waitAndAssertTraces(
571+
{ trace ->
572+
trace.hasSpansSatisfyingExactly(
573+
{
574+
it.hasName("ClazzWithDefaultConstructorArguments.sayHello")
575+
.hasNoParent()
576+
.hasAttributesSatisfyingExactly(
577+
equalTo(SemanticAttributes.CODE_NAMESPACE, ClazzWithDefaultConstructorArguments::class.qualifiedName),
578+
equalTo(SemanticAttributes.CODE_FUNCTION, "sayHello")
579+
)
580+
}
581+
)
582+
}
583+
)
584+
}
585+
562586
private fun tracedChild(opName: String) {
563587
tracer.spanBuilder(opName).startSpan().end()
564588
}

0 commit comments

Comments
 (0)