Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework xxl job instrumentation #6

Merged
merged 2 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions instrumentation/xxl-job/README.md
Original file line number Diff line number Diff line change
@@ -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. |
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -33,7 +32,7 @@ public ElementMatcher<TypeDescription> 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");
}

Expand All @@ -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;
}
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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<XxlJobProcessRequest, Void> INSTRUMENTER;

static {
XxlJobSpanNameExtractor spanNameExtractor = new XxlJobSpanNameExtractor();
InstrumenterBuilder<XxlJobProcessRequest, Void> builder =
Instrumenter.<XxlJobProcessRequest, Void>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<XxlJobProcessRequest, Void> instrumenter() {
return INSTRUMENTER;
private static final Instrumenter<XxlJobProcessRequest, Void> 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"})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
}
}
Loading
Loading