Skip to content

Commit 150a7cc

Browse files
committed
review
1 parent 149ef02 commit 150a7cc

File tree

10 files changed

+91
-158
lines changed

10 files changed

+91
-158
lines changed

instrumentation/apache-elasticjob-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apacheelasticjob/v3_0/DataflowJobExecutorInstrumentation.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,20 +51,15 @@ public static ElasticJobHelper.ElasticJobScope onEnter(
5151

5252
ElasticJobProcessRequest request =
5353
ElasticJobProcessRequest.createFromShardingContext(
54-
shardingContext,
55-
ElasticJobType.DATAFLOW.getValue(),
56-
elasticJob.getClass(),
57-
"processData");
54+
shardingContext, ElasticJobType.DATAFLOW, elasticJob.getClass(), "processData");
5855

5956
return helper().startSpan(request);
6057
}
6158

6259
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6360
public static void onExit(
6461
@Advice.Enter ElasticJobHelper.ElasticJobScope scope, @Advice.Thrown Throwable throwable) {
65-
if (scope != null) {
66-
helper().endSpan(scope, throwable);
67-
}
62+
helper().endSpan(scope, throwable);
6863
}
6964
}
7065
}

instrumentation/apache-elasticjob-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apacheelasticjob/v3_0/ElasticJobExecutorInstrumentation.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@
66
package io.opentelemetry.javaagent.instrumentation.apacheelasticjob.v3_0;
77

88
import static io.opentelemetry.javaagent.instrumentation.apacheelasticjob.v3_0.ElasticJobSingletons.helper;
9-
import static io.opentelemetry.javaagent.instrumentation.apacheelasticjob.v3_0.JobTypeHelper.determineJobTypeFromExecutor;
109
import static net.bytebuddy.matcher.ElementMatchers.named;
1110
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
1211

1312
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1413
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
14+
import java.util.Objects;
1515
import javax.annotation.Nullable;
1616
import net.bytebuddy.asm.Advice;
1717
import net.bytebuddy.description.type.TypeDescription;
@@ -53,7 +53,7 @@ public static ElasticJobHelper.ElasticJobScope onEnter(
5353
@Advice.Argument(1) ShardingContexts shardingContexts,
5454
@Advice.Argument(2) int item) {
5555

56-
ElasticJobType jobType = determineJobTypeFromExecutor(jobItemExecutor);
56+
ElasticJobType jobType = ElasticJobType.fromExecutor(jobItemExecutor);
5757
if (jobType != ElasticJobType.SCRIPT && jobType != ElasticJobType.HTTP) {
5858
return null;
5959
}
@@ -63,10 +63,8 @@ public static ElasticJobHelper.ElasticJobScope onEnter(
6363
shardingContexts.getTaskId(),
6464
item,
6565
shardingContexts.getShardingTotalCount(),
66-
shardingContexts.getShardingItemParameters() == null
67-
? ""
68-
: shardingContexts.getShardingItemParameters().toString(),
69-
jobType.getValue());
66+
Objects.toString(shardingContexts.getShardingItemParameters(), null),
67+
jobType);
7068
return helper().startSpan(request);
7169
}
7270

instrumentation/apache-elasticjob-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apacheelasticjob/v3_0/ElasticJobExperimentalAttributeExtractor.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import io.opentelemetry.context.Context;
1414
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
1515
import javax.annotation.Nullable;
16-
import org.apache.commons.lang3.StringUtils;
1716

