diff --git a/instrumentation/xxl-job/README.md b/instrumentation/xxl-job/README.md index d1476037445c..66f867b76fb8 100644 --- a/instrumentation/xxl-job/README.md +++ b/instrumentation/xxl-job/README.md @@ -1,5 +1,5 @@ # Settings for the XXL-JOB instrumentation -| System property | Type | Default | Description | -|-------------------------------------------------------------|---------|---------|----------------------------------------------------------------------------------------------------------------------------------------------------------------| -| `otel.instrumentation.xxl-job.experimental-span-attributes` | Boolean | `false` | Enable the capture of experimental span attributes. | +| System property | Type | Default | Description | +|-------------------------------------------------------------|---------|---------|-----------------------------------------------------| +| `otel.instrumentation.xxl-job.experimental-span-attributes` | Boolean | `false` | Enable the capture of experimental span attributes. | diff --git a/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/build.gradle.kts b/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/build.gradle.kts index f79df5cbdba4..57cbcd6c422f 100644 --- a/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/build.gradle.kts +++ b/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/build.gradle.kts @@ -17,6 +17,9 @@ dependencies { } implementation(project(":instrumentation:xxl-job:xxl-job-common:javaagent")) + testInstrumentation(project(":instrumentation:xxl-job:xxl-job-2.1.2:javaagent")) + testInstrumentation(project(":instrumentation:xxl-job:xxl-job-2.3.0:javaagent")) + // It needs the javax.annotation-api in xxl-job-core 1.9.2. testImplementation("javax.annotation:javax.annotation-api:1.3.2") testImplementation(project(":instrumentation:xxl-job:xxl-job-common:testing")) diff --git a/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/GlueJobHandlerInstrumentation.java b/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/GlueJobHandlerInstrumentation.java index f8c6c1374c57..5c737335ebcb 100644 --- a/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/GlueJobHandlerInstrumentation.java +++ b/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/GlueJobHandlerInstrumentation.java @@ -6,12 +6,11 @@ package io.opentelemetry.javaagent.instrumentation.xxljob.v1_9_2; import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; +import static io.opentelemetry.javaagent.instrumentation.xxljob.v1_9_2.XxlJobSingletons.helper; import static net.bytebuddy.matcher.ElementMatchers.isPublic; import static net.bytebuddy.matcher.ElementMatchers.named; -import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import static net.bytebuddy.matcher.ElementMatchers.takesArguments; -import com.xxl.job.core.glue.GlueTypeEnum; import com.xxl.job.core.handler.IJobHandler; import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; @@ -33,7 +32,7 @@ public ElementMatcher typeMatcher() { @Override public void transform(TypeTransformer transformer) { transformer.applyAdviceToMethod( - named("execute").and(isPublic()).and(takesArguments(1).and(takesArgument(0, String.class))), + named("execute").and(isPublic()).and(takesArguments(String.class)), GlueJobHandlerInstrumentation.class.getName() + "$ScheduleAdvice"); } @@ -47,10 +46,8 @@ public static void onSchedule( @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { Context parentContext = currentContext(); - request = new XxlJobProcessRequest(); - request.setDeclaringClass(handler.getClass()); - request.setGlueTypeEnum(GlueTypeEnum.GLUE_GROOVY); - context = XxlJobHelper.startSpan(parentContext, request); + request = XxlJobProcessRequest.createGlueJobRequest(handler); + context = helper().startSpan(parentContext, request); if (context == null) { return; } @@ -64,7 +61,7 @@ public static void stopSpan( @Advice.Local("otelRequest") XxlJobProcessRequest request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - XxlJobHelper.stopSpan(result, request, throwable, scope, context); + helper().stopSpan(result, request, throwable, scope, context); } } } diff --git a/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/ScriptJobHandlerInstrumentation.java b/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/ScriptJobHandlerInstrumentation.java index 5430300a9458..97783e52a574 100644 --- a/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/ScriptJobHandlerInstrumentation.java +++ b/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/ScriptJobHandlerInstrumentation.java @@ -6,6 +6,7 @@ package io.opentelemetry.javaagent.instrumentation.xxljob.v1_9_2; import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; +import static io.opentelemetry.javaagent.instrumentation.xxljob.v1_9_2.XxlJobSingletons.helper; import static net.bytebuddy.matcher.ElementMatchers.isPublic; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesArgument; @@ -47,10 +48,8 @@ public static void onSchedule( @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { Context parentContext = currentContext(); - request = new XxlJobProcessRequest(); - request.setGlueTypeEnum(glueTypeEnum); - request.setJobId(jobId); - context = XxlJobHelper.startSpan(parentContext, request); + request = XxlJobProcessRequest.createScriptJobRequest(glueTypeEnum, jobId); + context = helper().startSpan(parentContext, request); if (context == null) { return; } @@ -64,7 +63,7 @@ public static void stopSpan( @Advice.Local("otelRequest") XxlJobProcessRequest request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - XxlJobHelper.stopSpan(result, request, throwable, scope, context); + helper().stopSpan(result, request, throwable, scope, context); } } } diff --git a/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/SimpleJobHandlerInstrumentation.java b/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/SimpleJobHandlerInstrumentation.java index 7dca5e82d9a5..5adf7a4f7f35 100644 --- a/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/SimpleJobHandlerInstrumentation.java +++ b/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/SimpleJobHandlerInstrumentation.java @@ -9,6 +9,7 @@ import static io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobConstants.XXL_GLUE_JOB_HANDLER; import static io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobConstants.XXL_METHOD_JOB_HANDLER; import static io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobConstants.XXL_SCRIPT_JOB_HANDLER; +import static io.opentelemetry.javaagent.instrumentation.xxljob.v1_9_2.XxlJobSingletons.helper; import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; import static net.bytebuddy.matcher.ElementMatchers.isPublic; import static net.bytebuddy.matcher.ElementMatchers.named; @@ -17,7 +18,6 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument; import static net.bytebuddy.matcher.ElementMatchers.takesArguments; -import com.xxl.job.core.glue.GlueTypeEnum; import com.xxl.job.core.handler.IJobHandler; import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; @@ -54,10 +54,8 @@ public static void onSchedule( @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { Context parentContext = currentContext(); - request = new XxlJobProcessRequest(); - request.setDeclaringClass(handler.getClass()); - request.setGlueTypeEnum(GlueTypeEnum.BEAN); - context = XxlJobHelper.startSpan(parentContext, request); + request = XxlJobProcessRequest.createSimpleJobRequest(handler); + context = helper().startSpan(parentContext, request); if (context == null) { return; } @@ -72,7 +70,7 @@ public static void stopSpan( @Advice.Local("otelRequest") XxlJobProcessRequest request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - XxlJobHelper.stopSpan(result, request, throwable, scope, context); + helper().stopSpan(result, request, throwable, scope, context); } } } diff --git a/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/XxlJobHelper.java b/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/XxlJobHelper.java deleted file mode 100644 index 3a32724692ee..000000000000 --- a/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/XxlJobHelper.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.xxljob.v1_9_2; - -import static io.opentelemetry.javaagent.instrumentation.xxljob.v1_9_2.XxlJobSingletons.instrumenter; - -import com.xxl.job.core.biz.model.ReturnT; -import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; -import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobProcessRequest; - -public class XxlJobHelper { - - private XxlJobHelper() {} - - public static Context startSpan(Context parentContext, XxlJobProcessRequest request) { - if (!instrumenter().shouldStart(parentContext, request)) { - return null; - } - return instrumenter().start(parentContext, request); - } - - public static void stopSpan( - Object result, - XxlJobProcessRequest request, - Throwable throwable, - Scope scope, - Context context) { - if (scope == null) { - return; - } - if (result != null && (result instanceof ReturnT)) { - ReturnT res = (ReturnT) result; - if (res.getCode() == ReturnT.FAIL_CODE) { - request.setSchedulingSuccess(Boolean.FALSE); - } - } - if (throwable != null) { - request.setSchedulingSuccess(Boolean.FALSE); - } - scope.close(); - instrumenter().end(context, request, null, throwable); - } -} diff --git a/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/XxlJobSingletons.java b/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/XxlJobSingletons.java index 2544dafab904..2488b109e438 100644 --- a/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/XxlJobSingletons.java +++ b/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/XxlJobSingletons.java @@ -5,53 +5,30 @@ package io.opentelemetry.javaagent.instrumentation.xxljob.v1_9_2; +import com.xxl.job.core.biz.model.ReturnT; import com.xxl.job.core.glue.GlueTypeEnum; -import io.opentelemetry.api.GlobalOpenTelemetry; -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.api.trace.StatusCode; -import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesExtractor; -import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; -import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; -import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig; -import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobCodeAttributesGetter; -import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobExperimentalAttributeExtractor; +import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobHelper; +import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobInstrumenterFactory; import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobProcessRequest; -import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobSpanNameExtractor; public final class XxlJobSingletons { private static final String INSTRUMENTATION_NAME = "io.opentelemetry.xxl-job-1.9.2"; - - private static final boolean CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES = - InstrumentationConfig.get() - .getBoolean("otel.instrumentation.xxl-job.experimental-span-attributes", false); - - private static final Instrumenter INSTRUMENTER; - - static { - XxlJobSpanNameExtractor spanNameExtractor = new XxlJobSpanNameExtractor(); - InstrumenterBuilder builder = - Instrumenter.builder( - GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, spanNameExtractor) - .addAttributesExtractor( - CodeAttributesExtractor.create(new XxlJobCodeAttributesGetter())) - .setSpanStatusExtractor( - (spanStatusBuilder, xxlJobProcessRequest, response, error) -> { - if (error != null - || Boolean.FALSE.equals(xxlJobProcessRequest.getSchedulingSuccess())) { - spanStatusBuilder.setStatus(StatusCode.ERROR); - } - }); - if (CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { - builder.addAttributesExtractor( - AttributesExtractor.constant(AttributeKey.stringKey("job.system"), "xxl-job")); - builder.addAttributesExtractor(new XxlJobExperimentalAttributeExtractor()); - } - INSTRUMENTER = builder.buildInstrumenter(); - } - - public static Instrumenter instrumenter() { - return INSTRUMENTER; + private static final Instrumenter INSTRUMENTER = + XxlJobInstrumenterFactory.create(INSTRUMENTATION_NAME); + private static final XxlJobHelper HELPER = + XxlJobHelper.create( + INSTRUMENTER, + object -> { + if (object != null && (object instanceof ReturnT)) { + ReturnT result = (ReturnT) object; + return result.getCode() == ReturnT.FAIL_CODE; + } + return false; + }); + + public static XxlJobHelper helper() { + return HELPER; } @SuppressWarnings({"Unused", "ReturnValueIgnored"}) diff --git a/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/XxlJobTest.java b/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/XxlJobTest.java index 5071cbe66fc4..c09b0afb5b9b 100644 --- a/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/XxlJobTest.java +++ b/instrumentation/xxl-job/xxl-job-1.9.2/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/xxljob/v1_9_2/XxlJobTest.java @@ -8,7 +8,6 @@ import static io.opentelemetry.instrumentation.xxljob.XxlJobTestingConstants.DEFAULT_GLUE_UPDATE_TIME; import static io.opentelemetry.instrumentation.xxljob.XxlJobTestingConstants.GLUE_JOB_GROOVY_SOURCE_OLD; import static io.opentelemetry.instrumentation.xxljob.XxlJobTestingConstants.GLUE_JOB_SHELL_SCRIPT; -import static org.junit.jupiter.api.Assumptions.assumeTrue; import com.xxl.job.core.glue.GlueFactory; import com.xxl.job.core.glue.GlueTypeEnum; @@ -67,10 +66,4 @@ protected IJobHandler getCustomizeFailedHandler() { protected IJobHandler getMethodHandler() { return null; } - - @Override - public void testMethodJob() { - // The version [1.9.2~2.1.2) of the module does not support method job. - assumeTrue(Boolean.FALSE); - } } diff --git a/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/build.gradle.kts b/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/build.gradle.kts index 4671fe7b38b2..513d981448a0 100644 --- a/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/build.gradle.kts +++ b/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/build.gradle.kts @@ -17,6 +17,9 @@ dependencies { } implementation(project(":instrumentation:xxl-job:xxl-job-common:javaagent")) + testInstrumentation(project(":instrumentation:xxl-job:xxl-job-1.9.2:javaagent")) + testInstrumentation(project(":instrumentation:xxl-job:xxl-job-2.3.0:javaagent")) + testImplementation(project(":instrumentation:xxl-job:xxl-job-common:testing")) latestDepTestLibrary("com.xuxueli:xxl-job-core:2.2.+") { exclude("org.codehaus.groovy", "groovy") diff --git a/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/GlueJobHandlerInstrumentation.java b/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/GlueJobHandlerInstrumentation.java index a36aa253b4dd..2e3ddaca3ce8 100644 --- a/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/GlueJobHandlerInstrumentation.java +++ b/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/GlueJobHandlerInstrumentation.java @@ -6,10 +6,10 @@ package io.opentelemetry.javaagent.instrumentation.xxljob.v2_1_2; import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; +import static io.opentelemetry.javaagent.instrumentation.xxljob.v2_1_2.XxlJobSingletons.helper; import static net.bytebuddy.matcher.ElementMatchers.isPublic; import static net.bytebuddy.matcher.ElementMatchers.named; -import com.xxl.job.core.glue.GlueTypeEnum; import com.xxl.job.core.handler.IJobHandler; import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; @@ -44,10 +44,8 @@ public static void onSchedule( @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { Context parentContext = currentContext(); - request = new XxlJobProcessRequest(); - request.setDeclaringClass(handler.getClass()); - request.setGlueTypeEnum(GlueTypeEnum.GLUE_GROOVY); - context = XxlJobHelper.startSpan(parentContext, request); + request = XxlJobProcessRequest.createGlueJobRequest(handler); + context = helper().startSpan(parentContext, request); if (context == null) { return; } @@ -61,7 +59,7 @@ public static void stopSpan( @Advice.Local("otelRequest") XxlJobProcessRequest request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - XxlJobHelper.stopSpan(result, request, throwable, scope, context); + helper().stopSpan(result, request, throwable, scope, context); } } } diff --git a/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/MethodJobHandlerInstrumentation.java b/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/MethodJobHandlerInstrumentation.java index 6aef8c9c069a..021c3092682e 100644 --- a/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/MethodJobHandlerInstrumentation.java +++ b/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/MethodJobHandlerInstrumentation.java @@ -6,11 +6,10 @@ package io.opentelemetry.javaagent.instrumentation.xxljob.v2_1_2; import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; +import static io.opentelemetry.javaagent.instrumentation.xxljob.v2_1_2.XxlJobSingletons.helper; import static net.bytebuddy.matcher.ElementMatchers.isPublic; import static net.bytebuddy.matcher.ElementMatchers.named; -import com.xxl.job.core.glue.GlueTypeEnum; -import com.xxl.job.core.handler.annotation.XxlJob; import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; @@ -40,24 +39,14 @@ public void transform(TypeTransformer transformer) { public static class ScheduleAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) public static void onSchedule( - @Advice.FieldValue("target") Object declaringClass, + @Advice.FieldValue("target") Object target, @Advice.FieldValue("method") Method method, @Advice.Local("otelRequest") XxlJobProcessRequest request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { Context parentContext = currentContext(); - request = new XxlJobProcessRequest(); - request.setGlueTypeEnum(GlueTypeEnum.BEAN); - if (method != null) { - XxlJob xxlJobAnnotation = method.getAnnotation(XxlJob.class); - String annotationName = method.getName(); - if (xxlJobAnnotation != null) { - annotationName = xxlJobAnnotation.value(); - } - request.setMethodName(annotationName); - } - request.setDeclaringClass(declaringClass.getClass()); - context = XxlJobHelper.startSpan(parentContext, request); + request = XxlJobProcessRequest.createMethodJobRequest(target, method); + context = helper().startSpan(parentContext, request); if (context == null) { return; } @@ -71,7 +60,7 @@ public static void stopSpan( @Advice.Local("otelRequest") XxlJobProcessRequest request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - XxlJobHelper.stopSpan(result, request, throwable, scope, context); + helper().stopSpan(result, request, throwable, scope, context); } } } diff --git a/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/ScriptJobHandlerInstrumentation.java b/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/ScriptJobHandlerInstrumentation.java index dd9d1ab3312b..af6b612293c0 100644 --- a/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/ScriptJobHandlerInstrumentation.java +++ b/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/ScriptJobHandlerInstrumentation.java @@ -6,6 +6,7 @@ package io.opentelemetry.javaagent.instrumentation.xxljob.v2_1_2; import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; +import static io.opentelemetry.javaagent.instrumentation.xxljob.v2_1_2.XxlJobSingletons.helper; import static net.bytebuddy.matcher.ElementMatchers.isPublic; import static net.bytebuddy.matcher.ElementMatchers.named; @@ -44,10 +45,8 @@ public static void onSchedule( @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { Context parentContext = currentContext(); - request = new XxlJobProcessRequest(); - request.setGlueTypeEnum(glueTypeEnum); - request.setJobId(jobId); - context = XxlJobHelper.startSpan(parentContext, request); + request = XxlJobProcessRequest.createScriptJobRequest(glueTypeEnum, jobId); + context = helper().startSpan(parentContext, request); if (context == null) { return; } @@ -61,7 +60,7 @@ public static void stopSpan( @Advice.Local("otelRequest") XxlJobProcessRequest request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - XxlJobHelper.stopSpan(result, request, throwable, scope, context); + helper().stopSpan(result, request, throwable, scope, context); } } } diff --git a/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/SimpleJobHandlerInstrumentation.java b/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/SimpleJobHandlerInstrumentation.java index dd9512dc5600..acd0cd6494c2 100644 --- a/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/SimpleJobHandlerInstrumentation.java +++ b/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/SimpleJobHandlerInstrumentation.java @@ -9,13 +9,13 @@ import static io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobConstants.XXL_GLUE_JOB_HANDLER; import static io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobConstants.XXL_METHOD_JOB_HANDLER; import static io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobConstants.XXL_SCRIPT_JOB_HANDLER; +import static io.opentelemetry.javaagent.instrumentation.xxljob.v2_1_2.XxlJobSingletons.helper; import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; import static net.bytebuddy.matcher.ElementMatchers.isPublic; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; import static net.bytebuddy.matcher.ElementMatchers.not; -import com.xxl.job.core.glue.GlueTypeEnum; import com.xxl.job.core.handler.IJobHandler; import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; @@ -52,10 +52,8 @@ public static void onSchedule( @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { Context parentContext = currentContext(); - request = new XxlJobProcessRequest(); - request.setDeclaringClass(handler.getClass()); - request.setGlueTypeEnum(GlueTypeEnum.BEAN); - context = XxlJobHelper.startSpan(parentContext, request); + request = XxlJobProcessRequest.createSimpleJobRequest(handler); + context = helper().startSpan(parentContext, request); if (context == null) { return; } @@ -70,7 +68,7 @@ public static void stopSpan( @Advice.Local("otelRequest") XxlJobProcessRequest request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - XxlJobHelper.stopSpan(result, request, throwable, scope, context); + helper().stopSpan(result, request, throwable, scope, context); } } } diff --git a/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/XxlJobHelper.java b/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/XxlJobHelper.java deleted file mode 100644 index b592947b1a63..000000000000 --- a/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/XxlJobHelper.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.xxljob.v2_1_2; - -import static io.opentelemetry.javaagent.instrumentation.xxljob.v2_1_2.XxlJobSingletons.instrumenter; - -import com.xxl.job.core.biz.model.ReturnT; -import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; -import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobProcessRequest; - -public class XxlJobHelper { - - private XxlJobHelper() {} - - public static Context startSpan(Context parentContext, XxlJobProcessRequest request) { - if (!instrumenter().shouldStart(parentContext, request)) { - return null; - } - return instrumenter().start(parentContext, request); - } - - public static void stopSpan( - Object result, - XxlJobProcessRequest request, - Throwable throwable, - Scope scope, - Context context) { - if (scope == null) { - return; - } - if (result != null && (result instanceof ReturnT)) { - ReturnT res = (ReturnT) result; - if (res.getCode() == ReturnT.FAIL_CODE) { - request.setSchedulingSuccess(Boolean.FALSE); - } - } - if (throwable != null) { - request.setSchedulingSuccess(Boolean.FALSE); - } - scope.close(); - instrumenter().end(context, request, null, throwable); - } -} diff --git a/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/XxlJobSingletons.java b/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/XxlJobSingletons.java index 4793a7a9e809..d88fa2733031 100644 --- a/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/XxlJobSingletons.java +++ b/instrumentation/xxl-job/xxl-job-2.1.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_1_2/XxlJobSingletons.java @@ -5,52 +5,29 @@ package io.opentelemetry.javaagent.instrumentation.xxljob.v2_1_2; -import io.opentelemetry.api.GlobalOpenTelemetry; -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.api.trace.StatusCode; -import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesExtractor; -import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; +import com.xxl.job.core.biz.model.ReturnT; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; -import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; -import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig; -import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobCodeAttributesGetter; -import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobExperimentalAttributeExtractor; +import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobHelper; +import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobInstrumenterFactory; import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobProcessRequest; -import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobSpanNameExtractor; public final class XxlJobSingletons { private static final String INSTRUMENTATION_NAME = "io.opentelemetry.xxl-job-2.1.2"; - - private static final boolean CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES = - InstrumentationConfig.get() - .getBoolean("otel.instrumentation.xxl-job.experimental-span-attributes", false); - - private static final Instrumenter INSTRUMENTER; - - static { - XxlJobSpanNameExtractor spanNameExtractor = new XxlJobSpanNameExtractor(); - InstrumenterBuilder builder = - Instrumenter.builder( - GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, spanNameExtractor) - .addAttributesExtractor( - CodeAttributesExtractor.create(new XxlJobCodeAttributesGetter())) - .setSpanStatusExtractor( - (spanStatusBuilder, xxlJobProcessRequest, response, error) -> { - if (error != null - || Boolean.FALSE.equals(xxlJobProcessRequest.getSchedulingSuccess())) { - spanStatusBuilder.setStatus(StatusCode.ERROR); - } - }); - if (CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { - builder.addAttributesExtractor( - AttributesExtractor.constant(AttributeKey.stringKey("job.system"), "xxl-job")); - builder.addAttributesExtractor(new XxlJobExperimentalAttributeExtractor()); - } - INSTRUMENTER = builder.buildInstrumenter(); - } - - public static Instrumenter instrumenter() { - return INSTRUMENTER; + private static final Instrumenter INSTRUMENTER = + XxlJobInstrumenterFactory.create(INSTRUMENTATION_NAME); + private static final XxlJobHelper HELPER = + XxlJobHelper.create( + INSTRUMENTER, + object -> { + if (object != null && (object instanceof ReturnT)) { + ReturnT result = (ReturnT) object; + return result.getCode() == ReturnT.FAIL_CODE; + } + return false; + }); + + public static XxlJobHelper helper() { + return HELPER; } private XxlJobSingletons() {} diff --git a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/build.gradle.kts b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/build.gradle.kts index 2cc0b34a6936..56e5c7a630bb 100644 --- a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/build.gradle.kts +++ b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/build.gradle.kts @@ -16,6 +16,10 @@ dependencies { exclude("org.codehaus.groovy", "groovy") } implementation(project(":instrumentation:xxl-job:xxl-job-common:javaagent")) + + testInstrumentation(project(":instrumentation:xxl-job:xxl-job-2.1.2:javaagent")) + testInstrumentation(project(":instrumentation:xxl-job:xxl-job-2.3.0:javaagent")) + testImplementation(project(":instrumentation:xxl-job:xxl-job-common:testing")) } diff --git a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/GlueJobHandlerInstrumentation.java b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/GlueJobHandlerInstrumentation.java index 6626e29b6b13..7e67cd14e066 100644 --- a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/GlueJobHandlerInstrumentation.java +++ b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/GlueJobHandlerInstrumentation.java @@ -6,11 +6,11 @@ package io.opentelemetry.javaagent.instrumentation.xxljob.v2_3_0; import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; +import static io.opentelemetry.javaagent.instrumentation.xxljob.v2_3_0.XxlJobSingletons.helper; import static net.bytebuddy.matcher.ElementMatchers.isPublic; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments; -import com.xxl.job.core.glue.GlueTypeEnum; import com.xxl.job.core.handler.IJobHandler; import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; @@ -45,10 +45,8 @@ public static void onSchedule( @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { Context parentContext = currentContext(); - request = new XxlJobProcessRequest(); - request.setDeclaringClass(handler.getClass()); - request.setGlueTypeEnum(GlueTypeEnum.GLUE_GROOVY); - context = XxlJobHelper.startSpan(parentContext, request); + request = XxlJobProcessRequest.createGlueJobRequest(handler); + context = helper().startSpan(parentContext, request); if (context == null) { return; } @@ -61,7 +59,7 @@ public static void stopSpan( @Advice.Local("otelRequest") XxlJobProcessRequest request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - XxlJobHelper.stopSpan(request, throwable, scope, context); + helper().stopSpan(request, throwable, scope, context); } } } diff --git a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/MethodJobHandlerInstrumentation.java b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/MethodJobHandlerInstrumentation.java index 087cafc7e017..04d39252acaa 100644 --- a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/MethodJobHandlerInstrumentation.java +++ b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/MethodJobHandlerInstrumentation.java @@ -6,12 +6,11 @@ package io.opentelemetry.javaagent.instrumentation.xxljob.v2_3_0; import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; +import static io.opentelemetry.javaagent.instrumentation.xxljob.v2_3_0.XxlJobSingletons.helper; import static net.bytebuddy.matcher.ElementMatchers.isPublic; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments; -import com.xxl.job.core.glue.GlueTypeEnum; -import com.xxl.job.core.handler.annotation.XxlJob; import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; @@ -40,24 +39,14 @@ public void transform(TypeTransformer transformer) { public static class ScheduleAdvice { @Advice.OnMethodEnter(suppress = Throwable.class) public static void onSchedule( - @Advice.FieldValue("target") Object declaringClass, + @Advice.FieldValue("target") Object target, @Advice.FieldValue("method") Method method, @Advice.Local("otelRequest") XxlJobProcessRequest request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { Context parentContext = currentContext(); - request = new XxlJobProcessRequest(); - request.setGlueTypeEnum(GlueTypeEnum.BEAN); - if (method != null) { - XxlJob xxlJobAnnotation = method.getAnnotation(XxlJob.class); - String annotationName = method.getName(); - if (xxlJobAnnotation != null) { - annotationName = xxlJobAnnotation.value(); - } - request.setMethodName(annotationName); - } - request.setDeclaringClass(declaringClass.getClass()); - context = XxlJobHelper.startSpan(parentContext, request); + request = XxlJobProcessRequest.createMethodJobRequest(target, method); + context = helper().startSpan(parentContext, request); if (context == null) { return; } @@ -70,7 +59,7 @@ public static void stopSpan( @Advice.Local("otelRequest") XxlJobProcessRequest request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - XxlJobHelper.stopSpan(request, throwable, scope, context); + helper().stopSpan(request, throwable, scope, context); } } } diff --git a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/ScriptJobHandlerInstrumentation.java b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/ScriptJobHandlerInstrumentation.java index 1ab4d722551c..a2d4e06c9876 100644 --- a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/ScriptJobHandlerInstrumentation.java +++ b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/ScriptJobHandlerInstrumentation.java @@ -6,6 +6,7 @@ package io.opentelemetry.javaagent.instrumentation.xxljob.v2_3_0; import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; +import static io.opentelemetry.javaagent.instrumentation.xxljob.v2_3_0.XxlJobSingletons.helper; import static net.bytebuddy.matcher.ElementMatchers.isPublic; import static net.bytebuddy.matcher.ElementMatchers.named; import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments; @@ -45,10 +46,8 @@ public static void onSchedule( @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { Context parentContext = currentContext(); - request = new XxlJobProcessRequest(); - request.setGlueTypeEnum(glueTypeEnum); - request.setJobId(jobId); - context = XxlJobHelper.startSpan(parentContext, request); + request = XxlJobProcessRequest.createScriptJobRequest(glueTypeEnum, jobId); + context = helper().startSpan(parentContext, request); if (context == null) { return; } @@ -61,7 +60,7 @@ public static void stopSpan( @Advice.Local("otelRequest") XxlJobProcessRequest request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - XxlJobHelper.stopSpan(request, throwable, scope, context); + helper().stopSpan(request, throwable, scope, context); } } } diff --git a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/SimpleJobHandlerInstrumentation.java b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/SimpleJobHandlerInstrumentation.java index 084932c7352b..3e2b6a0ae882 100644 --- a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/SimpleJobHandlerInstrumentation.java +++ b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/SimpleJobHandlerInstrumentation.java @@ -9,6 +9,7 @@ import static io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobConstants.XXL_GLUE_JOB_HANDLER; import static io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobConstants.XXL_METHOD_JOB_HANDLER; import static io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobConstants.XXL_SCRIPT_JOB_HANDLER; +import static io.opentelemetry.javaagent.instrumentation.xxljob.v2_3_0.XxlJobSingletons.helper; import static net.bytebuddy.matcher.ElementMatchers.hasSuperType; import static net.bytebuddy.matcher.ElementMatchers.isPublic; import static net.bytebuddy.matcher.ElementMatchers.named; @@ -16,7 +17,6 @@ import static net.bytebuddy.matcher.ElementMatchers.not; import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments; -import com.xxl.job.core.glue.GlueTypeEnum; import com.xxl.job.core.handler.IJobHandler; import io.opentelemetry.context.Context; import io.opentelemetry.context.Scope; @@ -52,10 +52,8 @@ public static void onSchedule( @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { Context parentContext = currentContext(); - request = new XxlJobProcessRequest(); - request.setDeclaringClass(handler.getClass()); - request.setGlueTypeEnum(GlueTypeEnum.BEAN); - context = XxlJobHelper.startSpan(parentContext, request); + request = XxlJobProcessRequest.createSimpleJobRequest(handler); + context = helper().startSpan(parentContext, request); if (context == null) { return; } @@ -69,7 +67,7 @@ public static void stopSpan( @Advice.Local("otelRequest") XxlJobProcessRequest request, @Advice.Local("otelContext") Context context, @Advice.Local("otelScope") Scope scope) { - XxlJobHelper.stopSpan(request, throwable, scope, context); + helper().stopSpan(request, throwable, scope, context); } } } diff --git a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/XxlJobHelper.java b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/XxlJobHelper.java deleted file mode 100644 index 8fbb7eb05287..000000000000 --- a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/XxlJobHelper.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright The OpenTelemetry Authors - * SPDX-License-Identifier: Apache-2.0 - */ - -package io.opentelemetry.javaagent.instrumentation.xxljob.v2_3_0; - -import static com.xxl.job.core.context.XxlJobContext.HANDLE_COCE_SUCCESS; -import static io.opentelemetry.javaagent.instrumentation.xxljob.v2_3_0.XxlJobSingletons.instrumenter; - -import com.xxl.job.core.context.XxlJobContext; -import io.opentelemetry.context.Context; -import io.opentelemetry.context.Scope; -import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobProcessRequest; - -public class XxlJobHelper { - - private XxlJobHelper() {} - - public static Context startSpan(Context parentContext, XxlJobProcessRequest request) { - if (!instrumenter().shouldStart(parentContext, request)) { - return null; - } - return instrumenter().start(parentContext, request); - } - - public static void stopSpan( - XxlJobProcessRequest request, Throwable throwable, Scope scope, Context context) { - if (scope == null) { - return; - } - // From 2.3.0, XxlJobContext is used to store the result of the job execution. - XxlJobContext xxlJobContext = XxlJobContext.getXxlJobContext(); - if (xxlJobContext != null) { - int handleCode = xxlJobContext.getHandleCode(); - if (handleCode != HANDLE_COCE_SUCCESS) { - request.setSchedulingSuccess(Boolean.FALSE); - } - } - if (throwable != null) { - request.setSchedulingSuccess(Boolean.FALSE); - } - scope.close(); - instrumenter().end(context, request, null, throwable); - } -} diff --git a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/XxlJobInstrumentationModule.java b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/XxlJobInstrumentationModule.java index 89b950943793..3b17798a47ea 100644 --- a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/XxlJobInstrumentationModule.java +++ b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/XxlJobInstrumentationModule.java @@ -23,8 +23,8 @@ public XxlJobInstrumentationModule() { @Override public ElementMatcher.Junction classLoaderMatcher() { - return hasClassesNamed("com.xxl.job.core.handler.impl.MethodJobHandler") - .and(hasClassesNamed("com.xxl.job.core.context.XxlJobHelper")); + return hasClassesNamed( + "com.xxl.job.core.handler.impl.MethodJobHandler", "com.xxl.job.core.context.XxlJobHelper"); } @Override diff --git a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/XxlJobSingletons.java b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/XxlJobSingletons.java index e2fb1882bf07..884e0cd4f37d 100644 --- a/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/XxlJobSingletons.java +++ b/instrumentation/xxl-job/xxl-job-2.3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/v2_3_0/XxlJobSingletons.java @@ -5,52 +5,33 @@ package io.opentelemetry.javaagent.instrumentation.xxljob.v2_3_0; -import io.opentelemetry.api.GlobalOpenTelemetry; -import io.opentelemetry.api.common.AttributeKey; -import io.opentelemetry.api.trace.StatusCode; -import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesExtractor; -import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; +import static com.xxl.job.core.context.XxlJobContext.HANDLE_COCE_SUCCESS; + +import com.xxl.job.core.context.XxlJobContext; import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; -import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; -import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig; -import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobCodeAttributesGetter; -import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobExperimentalAttributeExtractor; +import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobHelper; +import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobInstrumenterFactory; import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobProcessRequest; -import io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobSpanNameExtractor; public final class XxlJobSingletons { private static final String INSTRUMENTATION_NAME = "io.opentelemetry.xxl-job-2.3.0"; - - private static final boolean CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES = - InstrumentationConfig.get() - .getBoolean("otel.instrumentation.xxl-job.experimental-span-attributes", false); - - private static final Instrumenter INSTRUMENTER; - - static { - XxlJobSpanNameExtractor spanNameExtractor = new XxlJobSpanNameExtractor(); - InstrumenterBuilder builder = - Instrumenter.builder( - GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, spanNameExtractor) - .addAttributesExtractor( - CodeAttributesExtractor.create(new XxlJobCodeAttributesGetter())) - .setSpanStatusExtractor( - (spanStatusBuilder, xxlJobProcessRequest, response, error) -> { - if (error != null - || Boolean.FALSE.equals(xxlJobProcessRequest.getSchedulingSuccess())) { - spanStatusBuilder.setStatus(StatusCode.ERROR); - } - }); - if (CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { - builder.addAttributesExtractor( - AttributesExtractor.constant(AttributeKey.stringKey("job.system"), "xxl-job")); - builder.addAttributesExtractor(new XxlJobExperimentalAttributeExtractor()); - } - INSTRUMENTER = builder.buildInstrumenter(); - } - - public static Instrumenter instrumenter() { - return INSTRUMENTER; + private static final Instrumenter INSTRUMENTER = + XxlJobInstrumenterFactory.create(INSTRUMENTATION_NAME); + private static final XxlJobHelper HELPER = + XxlJobHelper.create( + INSTRUMENTER, + unused -> { + // From 2.3.0, XxlJobContext is used to store the result of the job execution. + XxlJobContext xxlJobContext = XxlJobContext.getXxlJobContext(); + if (xxlJobContext != null) { + int handleCode = xxlJobContext.getHandleCode(); + return handleCode != HANDLE_COCE_SUCCESS; + } + return false; + }); + + public static XxlJobHelper helper() { + return HELPER; } private XxlJobSingletons() {} diff --git a/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobCodeAttributesGetter.java b/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobCodeAttributesGetter.java index cfe972a2288d..8f02df02af98 100644 --- a/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobCodeAttributesGetter.java +++ b/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobCodeAttributesGetter.java @@ -11,12 +11,12 @@ import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesGetter; import javax.annotation.Nullable; -public class XxlJobCodeAttributesGetter implements CodeAttributesGetter { +class XxlJobCodeAttributesGetter implements CodeAttributesGetter { @Nullable @Override public Class getCodeClass(XxlJobProcessRequest xxlJobProcessRequest) { - GlueTypeEnum glueTypeEnum = xxlJobProcessRequest.getGlueTypeEnum(); + GlueTypeEnum glueTypeEnum = xxlJobProcessRequest.getGlueType(); if (!SCRIPT_JOB_TYPE.contains(glueTypeEnum.getDesc())) { return xxlJobProcessRequest.getDeclaringClass(); } @@ -26,15 +26,10 @@ public Class getCodeClass(XxlJobProcessRequest xxlJobProcessRequest) { @Nullable @Override public String getMethodName(XxlJobProcessRequest xxlJobProcessRequest) { - GlueTypeEnum glueTypeEnum = xxlJobProcessRequest.getGlueTypeEnum(); + GlueTypeEnum glueTypeEnum = xxlJobProcessRequest.getGlueType(); if (!SCRIPT_JOB_TYPE.contains(glueTypeEnum.getDesc())) { - String methodName = xxlJobProcessRequest.getMethodName(); - if (methodName == null || methodName.isEmpty()) { - return "execute"; - } else { - return methodName; - } + return xxlJobProcessRequest.getMethodName(); } - return "ID-" + xxlJobProcessRequest.getJobId(); + return null; } } diff --git a/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobConstants.java b/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobConstants.java index da11fb2a0b47..e1c8aba732d9 100644 --- a/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobConstants.java +++ b/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobConstants.java @@ -11,7 +11,7 @@ import java.util.HashSet; import java.util.Set; -public class XxlJobConstants { +public final class XxlJobConstants { private XxlJobConstants() {} diff --git a/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobExperimentalAttributeExtractor.java b/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobExperimentalAttributeExtractor.java index 4dc1a59ffc82..53e4de92e144 100644 --- a/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobExperimentalAttributeExtractor.java +++ b/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobExperimentalAttributeExtractor.java @@ -12,7 +12,7 @@ import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; import javax.annotation.Nullable; -public class XxlJobExperimentalAttributeExtractor +class XxlJobExperimentalAttributeExtractor implements AttributesExtractor { private static final AttributeKey XXL_JOB_GLUE_TYPE = @@ -23,7 +23,7 @@ public void onStart( AttributesBuilder attributes, Context parentContext, XxlJobProcessRequest xxlJobProcessRequest) { - GlueTypeEnum glueTypeEnum = xxlJobProcessRequest.getGlueTypeEnum(); + GlueTypeEnum glueTypeEnum = xxlJobProcessRequest.getGlueType(); attributes.put(XXL_JOB_GLUE_TYPE, glueTypeEnum.getDesc()); } diff --git a/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobHelper.java b/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobHelper.java new file mode 100644 index 000000000000..3b90e1d3ad44 --- /dev/null +++ b/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobHelper.java @@ -0,0 +1,57 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.xxljob.common; + +import io.opentelemetry.context.Context; +import io.opentelemetry.context.Scope; +import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; +import java.util.function.Predicate; + +public final class XxlJobHelper { + private final Instrumenter instrumenter; + private final Predicate failedStatusPredicate; + + private XxlJobHelper( + Instrumenter instrumenter, + Predicate failedStatusPredicate) { + this.instrumenter = instrumenter; + this.failedStatusPredicate = failedStatusPredicate; + } + + public static XxlJobHelper create( + Instrumenter instrumenter, + Predicate failedStatusPredicate) { + return new XxlJobHelper(instrumenter, failedStatusPredicate); + } + + public Context startSpan(Context parentContext, XxlJobProcessRequest request) { + if (!instrumenter.shouldStart(parentContext, request)) { + return null; + } + return instrumenter.start(parentContext, request); + } + + public void stopSpan( + Object result, + XxlJobProcessRequest request, + Throwable throwable, + Scope scope, + Context context) { + if (scope == null) { + return; + } + if (failedStatusPredicate.test(result)) { + request.setFailed(); + } + scope.close(); + instrumenter.end(context, request, null, throwable); + } + + public void stopSpan( + XxlJobProcessRequest request, Throwable throwable, Scope scope, Context context) { + stopSpan(null, request, throwable, scope, context); + } +} diff --git a/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobInstrumenterFactory.java b/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobInstrumenterFactory.java new file mode 100644 index 000000000000..4a5f713808e2 --- /dev/null +++ b/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobInstrumenterFactory.java @@ -0,0 +1,45 @@ +/* + * Copyright The OpenTelemetry Authors + * SPDX-License-Identifier: Apache-2.0 + */ + +package io.opentelemetry.javaagent.instrumentation.xxljob.common; + +import io.opentelemetry.api.GlobalOpenTelemetry; +import io.opentelemetry.api.common.AttributeKey; +import io.opentelemetry.api.trace.StatusCode; +import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; +import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; +import io.opentelemetry.instrumentation.api.instrumenter.InstrumenterBuilder; +import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig; + +public final class XxlJobInstrumenterFactory { + + private static final boolean CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES = + InstrumentationConfig.get() + .getBoolean("otel.instrumentation.xxl-job.experimental-span-attributes", false); + + public static Instrumenter create(String instrumentationName) { + XxlJobCodeAttributesGetter codeAttributesGetter = new XxlJobCodeAttributesGetter(); + XxlJobSpanNameExtractor spanNameExtractor = new XxlJobSpanNameExtractor(codeAttributesGetter); + InstrumenterBuilder builder = + Instrumenter.builder( + GlobalOpenTelemetry.get(), instrumentationName, spanNameExtractor) + .addAttributesExtractor(CodeAttributesExtractor.create(codeAttributesGetter)) + .setSpanStatusExtractor( + (spanStatusBuilder, xxlJobProcessRequest, response, error) -> { + if (error != null || xxlJobProcessRequest.isFailed()) { + spanStatusBuilder.setStatus(StatusCode.ERROR); + } + }); + if (CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { + builder.addAttributesExtractor( + AttributesExtractor.constant(AttributeKey.stringKey("job.system"), "xxl-job")); + builder.addAttributesExtractor(new XxlJobExperimentalAttributeExtractor()); + } + return builder.buildInstrumenter(); + } + + private XxlJobInstrumenterFactory() {} +} diff --git a/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobProcessRequest.java b/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobProcessRequest.java index 7bea58a5afec..b758c584387c 100644 --- a/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobProcessRequest.java +++ b/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobProcessRequest.java @@ -6,56 +6,71 @@ package io.opentelemetry.javaagent.instrumentation.xxljob.common; import com.xxl.job.core.glue.GlueTypeEnum; +import com.xxl.job.core.handler.IJobHandler; +import java.lang.reflect.Method; -public class XxlJobProcessRequest { +public final class XxlJobProcessRequest { private String methodName; - private int jobId; - private Class declaringClass; + private boolean failed; + private final GlueTypeEnum glueType; - private Boolean schedulingSuccess = Boolean.TRUE; + private XxlJobProcessRequest(GlueTypeEnum glueType) { + this.glueType = glueType; + } - private GlueTypeEnum glueTypeEnum; + public static XxlJobProcessRequest createRequestForMethod( + GlueTypeEnum glueType, Class declaringClass, String methodName) { + XxlJobProcessRequest request = new XxlJobProcessRequest(glueType); + request.declaringClass = declaringClass; + request.methodName = methodName; - public Boolean getSchedulingSuccess() { - return schedulingSuccess; + return request; } - public void setSchedulingSuccess(Boolean schedulingSuccess) { - this.schedulingSuccess = schedulingSuccess; + public static XxlJobProcessRequest createGlueJobRequest(IJobHandler handler) { + return createRequestForMethod(GlueTypeEnum.GLUE_GROOVY, handler.getClass(), "execute"); } - public String getMethodName() { - return methodName; + public static XxlJobProcessRequest createScriptJobRequest(GlueTypeEnum glueTypeEnum, int jobId) { + XxlJobProcessRequest request = new XxlJobProcessRequest(glueTypeEnum); + request.jobId = jobId; + + return request; } - public void setMethodName(String methodName) { - this.methodName = methodName; + public static XxlJobProcessRequest createSimpleJobRequest(IJobHandler handler) { + return createRequestForMethod(GlueTypeEnum.BEAN, handler.getClass(), "execute"); } - public int getJobId() { - return jobId; + public static XxlJobProcessRequest createMethodJobRequest(Object target, Method method) { + return createRequestForMethod( + GlueTypeEnum.BEAN, target.getClass(), method != null ? method.getName() : null); } - public void setJobId(int jobId) { - this.jobId = jobId; + public void setFailed() { + failed = true; } - public Class getDeclaringClass() { - return declaringClass; + public boolean isFailed() { + return failed; } - public void setDeclaringClass(Class declaringClass) { - this.declaringClass = declaringClass; + public String getMethodName() { + return methodName; } - public GlueTypeEnum getGlueTypeEnum() { - return glueTypeEnum; + public int getJobId() { + return jobId; + } + + public Class getDeclaringClass() { + return declaringClass; } - public void setGlueTypeEnum(GlueTypeEnum glueTypeEnum) { - this.glueTypeEnum = glueTypeEnum; + public GlueTypeEnum getGlueType() { + return glueType; } } diff --git a/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobSpanNameExtractor.java b/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobSpanNameExtractor.java index 87498d609be7..4e5471c8391a 100644 --- a/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobSpanNameExtractor.java +++ b/instrumentation/xxl-job/xxl-job-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/xxljob/common/XxlJobSpanNameExtractor.java @@ -8,26 +8,23 @@ import static io.opentelemetry.javaagent.instrumentation.xxljob.common.XxlJobConstants.SCRIPT_JOB_TYPE; import com.xxl.job.core.glue.GlueTypeEnum; -import io.opentelemetry.api.internal.StringUtils; +import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesGetter; +import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeSpanNameExtractor; import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor; -public class XxlJobSpanNameExtractor implements SpanNameExtractor { +class XxlJobSpanNameExtractor implements SpanNameExtractor { + private final SpanNameExtractor codeSpanNameExtractor; + + XxlJobSpanNameExtractor(CodeAttributesGetter getter) { + codeSpanNameExtractor = CodeSpanNameExtractor.create(getter); + } @Override public String extract(XxlJobProcessRequest request) { - GlueTypeEnum glueTypeEnum = request.getGlueTypeEnum(); + GlueTypeEnum glueTypeEnum = request.getGlueType(); if (SCRIPT_JOB_TYPE.contains(glueTypeEnum.getDesc())) { return glueTypeEnum.getDesc() + ".ID-" + request.getJobId(); } - String methodName = request.getMethodName(); - if (StringUtils.isNullOrEmpty(methodName)) { - methodName = "execute"; - } - Class declaringClass = request.getDeclaringClass(); - if (declaringClass != null) { - return declaringClass.getSimpleName() + "." + methodName; - } else { - return "unknown" + "." + methodName; - } + return codeSpanNameExtractor.extract(request); } } diff --git a/instrumentation/xxl-job/xxl-job-common/testing/src/main/java/io/opentelemetry/instrumentation/xxljob/AbstractXxlJobTest.java b/instrumentation/xxl-job/xxl-job-common/testing/src/main/java/io/opentelemetry/instrumentation/xxljob/AbstractXxlJobTest.java index 0f4348e78e03..4ad61288fd9c 100644 --- a/instrumentation/xxl-job/xxl-job-common/testing/src/main/java/io/opentelemetry/instrumentation/xxljob/AbstractXxlJobTest.java +++ b/instrumentation/xxl-job/xxl-job-common/testing/src/main/java/io/opentelemetry/instrumentation/xxljob/AbstractXxlJobTest.java @@ -16,7 +16,13 @@ import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; +import io.opentelemetry.sdk.testing.assertj.AttributeAssertion; import io.opentelemetry.sdk.trace.data.StatusData; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import org.junit.jupiter.api.Assumptions; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.RegisterExtension; @@ -25,6 +31,11 @@ public abstract class AbstractXxlJobTest { @RegisterExtension private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); + @BeforeAll + static void setUp() { + XxlJobFileAppender.initLogPath("build/xxljob/log"); + } + @Test void testGlueJob() { JobThread jobThread = new JobThread(1, getGlueJobHandler()); @@ -35,23 +46,22 @@ void testGlueJob() { checkXxlJob( "CustomizedGroovyHandler.execute", StatusData.unset(), - "execute", - GlueTypeEnum.GLUE_GROOVY.getDesc(), - "CustomizedGroovyHandler"); + GlueTypeEnum.GLUE_GROOVY, + "CustomizedGroovyHandler", + "execute"); jobThread.toStop("Test finish"); } @Test void testScriptJob() { - XxlJobFileAppender.initLogPath("resources/test/log"); JobThread jobThread = new JobThread(2, getScriptJobHandler()); TriggerParam triggerParam = new TriggerParam(); triggerParam.setExecutorParams(""); triggerParam.setExecutorTimeout(0); jobThread.pushTriggerQueue(triggerParam); jobThread.start(); - checkXxlJobWithoutNamespace( - "GLUE(Shell).ID-2", StatusData.unset(), "ID-2", GlueTypeEnum.GLUE_SHELL.getDesc()); + checkXxlJobWithoutCodeAttributes( + "GLUE(Shell).ID-2", StatusData.unset(), GlueTypeEnum.GLUE_SHELL); jobThread.toStop("Test finish"); } @@ -65,14 +75,17 @@ void testSimpleJob() { checkXxlJob( "SimpleCustomizedHandler.execute", StatusData.unset(), - "execute", - GlueTypeEnum.BEAN.getDesc(), - getPackageName() + ".SimpleCustomizedHandler"); + GlueTypeEnum.BEAN, + getPackageName() + ".SimpleCustomizedHandler", + "execute"); jobThread.toStop("Test finish"); } @Test public void testMethodJob() { + // method handle is null if test is not supported by tested version of the library + Assumptions.assumeTrue(getMethodHandler() != null); + JobThread jobThread = new JobThread(4, getMethodHandler()); TriggerParam triggerParam = new TriggerParam(); triggerParam.setExecutorTimeout(0); @@ -81,9 +94,9 @@ public void testMethodJob() { checkXxlJob( "ReflectObject.echo", StatusData.unset(), - "echo", - GlueTypeEnum.BEAN.getDesc(), - "io.opentelemetry.instrumentation.xxljob.ReflectiveMethodsFactory$ReflectObject"); + GlueTypeEnum.BEAN, + "io.opentelemetry.instrumentation.xxljob.ReflectiveMethodsFactory$ReflectObject", + "echo"); jobThread.toStop("Test finish"); } @@ -97,9 +110,9 @@ void testFailedJob() { checkXxlJob( "CustomizedFailedHandler.execute", StatusData.error(), - "execute", - GlueTypeEnum.BEAN.getDesc(), - getPackageName() + ".CustomizedFailedHandler"); + GlueTypeEnum.BEAN, + getPackageName() + ".CustomizedFailedHandler", + "execute"); jobThread.toStop("Test finish"); } @@ -115,39 +128,40 @@ void testFailedJob() { protected abstract String getPackageName(); - private static void checkXxlJob(String spanName, StatusData statusData, String... attributes) { + private static void checkXxlJob( + String spanName, StatusData statusData, List assertions) { testing.waitAndAssertTraces( - trace -> { - trace.hasSpansSatisfyingExactly( - span -> { - span.hasKind(SpanKind.INTERNAL) - .hasName(spanName) - .hasStatus(statusData) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("job.system"), "xxl-job"), - equalTo(AttributeKey.stringKey("code.function"), attributes[0]), - equalTo( - AttributeKey.stringKey("scheduling.xxl-job.glue.type"), attributes[1]), - equalTo(AttributeKey.stringKey("code.namespace"), attributes[2])); - }); - }); + trace -> + trace.hasSpansSatisfyingExactly( + span -> + span.hasKind(SpanKind.INTERNAL) + .hasName(spanName) + .hasStatus(statusData) + .hasAttributesSatisfyingExactly(assertions))); } - private static void checkXxlJobWithoutNamespace( - String spanName, StatusData statusData, String... attributes) { - testing.waitAndAssertTraces( - trace -> { - trace.hasSpansSatisfyingExactly( - span -> { - span.hasKind(SpanKind.INTERNAL) - .hasName(spanName) - .hasStatus(statusData) - .hasAttributesSatisfying( - equalTo(AttributeKey.stringKey("job.system"), "xxl-job"), - equalTo(AttributeKey.stringKey("code.function"), attributes[0]), - equalTo( - AttributeKey.stringKey("scheduling.xxl-job.glue.type"), attributes[1])); - }); - }); + private static void checkXxlJob( + String spanName, + StatusData statusData, + GlueTypeEnum glueType, + String codeNamespace, + String codeFunction) { + List attributeAssertions = new ArrayList<>(); + attributeAssertions.addAll(attributeAssertions(glueType)); + attributeAssertions.add(equalTo(AttributeKey.stringKey("code.namespace"), codeNamespace)); + attributeAssertions.add(equalTo(AttributeKey.stringKey("code.function"), codeFunction)); + + checkXxlJob(spanName, statusData, attributeAssertions); + } + + private static void checkXxlJobWithoutCodeAttributes( + String spanName, StatusData statusData, GlueTypeEnum glueType) { + checkXxlJob(spanName, statusData, attributeAssertions(glueType)); + } + + private static List attributeAssertions(GlueTypeEnum glueType) { + return Arrays.asList( + equalTo(AttributeKey.stringKey("job.system"), "xxl-job"), + equalTo(AttributeKey.stringKey("scheduling.xxl-job.glue.type"), glueType.getDesc())); } }