-
Notifications
You must be signed in to change notification settings - Fork 881
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add instrumentation for opentelemetry-extension-kotlin (#7341)
Hopefully resolves #7124 Our kotlin coroutine instrumentation relies on a shaded copy of `opentelemetry-extension-kotlin`. This doesn't work well when application also uses `opentelemetry-extension-kotlin`, because the shaded and unshaded copy store opentelemery context under different key. This pr attempts to fix this by instrumenting `opentelemetry-extension-kotlin` provided by the application so that it would delegate to the one shaded inside the agent. Co-authored-by: Mateusz Rzeszutek <[email protected]>
- Loading branch information
Showing
9 changed files
with
312 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
instrumentation/opentelemetry-extension-kotlin-1.0/javaagent/build.gradle.kts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
plugins { | ||
id("org.jetbrains.kotlin.jvm") | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group.set("io.opentelemetry") | ||
module.set("opentelemetry-extension-kotlin") | ||
versions.set("[0.17.0,)") | ||
assertInverse.set(true) | ||
skip("0.13.0") // has a bad dependency on non-alpha api-metric 0.13.0 | ||
extraDependency("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0") | ||
} | ||
} | ||
|
||
dependencies { | ||
compileOnly("org.jetbrains.kotlin:kotlin-stdlib-jdk8") | ||
library("io.opentelemetry:opentelemetry-extension-kotlin") | ||
// see the comment in opentelemetry-api-1.0.gradle for more details | ||
compileOnly(project(":opentelemetry-api-shaded-for-instrumenting", configuration = "shadow")) | ||
|
||
implementation(project(":instrumentation:opentelemetry-api:opentelemetry-api-1.0:javaagent")) | ||
|
||
testImplementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") | ||
testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0") | ||
} | ||
|
||
if (!(findProperty("testLatestDeps") as Boolean)) { | ||
// run tests against an early version of opentelemetry-extension-kotlin, latest dep tests will use | ||
// the current version | ||
configurations.configureEach { | ||
if (!name.contains("muzzle")) { | ||
resolutionStrategy { | ||
eachDependency { | ||
if (requested.group == "io.opentelemetry" && requested.name == "opentelemetry-extension-kotlin") { | ||
useVersion("1.0.0") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
tasks { | ||
withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile::class).configureEach { | ||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
} | ||
} | ||
} |
1 change: 1 addition & 0 deletions
1
instrumentation/opentelemetry-extension-kotlin-1.0/javaagent/gradle.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
kotlin.stdlib.default.dependency=false |
118 changes: 118 additions & 0 deletions
118
...ntelemetry/javaagent/instrumentation/extensionkotlin/ContextExtensionInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.extensionkotlin; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import application.io.opentelemetry.context.Context; | ||
import io.opentelemetry.extension.kotlin.ContextExtensionsKt; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage; | ||
import kotlin.coroutines.CoroutineContext; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class ContextExtensionInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return named("application.io.opentelemetry.extension.kotlin.ContextExtensionsKt"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
named("asContextElement") | ||
.and(takesArgument(0, named("application.io.opentelemetry.context.Context"))), | ||
this.getClass().getName() + "$ContextAdvice"); | ||
|
||
transformer.applyAdviceToMethod( | ||
named("asContextElement") | ||
.and( | ||
takesArgument( | ||
0, named("application.io.opentelemetry.context.ImplicitContextKeyed"))), | ||
this.getClass().getName() + "$ImplicitContextKeyedAdvice"); | ||
|
||
transformer.applyAdviceToMethod( | ||
named("getOpenTelemetryContext") | ||
.and(takesArgument(0, named("kotlin.coroutines.CoroutineContext"))), | ||
this.getClass().getName() + "$GetOpenTelemetryContextAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ContextAdvice { | ||
|
||
@Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class) | ||
public static CoroutineContext enter(@Advice.Argument(0) Context applicationContext) { | ||
if (applicationContext != null) { | ||
io.opentelemetry.context.Context agentContext = | ||
AgentContextStorage.getAgentContext(applicationContext); | ||
return ContextExtensionsKt.asContextElement(agentContext); | ||
} | ||
return null; | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class) | ||
public static void onExit( | ||
@Advice.Return(readOnly = false) CoroutineContext result, | ||
@Advice.Enter CoroutineContext coroutineContext) { | ||
if (coroutineContext != null) { | ||
result = coroutineContext; | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ImplicitContextKeyedAdvice { | ||
|
||
@Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class) | ||
public static CoroutineContext enter( | ||
@Advice.Argument(0) | ||
application.io.opentelemetry.context.ImplicitContextKeyed implicitContextKeyed) { | ||
if (implicitContextKeyed != null) { | ||
Context applicationContext = Context.current().with(implicitContextKeyed); | ||
io.opentelemetry.context.Context agentContext = | ||
AgentContextStorage.getAgentContext(applicationContext); | ||
return ContextExtensionsKt.asContextElement(agentContext); | ||
} | ||
return null; | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class) | ||
public static void onExit( | ||
@Advice.Return(readOnly = false) CoroutineContext result, | ||
@Advice.Enter CoroutineContext coroutineContext) { | ||
if (coroutineContext != null) { | ||
result = coroutineContext; | ||
} | ||
} | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class GetOpenTelemetryContextAdvice { | ||
|
||
@Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class) | ||
public static Context enter(@Advice.Argument(0) CoroutineContext coroutineContext) { | ||
if (coroutineContext != null) { | ||
io.opentelemetry.context.Context agentContext = | ||
ContextExtensionsKt.getOpenTelemetryContext(coroutineContext); | ||
return AgentContextStorage.toApplicationContext(agentContext); | ||
} | ||
return null; | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class) | ||
public static void onExit( | ||
@Advice.Return(readOnly = false) Context result, @Advice.Enter Context context) { | ||
if (context != null) { | ||
result = context; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.