-
Notifications
You must be signed in to change notification settings - Fork 857
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
Add async operation end strategy for kotlin coroutines flow #11168
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🥲 |
||
} | ||
} |
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