|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.instrumentationapi; |
| 7 | + |
| 8 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 10 | + |
| 11 | +import application.io.opentelemetry.api.trace.Span; |
| 12 | +import application.io.opentelemetry.context.Context; |
| 13 | +import application.io.opentelemetry.instrumentation.api.internal.SpanKey; |
| 14 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 15 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 16 | +import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.context.AgentContextStorage; |
| 17 | +import io.opentelemetry.javaagent.instrumentation.opentelemetryapi.trace.Bridging; |
| 18 | +import net.bytebuddy.asm.Advice; |
| 19 | +import net.bytebuddy.description.type.TypeDescription; |
| 20 | +import net.bytebuddy.matcher.ElementMatcher; |
| 21 | + |
| 22 | +final class SpanKeyInstrumentation implements TypeInstrumentation { |
| 23 | + |
| 24 | + @Override |
| 25 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 26 | + return named("application.io.opentelemetry.instrumentation.api.internal.SpanKey"); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public void transform(TypeTransformer transformer) { |
| 31 | + transformer.applyAdviceToMethod( |
| 32 | + named("storeInContext") |
| 33 | + .and(takesArgument(0, named("application.io.opentelemetry.context.Context"))) |
| 34 | + .and(takesArgument(1, named("application.io.opentelemetry.api.trace.Span"))), |
| 35 | + this.getClass().getName() + "$StoreInContextAdvice"); |
| 36 | + transformer.applyAdviceToMethod( |
| 37 | + named("fromContextOrNull") |
| 38 | + .and(takesArgument(0, named("application.io.opentelemetry.context.Context"))), |
| 39 | + this.getClass().getName() + "$FromContextOrNullAdvice"); |
| 40 | + } |
| 41 | + |
| 42 | + @SuppressWarnings("unused") |
| 43 | + public static class StoreInContextAdvice { |
| 44 | + @Advice.OnMethodEnter(skipOn = Advice.OnDefaultValue.class) |
| 45 | + public static Object onEnter() { |
| 46 | + return null; |
| 47 | + } |
| 48 | + |
| 49 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 50 | + public static void onExit( |
| 51 | + @Advice.This SpanKey applicationSpanKey, |
| 52 | + @Advice.Argument(0) Context applicationContext, |
| 53 | + @Advice.Argument(1) Span applicationSpan, |
| 54 | + @Advice.Return(readOnly = false) Context newApplicationContext) { |
| 55 | + |
| 56 | + io.opentelemetry.instrumentation.api.internal.SpanKey agentSpanKey = |
| 57 | + SpanKeyBridging.toAgentOrNull(applicationSpanKey); |
| 58 | + if (agentSpanKey == null) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + io.opentelemetry.context.Context agentContext = |
| 63 | + AgentContextStorage.getAgentContext(applicationContext); |
| 64 | + |
| 65 | + io.opentelemetry.api.trace.Span agentSpan = Bridging.toAgentOrNull(applicationSpan); |
| 66 | + if (agentSpan == null) { |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + io.opentelemetry.context.Context newAgentContext = |
| 71 | + agentSpanKey.storeInContext(agentContext, agentSpan); |
| 72 | + |
| 73 | + newApplicationContext = AgentContextStorage.toApplicationContext(newAgentContext); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @SuppressWarnings("unused") |
| 78 | + public static class FromContextOrNullAdvice { |
| 79 | + @Advice.OnMethodEnter(skipOn = Advice.OnDefaultValue.class) |
| 80 | + public static Object onEnter() { |
| 81 | + return null; |
| 82 | + } |
| 83 | + |
| 84 | + @Advice.OnMethodExit(suppress = Throwable.class) |
| 85 | + public static void onExit( |
| 86 | + @Advice.This SpanKey applicationSpanKey, |
| 87 | + @Advice.Argument(0) Context applicationContext, |
| 88 | + @Advice.Return(readOnly = false) Span applicationSpan) { |
| 89 | + |
| 90 | + io.opentelemetry.instrumentation.api.internal.SpanKey agentSpanKey = |
| 91 | + SpanKeyBridging.toAgentOrNull(applicationSpanKey); |
| 92 | + if (agentSpanKey == null) { |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + io.opentelemetry.context.Context agentContext = |
| 97 | + AgentContextStorage.getAgentContext(applicationContext); |
| 98 | + |
| 99 | + io.opentelemetry.api.trace.Span agentSpan = agentSpanKey.fromContextOrNull(agentContext); |
| 100 | + |
| 101 | + applicationSpan = agentSpan == null ? null : Bridging.toApplication(agentSpan); |
| 102 | + } |
| 103 | + } |
| 104 | +} |
0 commit comments