|
| 1 | +/* |
| 2 | + * Copyright The OpenTelemetry Authors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package io.opentelemetry.javaagent.instrumentation.powerjob.v4_0; |
| 7 | + |
| 8 | +import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; |
| 9 | +import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface; |
| 10 | +import static io.opentelemetry.javaagent.instrumentation.powerjob.v4_0.PowerJobSingletons.instrumenter; |
| 11 | +import static net.bytebuddy.matcher.ElementMatchers.isPublic; |
| 12 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 13 | +import static net.bytebuddy.matcher.ElementMatchers.takesArgument; |
| 14 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 15 | + |
| 16 | +import io.opentelemetry.context.Context; |
| 17 | +import io.opentelemetry.context.Scope; |
| 18 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; |
| 19 | +import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; |
| 20 | +import net.bytebuddy.asm.Advice; |
| 21 | +import net.bytebuddy.description.type.TypeDescription; |
| 22 | +import net.bytebuddy.matcher.ElementMatcher; |
| 23 | +import tech.powerjob.worker.core.processor.ProcessResult; |
| 24 | +import tech.powerjob.worker.core.processor.TaskContext; |
| 25 | +import tech.powerjob.worker.core.processor.sdk.BasicProcessor; |
| 26 | + |
| 27 | +public class BasicProcessorInstrumentation implements TypeInstrumentation { |
| 28 | + @Override |
| 29 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 30 | + return implementsInterface(named("tech.powerjob.worker.core.processor.sdk.BasicProcessor")); |
| 31 | + } |
| 32 | + |
| 33 | + @Override |
| 34 | + public void transform(TypeTransformer transformer) { |
| 35 | + transformer.applyAdviceToMethod( |
| 36 | + named("process") |
| 37 | + .and(isPublic()) |
| 38 | + .and( |
| 39 | + takesArguments(1) |
| 40 | + .and( |
| 41 | + takesArgument( |
| 42 | + 0, named("tech.powerjob.worker.core.processor.TaskContext")))), |
| 43 | + BasicProcessorInstrumentation.class.getName() + "$ProcessAdvice"); |
| 44 | + } |
| 45 | + |
| 46 | + public static class ProcessAdvice { |
| 47 | + |
| 48 | + @SuppressWarnings("unused") |
| 49 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 50 | + public static void onSchedule( |
| 51 | + @Advice.This BasicProcessor handler, |
| 52 | + @Advice.Argument(0) TaskContext taskContext, |
| 53 | + @Advice.Local("otelRequest") PowerJobProcessRequest request, |
| 54 | + @Advice.Local("otelContext") Context context, |
| 55 | + @Advice.Local("otelScope") Scope scope) { |
| 56 | + Context parentContext = currentContext(); |
| 57 | + request = |
| 58 | + PowerJobProcessRequest.createRequest( |
| 59 | + taskContext.getJobId(), |
| 60 | + handler, |
| 61 | + "process", |
| 62 | + taskContext.getJobParams(), |
| 63 | + taskContext.getInstanceParams()); |
| 64 | + |
| 65 | + if (!instrumenter().shouldStart(parentContext, request)) { |
| 66 | + return; |
| 67 | + } |
| 68 | + context = instrumenter().start(parentContext, request); |
| 69 | + scope = context.makeCurrent(); |
| 70 | + } |
| 71 | + |
| 72 | + @SuppressWarnings("unused") |
| 73 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 74 | + public static void stopSpan( |
| 75 | + @Advice.Return ProcessResult result, |
| 76 | + @Advice.Thrown Throwable throwable, |
| 77 | + @Advice.Local("otelRequest") PowerJobProcessRequest request, |
| 78 | + @Advice.Local("otelContext") Context context, |
| 79 | + @Advice.Local("otelScope") Scope scope) { |
| 80 | + if (scope == null) { |
| 81 | + return; |
| 82 | + } |
| 83 | + scope.close(); |
| 84 | + instrumenter().end(context, request, result, throwable); |
| 85 | + } |
| 86 | + } |
| 87 | +} |
0 commit comments