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 VerifyError with kotlin @WithSpan instrumentation #9313

Merged
merged 1 commit into from
Aug 26, 2023
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 @@ -41,16 +41,19 @@ public void visit(
public MethodVisitor visitMethod(
int access, String name, String descriptor, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
return new ExpandFramesMethodVisitor(mv, className, access, descriptor);
return new ExpandFramesMethodVisitor(mv, className, name, access, descriptor);
}

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

ExpandFramesMethodVisitor(MethodVisitor mv, String className, int access, String descriptor) {
ExpandFramesMethodVisitor(
MethodVisitor mv, String className, String methodName, int access, String descriptor) {
super(Opcodes.ASM9, mv);
if (!Modifier.isStatic(access)) {
if ("<init>".equals(methodName)) {
currentLocals.add(Opcodes.UNINITIALIZED_THIS);
} else if (!Modifier.isStatic(access)) {
currentLocals.add(className);
}
for (Type type : Type.getArgumentTypes(descriptor)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.kotlinxcoroutines

import io.opentelemetry.instrumentation.annotations.WithSpan
import kotlinx.coroutines.delay

class ClazzWithDefaultConstructorArguments(val name: String = "Ktor") {

@WithSpan
suspend fun sayHello(): String {
delay(10)
return "Hello World $name from ${ClazzWithDefaultConstructorArguments::class.simpleName}!"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,30 @@ class KotlinCoroutinesInstrumentationTest {
delay(10)
}

// regression test for https://github.com/open-telemetry/opentelemetry-java-instrumentation/issues/9312
@Test
fun `test class with default constructor argument`() {
runBlocking {
val classDefaultConstructorArguments = ClazzWithDefaultConstructorArguments()
classDefaultConstructorArguments.sayHello()
}

testing.waitAndAssertTraces(
{ trace ->
trace.hasSpansSatisfyingExactly(
{
it.hasName("ClazzWithDefaultConstructorArguments.sayHello")
.hasNoParent()
.hasAttributesSatisfyingExactly(
equalTo(SemanticAttributes.CODE_NAMESPACE, ClazzWithDefaultConstructorArguments::class.qualifiedName),
equalTo(SemanticAttributes.CODE_FUNCTION, "sayHello")
)
}
)
}
)
}

private fun tracedChild(opName: String) {
tracer.spanBuilder(opName).startSpan().end()
}
Expand Down