|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.otel4s.v0_12; |
| 7 | + |
| 8 | +import static net.bytebuddy.matcher.ElementMatchers.isMethod; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.isPublic; |
| 10 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 12 | + |
| 13 | +import io.opentelemetry.javaagent.bootstrap.otel4s.FiberLocalContextHelper; |
| 14 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 15 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 16 | +import net.bytebuddy.asm.Advice; |
| 17 | +import net.bytebuddy.description.type.TypeDescription; |
| 18 | +import net.bytebuddy.matcher.ElementMatcher; |
| 19 | + |
| 20 | +@SuppressWarnings("IdentifierName") |
| 21 | +public class Otel4sIOLocalContextStorageInstrumentation implements TypeInstrumentation { |
| 22 | + |
| 23 | + @Override |
| 24 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 25 | + return named("org.typelevel.otel4s.oteljava.context.IOLocalContextStorage$"); |
| 26 | + } |
| 27 | + |
| 28 | + @Override |
| 29 | + @SuppressWarnings({"CatchingUnchecked", "CatchAndPrintStackTrace"}) |
| 30 | + public void transform(TypeTransformer transformer) { |
| 31 | + transformer.applyAdviceToMethod( |
| 32 | + isPublic() |
| 33 | + .and(isMethod()) |
| 34 | + .and(named("registerFiberThreadContext")) |
| 35 | + .and(takesArgument(0, ThreadLocal.class)), |
| 36 | + this.getClass().getName() + "$RegisterAdvice"); |
| 37 | + } |
| 38 | + |
| 39 | + @SuppressWarnings("unused") |
| 40 | + public static final class RegisterAdvice { |
| 41 | + |
| 42 | + private RegisterAdvice() {} |
| 43 | + |
| 44 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 45 | + public static void onEnter( |
| 46 | + @Advice.Argument(0) |
| 47 | + ThreadLocal<application.io.opentelemetry.context.Context> _fiberThreadLocal) { |
| 48 | + FiberLocalContextHelper.setFiberThreadLocal( |
| 49 | + Otel4sFiberContextBridge.contextThreadLocal(_fiberThreadLocal)); |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments