|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.reactor; |
| 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.namedOneOf; |
| 13 | +import static net.bytebuddy.matcher.ElementMatchers.returns; |
| 14 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 15 | + |
| 16 | +import application.io.opentelemetry.context.Context; |
| 17 | +import io.opentelemetry.instrumentation.reactor.ContextPropagationOperator; |
| 18 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 19 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 20 | +import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage; |
| 21 | +import net.bytebuddy.asm.Advice; |
| 22 | +import net.bytebuddy.description.type.TypeDescription; |
| 23 | +import net.bytebuddy.matcher.ElementMatcher; |
| 24 | + |
| 25 | +public class ContextPropagationOperatorInstrumentation implements TypeInstrumentation { |
| 26 | + @Override |
| 27 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 28 | + return named("application.io.opentelemetry.instrumentation.reactor.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("storeOpenTelemetryContext")) |
| 38 | + .and(takesArgument(0, named("reactor.util.context.Context"))) |
| 39 | + .and(takesArgument(1, named("application.io.opentelemetry.context.Context"))) |
| 40 | + .and(returns(named("reactor.util.context.Context"))), |
| 41 | + ContextPropagationOperatorInstrumentation.class.getName() + "$StoreAdvice"); |
| 42 | + transformer.applyAdviceToMethod( |
| 43 | + isMethod() |
| 44 | + .and(isPublic()) |
| 45 | + .and(isStatic()) |
| 46 | + .and(named("getOpenTelemetryContext")) |
| 47 | + .and(takesArgument(0, named("reactor.util.context.Context"))) |
| 48 | + .and(takesArgument(1, named("application.io.opentelemetry.context.Context"))) |
| 49 | + .and(returns(named("application.io.opentelemetry.context.Context"))), |
| 50 | + ContextPropagationOperatorInstrumentation.class.getName() + "$GetAdvice"); |
| 51 | + transformer.applyAdviceToMethod( |
| 52 | + isMethod() |
| 53 | + .and(isPublic()) |
| 54 | + .and(isStatic()) |
| 55 | + .and(named("runWithContext")) |
| 56 | + .and( |
| 57 | + takesArgument( |
| 58 | + 0, namedOneOf("reactor.core.publisher.Mono", "reactor.core.publisher.Flux"))) |
| 59 | + .and(takesArgument(1, named("application.io.opentelemetry.context.Context"))) |
| 60 | + .and(returns(namedOneOf("reactor.core.publisher.Mono", "reactor.core.publisher.Flux"))), |
| 61 | + ContextPropagationOperatorInstrumentation.class.getName() + "$RunWithAdvice"); |
| 62 | + } |
| 63 | + |
| 64 | + @SuppressWarnings("unused") |
| 65 | + public static class StoreAdvice { |
| 66 | + @Advice.OnMethodEnter(suppress = Throwable.class, skipOn = Advice.OnDefaultValue.class) |
| 67 | + public static boolean methodEnter() { |
| 68 | + return false; |
| 69 | + } |
| 70 | + |
| 71 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 72 | + public static void methodExit( |
| 73 | + @Advice.Argument(0) reactor.util.context.Context reactorContext, |
| 74 | + @Advice.Argument(1) Context applicationContext, |
| 75 | + @Advice.Return(readOnly = false) reactor.util.context.Context updatedReactorContext) { |
| 76 | + updatedReactorContext = |
| 77 | + ContextPropagationOperator.storeOpenTelemetryContext( |
| 78 | + reactorContext, AgentContextStorage.getAgentContext(applicationContext)); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + @SuppressWarnings("unused") |
| 83 | + public static class GetAdvice { |
| 84 | + @Advice.OnMethodEnter(skipOn = Advice.OnDefaultValue.class) |
| 85 | + public static boolean methodEnter() { |
| 86 | + return false; |
| 87 | + } |
| 88 | + |
| 89 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 90 | + public static void methodExit( |
| 91 | + @Advice.Argument(0) reactor.util.context.Context reactorContext, |
| 92 | + @Advice.Argument(1) Context defaultContext, |
| 93 | + @Advice.Return(readOnly = false) Context applicationContext) { |
| 94 | + |
| 95 | + io.opentelemetry.context.Context agentContext = |
| 96 | + ContextPropagationOperator.getOpenTelemetryContext(reactorContext, null); |
| 97 | + if (agentContext == null) { |
| 98 | + applicationContext = defaultContext; |
| 99 | + } else { |
| 100 | + applicationContext = AgentContextStorage.toApplicationContext(agentContext); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @SuppressWarnings("unused") |
| 106 | + public static class RunWithAdvice { |
| 107 | + @Advice.OnMethodEnter |
| 108 | + public static void methodEnter( |
| 109 | + @Advice.FieldValue(value = "enabled", readOnly = false) boolean enabled) { |
| 110 | + enabled = true; |
| 111 | + } |
| 112 | + } |
| 113 | +} |
0 commit comments