|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.reactor.v3_4.operator; |
| 7 | + |
| 8 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.isPublic; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.isStatic; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 12 | +import static net.bytebuddy.matcher.ElementMatchers.returns; |
| 13 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 14 | + |
| 15 | +import application.io.opentelemetry.context.Context; |
| 16 | +import io.opentelemetry.instrumentation.reactor.v3_1.ContextPropagationOperator; |
| 17 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 18 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 19 | +import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage; |
| 20 | +import net.bytebuddy.asm.Advice; |
| 21 | +import net.bytebuddy.description.type.TypeDescription; |
| 22 | +import net.bytebuddy.matcher.ElementMatcher; |
| 23 | + |
| 24 | +public class ContextPropagationOperator34Instrumentation implements TypeInstrumentation { |
| 25 | + @Override |
| 26 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 27 | + return named( |
| 28 | + "application.io.opentelemetry.instrumentation.reactor.v3_1.ContextPropagationOperator"); |
| 29 | + } |
| 30 | + |
| 31 | + @Override |
| 32 | + public void transform(TypeTransformer transformer) { |
| 33 | + transformer.applyAdviceToMethod( |
| 34 | + isMethod() |
| 35 | + .and(isPublic()) |
| 36 | + .and(isStatic()) |
| 37 | + .and(named("getOpenTelemetryContextFromContextView")) |
| 38 | + .and(takesArgument(0, named("reactor.util.context.ContextView"))) |
| 39 | + .and(takesArgument(1, named("application.io.opentelemetry.context.Context"))) |
| 40 | + .and(returns(named("application.io.opentelemetry.context.Context"))), |
| 41 | + ContextPropagationOperator34Instrumentation.class.getName() + "$GetContextViewAdvice"); |
| 42 | + } |
| 43 | + |
| 44 | + @SuppressWarnings("unused") |
| 45 | + public static class GetContextViewAdvice { |
| 46 | + @Advice.OnMethodEnter(skipOn = Advice.OnDefaultValue.class) |
| 47 | + public static boolean methodEnter() { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 52 | + public static void methodExit( |
| 53 | + @Advice.Argument(0) reactor.util.context.ContextView reactorContext, |
| 54 | + @Advice.Argument(1) Context defaultContext, |
| 55 | + @Advice.Return(readOnly = false) Context applicationContext) { |
| 56 | + |
| 57 | + io.opentelemetry.context.Context agentContext = |
| 58 | + ContextPropagationOperator.getOpenTelemetryContextFromContextView(reactorContext, null); |
| 59 | + if (agentContext == null) { |
| 60 | + applicationContext = defaultContext; |
| 61 | + } else { |
| 62 | + applicationContext = AgentContextStorage.toApplicationContext(agentContext); |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
0 commit comments