-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Allow reading otel context from reactor ContextView #11235
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
79eda09
Allow reading otel context from reactor ContextView
laurit 8755830
move instrumentation to separate module
laurit 9b8b9cd
fix build failure
laurit 5202771
improve test
laurit 2d53bd1
Update instrumentation/reactor/reactor-3.1/javaagent/build.gradle.kts
laurit 8745806
add comment
laurit 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
34 changes: 34 additions & 0 deletions
34
instrumentation/reactor/reactor-3.4/javaagent/build.gradle.kts
This file contains hidden or 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,34 @@ | ||
| plugins { | ||
| id("otel.javaagent-instrumentation") | ||
| } | ||
|
|
||
| muzzle { | ||
| pass { | ||
| group.set("io.projectreactor") | ||
| module.set("reactor-core") | ||
| versions.set("[3.4.0,)") | ||
| extraDependency("io.opentelemetry:opentelemetry-api:1.0.0") | ||
| assertInverse.set(true) | ||
| excludeInstrumentationName("opentelemetry-api") | ||
| } | ||
| } | ||
|
|
||
| dependencies { | ||
| library("io.projectreactor:reactor-core:3.4.0") | ||
| implementation(project(":instrumentation:reactor:reactor-3.1:library")) | ||
|
|
||
| implementation(project(":instrumentation:opentelemetry-api:opentelemetry-api-1.0:javaagent")) | ||
|
|
||
| compileOnly(project(":javaagent-tooling")) | ||
| compileOnly(project(":instrumentation-annotations-support")) | ||
| compileOnly(project(":opentelemetry-api-shaded-for-instrumenting", configuration = "shadow")) | ||
|
|
||
| testInstrumentation(project(":instrumentation:reactor:reactor-3.1:javaagent")) | ||
| testInstrumentation(project(":instrumentation:opentelemetry-extension-annotations-1.0:javaagent")) | ||
|
|
||
| testLibrary("io.projectreactor:reactor-test:3.1.0.RELEASE") | ||
| testImplementation(project(":instrumentation-annotations-support-testing")) | ||
| testImplementation(project(":instrumentation:reactor:reactor-3.1:testing")) | ||
| testImplementation(project(":instrumentation-annotations")) | ||
| testImplementation("io.opentelemetry:opentelemetry-extension-annotations") | ||
| } |
66 changes: 66 additions & 0 deletions
66
...nt/instrumentation/reactor/v3_4/operator/ContextPropagationOperator34Instrumentation.java
This file contains hidden or 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,66 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.reactor.v3_4.operator; | ||
|
|
||
| import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
| import static net.bytebuddy.matcher.ElementMatchers.isStatic; | ||
| import static net.bytebuddy.matcher.ElementMatchers.named; | ||
| import static net.bytebuddy.matcher.ElementMatchers.returns; | ||
| import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
|
||
| import application.io.opentelemetry.context.Context; | ||
| import io.opentelemetry.instrumentation.reactor.v3_1.ContextPropagationOperator; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
| import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
| import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage; | ||
| import net.bytebuddy.asm.Advice; | ||
| import net.bytebuddy.description.type.TypeDescription; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
|
|
||
| public class ContextPropagationOperator34Instrumentation implements TypeInstrumentation { | ||
| @Override | ||
| public ElementMatcher<TypeDescription> typeMatcher() { | ||
| return named( | ||
| "application.io.opentelemetry.instrumentation.reactor.v3_1.ContextPropagationOperator"); | ||
| } | ||
|
|
||
| @Override | ||
| public void transform(TypeTransformer transformer) { | ||
| transformer.applyAdviceToMethod( | ||
| isMethod() | ||
| .and(isPublic()) | ||
| .and(isStatic()) | ||
| .and(named("getOpenTelemetryContextFromContextView")) | ||
| .and(takesArgument(0, named("reactor.util.context.ContextView"))) | ||
| .and(takesArgument(1, named("application.io.opentelemetry.context.Context"))) | ||
| .and(returns(named("application.io.opentelemetry.context.Context"))), | ||
| ContextPropagationOperator34Instrumentation.class.getName() + "$GetContextViewAdvice"); | ||
| } | ||
|
|
||
| @SuppressWarnings("unused") | ||
| public static class GetContextViewAdvice { | ||
| @Advice.OnMethodEnter(skipOn = Advice.OnDefaultValue.class) | ||
| public static boolean methodEnter() { | ||
| return false; | ||
| } | ||
|
|
||
| @Advice.OnMethodExit(suppress = Throwable.class) | ||
| public static void methodExit( | ||
| @Advice.Argument(0) reactor.util.context.ContextView reactorContext, | ||
| @Advice.Argument(1) Context defaultContext, | ||
| @Advice.Return(readOnly = false) Context applicationContext) { | ||
|
|
||
| io.opentelemetry.context.Context agentContext = | ||
| ContextPropagationOperator.getOpenTelemetryContextFromContextView(reactorContext, null); | ||
| if (agentContext == null) { | ||
| applicationContext = defaultContext; | ||
| } else { | ||
| applicationContext = AgentContextStorage.toApplicationContext(agentContext); | ||
| } | ||
| } | ||
| } | ||
| } |
34 changes: 34 additions & 0 deletions
34
...trumentation/reactor/v3_4/operator/ContextPropagationOperator34InstrumentationModule.java
This file contains hidden or 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,34 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.reactor.v3_4.operator; | ||
|
|
||
| import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed; | ||
| 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; | ||
| import net.bytebuddy.matcher.ElementMatcher; | ||
|
|
||
| @AutoService(InstrumentationModule.class) | ||
| public class ContextPropagationOperator34InstrumentationModule extends InstrumentationModule { | ||
|
|
||
| public ContextPropagationOperator34InstrumentationModule() { | ||
| super("reactor", "reactor-3.4", "reactor-context-propagation-operator"); | ||
| } | ||
|
|
||
| @Override | ||
| public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() { | ||
| return hasClassesNamed( | ||
| "application.io.opentelemetry.context.Context", "reactor.util.context.ContextView"); | ||
| } | ||
|
|
||
| @Override | ||
| public List<TypeInstrumentation> typeInstrumentations() { | ||
| return singletonList(new ContextPropagationOperator34Instrumentation()); | ||
| } | ||
| } |
55 changes: 55 additions & 0 deletions
55
...vaagent/instrumentation/reactor/v3_4/ContextPropagationOperator34InstrumentationTest.java
This file contains hidden or 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,55 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.javaagent.instrumentation.reactor.v3_4; | ||
|
|
||
| import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.attributeEntry; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import io.opentelemetry.api.trace.Span; | ||
| import io.opentelemetry.api.trace.SpanKind; | ||
| import io.opentelemetry.context.Context; | ||
| import io.opentelemetry.instrumentation.reactor.v3_1.ContextPropagationOperator; | ||
| import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
| import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
|
||
| class ContextPropagationOperator34InstrumentationTest { | ||
|
|
||
| @RegisterExtension | ||
| static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
|
||
| @Test | ||
| void storeAndGetContext() { | ||
| reactor.util.context.Context reactorContext = reactor.util.context.Context.empty(); | ||
| testing.runWithSpan( | ||
| "parent", | ||
| () -> { | ||
| reactor.util.context.Context newReactorContext = | ||
| ContextPropagationOperator.storeOpenTelemetryContext( | ||
| reactorContext, Context.current()); | ||
| Context otelContext = | ||
| ContextPropagationOperator.getOpenTelemetryContextFromContextView( | ||
| newReactorContext, null); | ||
| assertThat(otelContext).isNotNull(); | ||
| Span.fromContext(otelContext).setAttribute("foo", "bar"); | ||
| Context otelContext2 = | ||
| ContextPropagationOperator.getOpenTelemetryContext(newReactorContext, null); | ||
| assertThat(otelContext2).isNotNull(); | ||
| Span.fromContext(otelContext2).setAttribute("foo2", "bar2"); | ||
| }); | ||
|
|
||
| testing.waitAndAssertTraces( | ||
| trace -> | ||
| trace.hasSpansSatisfyingExactly( | ||
| span -> | ||
| span.hasName("parent") | ||
| .hasKind(SpanKind.INTERNAL) | ||
| .hasNoParent() | ||
| .hasAttributes( | ||
| attributeEntry("foo", "bar"), attributeEntry("foo2", "bar2")))); | ||
| } | ||
| } |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.