|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.akkaactor; |
| 7 | + |
| 8 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 9 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 10 | + |
| 11 | +import io.opentelemetry.context.Context; |
| 12 | +import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge; |
| 13 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 14 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 15 | +import net.bytebuddy.asm.Advice; |
| 16 | +import net.bytebuddy.description.type.TypeDescription; |
| 17 | +import net.bytebuddy.matcher.ElementMatcher; |
| 18 | + |
| 19 | +public class AkkaScheduleInstrumentation implements TypeInstrumentation { |
| 20 | + |
| 21 | + @Override |
| 22 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 23 | + return named("akka.actor.LightArrayRevolverScheduler"); |
| 24 | + } |
| 25 | + |
| 26 | + @Override |
| 27 | + public void transform(TypeTransformer transformer) { |
| 28 | + transformer.applyAdviceToMethod( |
| 29 | + named("schedule") |
| 30 | + .and(takesArgument(0, named("scala.concurrent.duration.FiniteDuration"))) |
| 31 | + .and(takesArgument(1, named("scala.concurrent.duration.FiniteDuration"))) |
| 32 | + .and(takesArgument(2, named("java.lang.Runnable"))) |
| 33 | + .and(takesArgument(3, named("scala.concurrent.ExecutionContext"))), |
| 34 | + AkkaScheduleInstrumentation.class.getName() + "$ScheduleAdvice"); |
| 35 | + transformer.applyAdviceToMethod( |
| 36 | + named("scheduleOnce") |
| 37 | + .and(takesArgument(0, named("scala.concurrent.duration.FiniteDuration"))) |
| 38 | + .and(takesArgument(1, named("java.lang.Runnable"))) |
| 39 | + .and(takesArgument(2, named("scala.concurrent.ExecutionContext"))), |
| 40 | + AkkaScheduleInstrumentation.class.getName() + "$ScheduleOnceAdvice"); |
| 41 | + } |
| 42 | + |
| 43 | + @SuppressWarnings("unused") |
| 44 | + public static class ScheduleAdvice { |
| 45 | + |
| 46 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 47 | + public static void enterSchedule( |
| 48 | + @Advice.Argument(value = 2, readOnly = false) Runnable runnable) { |
| 49 | + Context context = Java8BytecodeBridge.currentContext(); |
| 50 | + runnable = context.wrap(runnable); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + @SuppressWarnings("unused") |
| 55 | + public static class ScheduleOnceAdvice { |
| 56 | + |
| 57 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 58 | + public static void enterScheduleOnce( |
| 59 | + @Advice.Argument(value = 1, readOnly = false) Runnable runnable) { |
| 60 | + Context context = Java8BytecodeBridge.currentContext(); |
| 61 | + runnable = context.wrap(runnable); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments