-
Notifications
You must be signed in to change notification settings - Fork 859
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add async operation end strategy for kotlin coroutines flow (#11168)
- Loading branch information
Showing
26 changed files
with
487 additions
and
138 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 was deleted.
Oops, something went wrong.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
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
23 changes: 23 additions & 0 deletions
23
...entation/kotlinx-coroutines/kotlinx-coroutines-flow-1.3/javaagent-kotlin/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,23 @@ | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
// We are using a separate module for kotlin source instead of placing them in | ||
// instrumentation/kotlinx-coroutines/kotlinx-coroutines-flow-1.3/javaagent because muzzle | ||
// generation plugin currently doesn't handle kotlin sources correctly. | ||
plugins { | ||
id("org.jetbrains.kotlin.jvm") | ||
id("otel.java-conventions") | ||
} | ||
|
||
dependencies { | ||
compileOnly("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0") | ||
compileOnly("org.jetbrains.kotlin:kotlin-stdlib-jdk8") | ||
compileOnly(project(":instrumentation-api")) | ||
} | ||
|
||
tasks { | ||
withType(KotlinCompile::class).configureEach { | ||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
} | ||
} | ||
} |
17 changes: 17 additions & 0 deletions
17
...main/kotlin/io/opentelemetry/javaagent/instrumentation/kotlinxcoroutines/flow/FlowUtil.kt
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,17 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.kotlinxcoroutines.flow | ||
|
||
import io.opentelemetry.context.Context | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter | ||
import kotlinx.coroutines.flow.Flow | ||
import kotlinx.coroutines.flow.onCompletion | ||
|
||
fun <REQUEST, RESPONSE> onComplete(flow: Flow<*>, instrumenter: Instrumenter<REQUEST, RESPONSE>, context: Context, request: REQUEST): Flow<*> { | ||
return flow.onCompletion { cause: Throwable? -> | ||
instrumenter.end(context, request, null, cause) | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
instrumentation/kotlinx-coroutines/kotlinx-coroutines-flow-1.3/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,48 @@ | ||
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile | ||
|
||
plugins { | ||
id("org.jetbrains.kotlin.jvm") | ||
id("otel.javaagent-instrumentation") | ||
} | ||
|
||
muzzle { | ||
pass { | ||
group.set("org.jetbrains.kotlinx") | ||
module.set("kotlinx-coroutines-core") | ||
versions.set("[1.3.0,1.3.8)") | ||
} | ||
// 1.3.9 (and beyond?) have changed how artifact names are resolved due to multiplatform variants | ||
pass { | ||
group.set("org.jetbrains.kotlinx") | ||
module.set("kotlinx-coroutines-core-jvm") | ||
versions.set("[1.3.9,)") | ||
} | ||
} | ||
|
||
dependencies { | ||
library("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.0") | ||
compileOnly(project(":instrumentation-annotations-support")) | ||
implementation(project(":instrumentation:kotlinx-coroutines:kotlinx-coroutines-flow-1.3:javaagent-kotlin")) | ||
|
||
testInstrumentation(project(":instrumentation:kotlinx-coroutines:kotlinx-coroutines-1.0:javaagent")) | ||
testInstrumentation(project(":instrumentation:opentelemetry-extension-kotlin-1.0:javaagent")) | ||
testInstrumentation(project(":instrumentation:reactor:reactor-3.1:javaagent")) | ||
|
||
testImplementation("io.opentelemetry:opentelemetry-extension-kotlin") | ||
testImplementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8") | ||
testImplementation(project(":instrumentation:reactor:reactor-3.1:library")) | ||
testImplementation(project(":instrumentation-annotations")) | ||
|
||
testLibrary("org.jetbrains.kotlinx:kotlinx-coroutines-reactor:1.3.0") | ||
} | ||
|
||
tasks { | ||
withType(KotlinCompile::class).configureEach { | ||
kotlinOptions { | ||
jvmTarget = "1.8" | ||
} | ||
} | ||
withType<Test>().configureEach { | ||
jvmArgs("-Dio.opentelemetry.javaagent.shaded.io.opentelemetry.context.enableStrictContext=false") | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
...lemetry/javaagent/instrumentation/kotlinxcoroutines/flow/AbstractFlowInstrumentation.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,38 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.kotlinxcoroutines.flow; | ||
|
||
import static net.bytebuddy.matcher.ElementMatchers.isConstructor; | ||
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; | ||
|
||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class AbstractFlowInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return namedOneOf("kotlinx.coroutines.flow.AbstractFlow", "kotlinx.coroutines.flow.SafeFlow"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
isConstructor(), this.getClass().getName() + "$ConstructorAdvice"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ConstructorAdvice { | ||
|
||
@Advice.OnMethodEnter | ||
public static void enter() { | ||
FlowInstrumentationHelper.initialize(); | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...telemetry/javaagent/instrumentation/kotlinxcoroutines/flow/FlowInstrumentationHelper.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,44 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.kotlinxcoroutines.flow; | ||
|
||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.instrumentation.api.annotation.support.async.AsyncOperationEndStrategies; | ||
import io.opentelemetry.instrumentation.api.annotation.support.async.AsyncOperationEndStrategy; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import kotlinx.coroutines.flow.Flow; | ||
|
||
public final class FlowInstrumentationHelper { | ||
private static final FlowAsyncOperationEndStrategy asyncOperationEndStrategy = | ||
new FlowAsyncOperationEndStrategy(); | ||
|
||
static { | ||
AsyncOperationEndStrategies.instance().registerStrategy(asyncOperationEndStrategy); | ||
} | ||
|
||
public static void initialize() {} | ||
|
||
private FlowInstrumentationHelper() {} | ||
|
||
private static final class FlowAsyncOperationEndStrategy implements AsyncOperationEndStrategy { | ||
|
||
@Override | ||
public boolean supports(Class<?> returnType) { | ||
return Flow.class.isAssignableFrom(returnType); | ||
} | ||
|
||
@Override | ||
public <REQUEST, RESPONSE> Object end( | ||
Instrumenter<REQUEST, RESPONSE> instrumenter, | ||
Context context, | ||
REQUEST request, | ||
Object asyncValue, | ||
Class<RESPONSE> responseType) { | ||
Flow<?> flow = (Flow<?>) asyncValue; | ||
return FlowUtilKt.onComplete(flow, instrumenter, context, request); | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...ent/instrumentation/kotlinxcoroutines/flow/KotlinCoroutinesFlowInstrumentationModule.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,26 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.kotlinxcoroutines.flow; | ||
|
||
import static java.util.Collections.singletonList; | ||
|
||
import com.google.auto.service.AutoService; | ||
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import java.util.List; | ||
|
||
@AutoService(InstrumentationModule.class) | ||
public class KotlinCoroutinesFlowInstrumentationModule extends InstrumentationModule { | ||
|
||
public KotlinCoroutinesFlowInstrumentationModule() { | ||
super("kotlinx-coroutines", "kotlinx-coroutines-flow"); | ||
} | ||
|
||
@Override | ||
public List<TypeInstrumentation> typeInstrumentations() { | ||
return singletonList(new AbstractFlowInstrumentation()); | ||
} | ||
} |
Oops, something went wrong.