-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow reading otel context from reactor ContextView (open-telemetry#1…
…1235) Co-authored-by: Trask Stalnaker <trask.stalnaker@gmail.com>
- Loading branch information
1 parent
fd5d7e1
commit 1b9b168
Showing
11 changed files
with
241 additions
and
16 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
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
34 changes: 34 additions & 0 deletions
34
instrumentation/reactor/reactor-3.4/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,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 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 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 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 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