1817
class ElasticJobExperimentalAttributeExtractor
1918
implements AttributesExtractor<ElasticJobProcessRequest, Void> {
@@ -39,10 +38,9 @@ public void onStart(
3938
attributes.put(ELASTICJOB_SHARDING_ITEM_INDEX, elasticJobProcessRequest.getShardingItemIndex());
4039
attributes.put(
4140
ELASTICJOB_SHARDING_TOTAL_COUNT, elasticJobProcessRequest.getShardingTotalCount());
42-
if (!StringUtils.isEmpty(elasticJobProcessRequest.getShardingItemParameters())) {
43-
attributes.put(
44-
ELASTICJOB_SHARDING_ITEM_PARAMETERS,
45-
elasticJobProcessRequest.getShardingItemParameters());
41+
String shardingItemParameters = elasticJobProcessRequest.getShardingItemParameters();
42+
if (shardingItemParameters != null) {
43+
attributes.put(ELASTICJOB_SHARDING_ITEM_PARAMETERS, shardingItemParameters);
4644
}
4745
}
4846

instrumentation/apache-elasticjob-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apacheelasticjob/v3_0/ElasticJobProcessRequest.java

Lines changed: 36 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -8,76 +8,61 @@
88
import org.apache.shardingsphere.elasticjob.api.ShardingContext;
99

1010
public final class ElasticJobProcessRequest {
11-
private String jobName;
12-
private String taskId;
13-
private int shardingItemIndex;
14-
private int shardingTotalCount;
15-
private String shardingItemParameters;
16-
private boolean failed;
17-
private String jobType;
11+
private final String jobName;
12+
private final String taskId;
13+
private final int shardingItemIndex;
14+
private final int shardingTotalCount;
15+
private final String shardingItemParameters;
16+
private final ElasticJobType jobType;
1817
private Class<?> userJobClass;
1918
private String userMethodName = "process";
2019

21-
public static ElasticJobProcessRequest create(
20+
private ElasticJobProcessRequest(
2221
String jobName,
2322
String taskId,
2423
int shardingItemIndex,
2524
int shardingTotalCount,
2625
String shardingItemParameters,
27-
String jobType) {
28-
ElasticJobProcessRequest request = new ElasticJobProcessRequest();
29-
request.jobName = jobName;
30-
request.taskId = taskId;
31-
request.shardingItemIndex = shardingItemIndex;
32-
request.shardingTotalCount = shardingTotalCount;
33-
request.shardingItemParameters = shardingItemParameters;
34-
request.jobType = jobType;
35-
return request;
26+
ElasticJobType jobType) {
27+
this.jobName = jobName;
28+
this.taskId = taskId;
29+
this.shardingItemIndex = shardingItemIndex;
30+
this.shardingTotalCount = shardingTotalCount;
31+
this.shardingItemParameters = emptyToNull(shardingItemParameters);
32+
this.jobType = jobType;
3633
}
3734

38-
public static ElasticJobProcessRequest createWithUserJobInfo(
35+
public static ElasticJobProcessRequest create(
3936
String jobName,
4037
String taskId,
4138
int shardingItemIndex,
4239
int shardingTotalCount,
4340
String shardingItemParameters,
44-
String jobType,
45-
Class<?> userJobClass,
46-
String userMethodName) {
47-
ElasticJobProcessRequest request = new ElasticJobProcessRequest();
48-
request.jobName = jobName;
49-
request.taskId = taskId;
50-
request.shardingItemIndex = shardingItemIndex;
51-
request.shardingTotalCount = shardingTotalCount;
52-
request.shardingItemParameters = shardingItemParameters;
53-
request.jobType = jobType;
54-
request.userJobClass = userJobClass;
55-
request.userMethodName = userMethodName;
56-
return request;
41+
ElasticJobType jobType) {
42+
return new ElasticJobProcessRequest(
43+
jobName, taskId, shardingItemIndex, shardingTotalCount, shardingItemParameters, jobType);
44+
}
45+
46+
private static String emptyToNull(String string) {
47+
return string == null || string.isEmpty() ? null : string;
5748
}
5849

5950
public static ElasticJobProcessRequest createFromShardingContext(
6051
ShardingContext shardingContext,
61-
String jobType,
52+
ElasticJobType jobType,
6253
Class<?> userJobClass,
6354
String userMethodName) {
64-
return createWithUserJobInfo(
65-
shardingContext.getJobName(),
66-
shardingContext.getTaskId(),
67-
shardingContext.getShardingItem(),
68-
shardingContext.getShardingTotalCount(),
69-
shardingContext.getShardingParameter(),
70-
jobType,
71-
userJobClass,
72-
userMethodName);
73-
}
74-
75-
public void setFailed() {
76-
this.failed = true;
77-
}
78-
79-
public boolean isFailed() {
80-
return this.failed;
55+
ElasticJobProcessRequest request =
56+
create(
57+
shardingContext.getJobName(),
58+
shardingContext.getTaskId(),
59+
shardingContext.getShardingItem(),
60+
shardingContext.getShardingTotalCount(),
61+
shardingContext.getShardingParameter(),
62+
jobType);
63+
request.userJobClass = userJobClass;
64+
request.userMethodName = userMethodName;
65+
return request;
8166
}
8267

8368
public String getJobName() {
@@ -100,16 +85,12 @@ public String getShardingItemParameters() {
10085
return this.shardingItemParameters;
10186
}
10287

103-
public String getJobType() {
104-
return this.jobType;
105-
}
106-
10788
public boolean isScriptJob() {
108-
return "SCRIPT".equals(this.jobType);
89+
return ElasticJobType.SCRIPT == jobType;
10990
}
11091

11192
public boolean isHttpJob() {
112-
return "HTTP".equals(this.jobType);
93+
return ElasticJobType.HTTP == jobType;
11394
}
11495

11596
public Class<?> getUserJobClass() {

instrumentation/apache-elasticjob-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apacheelasticjob/v3_0/ElasticJobSingletons.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
import io.opentelemetry.api.GlobalOpenTelemetry;
99
import io.opentelemetry.api.common.AttributeKey;
10-
import io.opentelemetry.api.trace.StatusCode;
1110
import io.opentelemetry.instrumentation.api.incubator.semconv.code.CodeAttributesExtractor;
1211
import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor;
1312
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
@@ -31,13 +30,7 @@ private static Instrumenter<ElasticJobProcessRequest, Void> createInstrumenter()
3130
GlobalOpenTelemetry.get(),
3231
INSTRUMENTATION_NAME,
3332
new ElasticJobSpanNameExtractor(codeAttributesGetter))
34-
.addAttributesExtractor(CodeAttributesExtractor.create(codeAttributesGetter))
35-
.setSpanStatusExtractor(
36-
(spanStatusBuilder, elasticJobProcessRequest, unused, error) -> {
37-
if (error != null || elasticJobProcessRequest.isFailed()) {
38-
spanStatusBuilder.setStatus(StatusCode.ERROR);
39-
}
40-
});
33+
.addAttributesExtractor(CodeAttributesExtractor.create(codeAttributesGetter));
4134
if (CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) {
4235
builder.addAttributesExtractor(
4336
AttributesExtractor.constant(AttributeKey.stringKey("job.system"), "elasticjob"));

instrumentation/apache-elasticjob-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apacheelasticjob/v3_0/ElasticJobType.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,28 @@
66
package io.opentelemetry.javaagent.instrumentation.apacheelasticjob.v3_0;
77

88
public enum ElasticJobType {
9-
SIMPLE("SIMPLE"),
10-
DATAFLOW("DATAFLOW"),
11-
HTTP("HTTP"),
12-
SCRIPT("SCRIPT"),
13-
UNKNOWN("UNKNOWN");
9+
SIMPLE,
10+
DATAFLOW,
11+
HTTP,
12+
SCRIPT,
13+
UNKNOWN;
1414

15-
private final String value;
15+
public static ElasticJobType fromExecutor(Object jobItemExecutor) {
16+
if (jobItemExecutor == null) {
17+
return UNKNOWN;
18+
}
1619

17-
ElasticJobType(String value) {
18-
this.value = value;
19-
}
20-
21-
public String getValue() {
22-
return value;
23-
}
24-
25-
@Override
26-
public String toString() {
27-
return value;
20+
switch (jobItemExecutor.getClass().getSimpleName()) {
21+
case "HttpJobExecutor":
22+
return HTTP;
23+
case "ScriptJobExecutor":
24+
return SCRIPT;
25+
case "SimpleJobExecutor":
26+
return SIMPLE;
27+
case "DataflowJobExecutor":
28+
return DATAFLOW;
29+
default:
30+
return UNKNOWN;
31+
}
2832
}
2933
}

instrumentation/apache-elasticjob-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apacheelasticjob/v3_0/JobTypeHelper.java

Lines changed: 0 additions & 36 deletions
This file was deleted.

instrumentation/apache-elasticjob-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/apacheelasticjob/v3_0/SimpleJobExecutorInstrumentation.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,17 +51,15 @@ public static ElasticJobHelper.ElasticJobScope onEnter(
5151

5252
ElasticJobProcessRequest request =
5353
ElasticJobProcessRequest.createFromShardingContext(
54-
shardingContext, ElasticJobType.SIMPLE.getValue(), elasticJob.getClass(), "execute");
54+
shardingContext, ElasticJobType.SIMPLE, elasticJob.getClass(), "execute");
5555

5656
return helper().startSpan(request);
5757
}
5858

5959
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
6060
public static void onExit(
6161
@Advice.Enter ElasticJobHelper.ElasticJobScope scope, @Advice.Thrown Throwable throwable) {
62-
if (scope != null) {
63-
helper().endSpan(scope, throwable);
64-
}
62+
helper().endSpan(scope, throwable);
6563
}
6664
}
6765
}

0 commit comments

Comments
 (0)