From 6489cd85818e579690b36f7432adafea5bd911fa Mon Sep 17 00:00:00 2001 From: orangewest Date: Tue, 30 Jul 2024 14:10:07 +0800 Subject: [PATCH] feat: support powerjob --- .../impl/PowerJobScheduleTaskInterceptor.java | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/main/java/io/github/artlibs/autotrace4j/interceptor/impl/PowerJobScheduleTaskInterceptor.java diff --git a/src/main/java/io/github/artlibs/autotrace4j/interceptor/impl/PowerJobScheduleTaskInterceptor.java b/src/main/java/io/github/artlibs/autotrace4j/interceptor/impl/PowerJobScheduleTaskInterceptor.java new file mode 100644 index 0000000..c68455d --- /dev/null +++ b/src/main/java/io/github/artlibs/autotrace4j/interceptor/impl/PowerJobScheduleTaskInterceptor.java @@ -0,0 +1,60 @@ +package io.github.artlibs.autotrace4j.interceptor.impl; + +import io.github.artlibs.autotrace4j.context.AutoTraceCtx; +import io.github.artlibs.autotrace4j.interceptor.base.AbstractInstanceInterceptor; +import net.bytebuddy.description.method.MethodDescription; +import net.bytebuddy.description.type.TypeDescription; +import net.bytebuddy.matcher.ElementMatcher; + +import java.lang.reflect.Method; + +import static net.bytebuddy.matcher.ElementMatchers.*; + +/** + * PowerJob Interceptor + * + * @author orangewest + * @since 2024-07-30 + *

+ */ +public class PowerJobScheduleTaskInterceptor extends AbstractInstanceInterceptor { + + /** + * {@inheritDoc} + */ + @Override + public ElementMatcher typeMatcher() { + return hasSuperType(named("tech.powerjob.worker.core.processor.sdk.BasicProcessor")) + .and(not(isInterface())).and(not(isAbstract())); + } + + /** + * {@inheritDoc} + */ + @Override + public ElementMatcher methodMatcher() { + return named("process") + .and(takesArgument(0, named("tech.powerjob.worker.core.processor.TaskContext"))); + } + + /** + * {@inheritDoc} + */ + @Override + public void onMethodEnter(Object obj, Object[] allArgs, Method originMethod) throws Exception { + AutoTraceCtx.setTraceId(AutoTraceCtx.generate()); + AutoTraceCtx.setSpanId(AutoTraceCtx.generate()); + // There will be no parent span as this is a startup context + AutoTraceCtx.setParentSpanId(null); + } + + /** + * {@inheritDoc} + */ + @Override + public Object onMethodExit(Object obj, Object[] allArgs, Object result, Method originMethod) throws Exception { + AutoTraceCtx.removeAll(); + return result; + } + +}