Skip to content

Commit 5776b56

Browse files
committed
post-review simplification
1 parent 5850a34 commit 5776b56

File tree

3 files changed

+57
-49
lines changed

3 files changed

+57
-49
lines changed

instrumentation/jaxrs/jaxrs-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v1_0/JaxrsAnnotationsInstrumentation.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import static net.bytebuddy.matcher.ElementMatchers.namedOneOf;
1818
import static net.bytebuddy.matcher.ElementMatchers.not;
1919

20-
import com.google.errorprone.annotations.CanIgnoreReturnValue;
2120
import io.opentelemetry.context.Context;
2221
import io.opentelemetry.context.Scope;
2322
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute;
@@ -70,19 +69,18 @@ public void transform(TypeTransformer transformer) {
7069
public static class JaxRsAnnotationsAdvice {
7170

7271
public static class AdviceScope {
73-
private HandlerData handlerData;
72+
private final HandlerData handlerData;
7473
private final CallDepth callDepth;
75-
private Context context;
76-
private Scope scope;
74+
private final Context context;
75+
private final Scope scope;
7776

78-
public AdviceScope(CallDepth callDepth) {
77+
public AdviceScope(CallDepth callDepth, Class<?> type, Method method) {
7978
this.callDepth = callDepth;
80-
}
81-
82-
@CanIgnoreReturnValue
83-
public AdviceScope enter(Class<?> type, Method method) {
8479
if (callDepth.getAndIncrement() > 0) {
85-
return this;
80+
handlerData = null;
81+
context = null;
82+
scope = null;
83+
return;
8684
}
8785

8886
Context parentContext = Java8BytecodeBridge.currentContext();
@@ -95,12 +93,13 @@ public AdviceScope enter(Class<?> type, Method method) {
9593
handlerData);
9694

9795
if (!instrumenter().shouldStart(parentContext, handlerData)) {
98-
return this;
96+
scope = null;
97+
context = null;
98+
return;
9999
}
100100

101101
context = instrumenter().start(parentContext, handlerData);
102102
scope = context.makeCurrent();
103-
return this;
104103
}
105104

106105
public void exit(@Nullable Throwable throwable) {
@@ -118,8 +117,7 @@ public void exit(@Nullable Throwable throwable) {
118117

119118
@Advice.OnMethodEnter(suppress = Throwable.class)
120119
public static AdviceScope nameSpan(@Advice.This Object target, @Advice.Origin Method method) {
121-
AdviceScope adviceScope = new AdviceScope(CallDepth.forClass(Path.class));
122-
return adviceScope.enter(target.getClass(), method);
120+
return new AdviceScope(CallDepth.forClass(Path.class), target.getClass(), method);
123121
}
124122

125123
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)

instrumentation/jaxrs/jaxrs-2.0/jaxrs-2.0-annotations/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v2_0/JaxrsAnnotationsInstrumentation.java

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,36 +74,42 @@ public static class JaxRsAnnotationsAdvice {
7474

7575
public static class AdviceScope {
7676
private final CallDepth callDepth;
77-
private Jaxrs2HandlerData handlerData;
78-
private AsyncResponse asyncResponse;
79-
private Context context;
80-
private Scope scope;
77+
private final Jaxrs2HandlerData handlerData;
78+
private final AsyncResponse asyncResponse;
79+
private final Context context;
80+
private final Scope scope;
8181

82-
public AdviceScope(CallDepth callDepth) {
82+
public AdviceScope(CallDepth callDepth, Object[] args, Object target, Method method) {
8383
this.callDepth = callDepth;
84-
}
85-
86-
@CanIgnoreReturnValue
87-
public AdviceScope enter(Object[] args, Object target, Method method) {
88-
if (callDepth.getAndIncrement() > 0) {
89-
return this;
84+
if (this.callDepth.getAndIncrement() > 0) {
85+
handlerData = null;
86+
context = null;
87+
scope = null;
88+
asyncResponse = null;
89+
return;
9090
}
9191

92+
AsyncResponse asyncResponseArg = null;
9293
for (Object arg : args) {
9394
if (arg instanceof AsyncResponse) {
94-
asyncResponse = (AsyncResponse) arg;
95-
if (ASYNC_RESPONSE_DATA.get(asyncResponse) != null) {
95+
asyncResponseArg = (AsyncResponse) arg;
96+
if (ASYNC_RESPONSE_DATA.get(asyncResponseArg) != null) {
9697
/*
9798
* We are probably in a recursive call and don't want to start a new span because it
9899
* would replace the existing span in the asyncResponse and cause it to never finish. We
99100
* could work around this by using a list instead, but we likely don't want the extra
100101
* span anyway.
101102
*/
102-
return this;
103+
handlerData = null;
104+
context = null;
105+
scope = null;
106+
asyncResponse = null;
107+
return;
103108
}
104109
break;
105110
}
106111
}
112+
asyncResponse = asyncResponseArg;
107113

108114
Context parentContext = Java8BytecodeBridge.currentContext();
109115
handlerData = new Jaxrs2HandlerData(target.getClass(), method);
@@ -115,7 +121,9 @@ public AdviceScope enter(Object[] args, Object target, Method method) {
115121
handlerData);
116122

117123
if (!instrumenter().shouldStart(parentContext, handlerData)) {
118-
return this;
124+
context = null;
125+
scope = null;
126+
return;
119127
}
120128

121129
context = instrumenter().start(parentContext, handlerData);
@@ -124,7 +132,6 @@ public AdviceScope enter(Object[] args, Object target, Method method) {
124132
if (ASYNC_RESPONSE_DATA != null && asyncResponse != null) {
125133
ASYNC_RESPONSE_DATA.set(asyncResponse, AsyncResponseData.create(context, handlerData));
126134
}
127-
return this;
128135
}
129136

130137
@Nullable
@@ -167,8 +174,7 @@ public static AdviceScope nameSpan(
167174
@Advice.This Object target,
168175
@Advice.Origin Method method,
169176
@Advice.AllArguments Object[] args) {
170-
AdviceScope adviceScope = new AdviceScope(CallDepth.forClass(Path.class));
171-
return adviceScope.enter(args, target, method);
177+
return new AdviceScope(CallDepth.forClass(Path.class), args, target, method);
172178
}
173179

174180
@AssignReturned.ToReturned

instrumentation/jaxrs/jaxrs-3.0/jaxrs-3.0-annotations/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/jaxrs/v3_0/JaxrsAnnotationsInstrumentation.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -74,36 +74,40 @@ public static class JaxRsAnnotationsAdvice {
7474

7575
public static class AdviceScope {
7676
public Jaxrs3HandlerData handlerData;
77-
public AsyncResponse asyncResponse;
78-
public CallDepth callDepth;
79-
public Context context;
80-
public Scope scope;
77+
public final AsyncResponse asyncResponse;
78+
public final CallDepth callDepth;
79+
public final Context context;
80+
public final Scope scope;
8181

82-
public AdviceScope(CallDepth callDepth) {
82+
public AdviceScope(CallDepth callDepth, Class<?> type, Method method, Object[] args) {
8383
this.callDepth = callDepth;
84-
}
85-
86-
@CanIgnoreReturnValue
87-
public AdviceScope enter(Class<?> type, Method method, Object[] args) {
8884
if (callDepth.getAndIncrement() > 0) {
89-
return this;
85+
asyncResponse = null;
86+
context = null;
87+
scope = null;
88+
return;
9089
}
9190

91+
AsyncResponse asyncResponseArg = null;
9292
for (Object arg : args) {
9393
if (arg instanceof AsyncResponse) {
94-
asyncResponse = (AsyncResponse) arg;
95-
if (RESPONSE_DATA.get(asyncResponse) != null) {
94+
asyncResponseArg = (AsyncResponse) arg;
95+
if (RESPONSE_DATA.get(asyncResponseArg) != null) {
9696
/*
9797
* We are probably in a recursive call and don't want to start a new span because it
9898
* would replace the existing span in the asyncResponse and cause it to never finish. We
9999
* could work around this by using a list instead, but we likely don't want the extra
100100
* span anyway.
101101
*/
102-
return this;
102+
asyncResponse = null;
103+
context = null;
104+
scope = null;
105+
return;
103106
}
104107
break;
105108
}
106109
}
110+
asyncResponse = asyncResponseArg;
107111

108112
Context parentContext = Java8BytecodeBridge.currentContext();
109113
handlerData = new Jaxrs3HandlerData(type, method);
@@ -115,7 +119,9 @@ public AdviceScope enter(Class<?> type, Method method, Object[] args) {
115119
handlerData);
116120

117121
if (!instrumenter().shouldStart(parentContext, handlerData)) {
118-
return this;
122+
context = null;
123+
scope = null;
124+
return;
119125
}
120126

121127
context = instrumenter().start(parentContext, handlerData);
@@ -124,7 +130,6 @@ public AdviceScope enter(Class<?> type, Method method, Object[] args) {
124130
if (asyncResponse != null) {
125131
RESPONSE_DATA.set(asyncResponse, AsyncResponseData.create(context, handlerData));
126132
}
127-
return this;
128133
}
129134

130135
@Nullable
@@ -164,8 +169,7 @@ public static AdviceScope nameSpan(
164169
@Advice.This Object target,
165170
@Advice.Origin Method method,
166171
@Advice.AllArguments Object[] args) {
167-
AdviceScope adviceScope = new AdviceScope(CallDepth.forClass(Path.class));
168-
return adviceScope.enter(target.getClass(), method, args);
172+
return new AdviceScope(CallDepth.forClass(Path.class), target.getClass(), method, args);
169173
}
170174

171175
@AssignReturned.ToReturned

0 commit comments

Comments
 (0)