Skip to content

Commit f5d1d0d

Browse files
authored
Code review sweep (run 24918537506) (open-telemetry#18281)
1 parent 51c0581 commit f5d1d0d

10 files changed

Lines changed: 95 additions & 106 deletions

File tree

instrumentation/tomcat/tomcat-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/v7_0/Tomcat7AttachResponseAdvice.java

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

instrumentation/tomcat/tomcat-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/v7_0/Tomcat7InstrumentationModule.java

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,23 @@
66
package io.opentelemetry.javaagent.instrumentation.tomcat.v7_0;
77

88
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
9+
import static io.opentelemetry.javaagent.instrumentation.tomcat.v7_0.Tomcat7Singletons.helper;
910
import static java.util.Collections.singletonList;
1011

1112
import com.google.auto.service.AutoService;
13+
import io.opentelemetry.context.Context;
14+
import io.opentelemetry.context.Scope;
15+
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
16+
import io.opentelemetry.javaagent.bootstrap.http.HttpServerResponseCustomizerHolder;
1217
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
1318
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
1419
import io.opentelemetry.javaagent.instrumentation.tomcat.common.TomcatServerHandlerInstrumentation;
1520
import java.util.List;
21+
import javax.annotation.Nullable;
22+
import net.bytebuddy.asm.Advice;
1623
import net.bytebuddy.matcher.ElementMatcher;
24+
import org.apache.coyote.Request;
25+
import org.apache.coyote.Response;
1726

1827
@AutoService(InstrumentationModule.class)
1928
public class Tomcat7InstrumentationModule extends InstrumentationModule {
@@ -24,19 +33,84 @@ public Tomcat7InstrumentationModule() {
2433

2534
@Override
2635
public ElementMatcher.Junction<ClassLoader> classLoaderMatcher() {
27-
// does not match tomcat 10.0+
2836
return hasClassesNamed(
29-
"javax.servlet.http.HttpServletRequest", "org.apache.catalina.loader.Constants");
37+
// removed in Servlet 5.0 (renamed to jakarta.servlet)
38+
"javax.servlet.http.HttpServletRequest",
39+
// removed in 10.0
40+
"org.apache.catalina.loader.Constants");
3041
}
3142

3243
@Override
3344
public List<TypeInstrumentation> typeInstrumentations() {
34-
// Tomcat 10+ is excluded by making sure Request does not have any methods returning
35-
// jakarta.servlet.ReadListener which is returned by getReadListener method on Tomcat 10+
36-
String packageName = Tomcat7InstrumentationModule.class.getPackage().getName();
3745
return singletonList(
3846
new TomcatServerHandlerInstrumentation(
39-
packageName + ".Tomcat7ServerHandlerAdvice",
40-
packageName + ".Tomcat7AttachResponseAdvice"));
47+
getClass().getName() + "$Tomcat7ServerHandlerAdvice",
48+
getClass().getName() + "$Tomcat7AttachResponseAdvice"));
49+
}
50+
51+
@SuppressWarnings("unused")
52+
public static class Tomcat7AttachResponseAdvice {
53+
54+
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class, inline = false)
55+
public static void attachResponse(
56+
@Advice.Argument(2) Response response, @Advice.Return boolean success) {
57+
58+
if (success) {
59+
helper().attachResponseToRequest(Java8BytecodeBridge.currentContext(), response);
60+
}
61+
}
62+
}
63+
64+
@SuppressWarnings("unused")
65+
public static class Tomcat7ServerHandlerAdvice {
66+
67+
public static class AdviceScope {
68+
private final Context context;
69+
private final Scope scope;
70+
71+
private AdviceScope(Context context, Scope scope) {
72+
this.context = context;
73+
this.scope = scope;
74+
}
75+
76+
@Nullable
77+
public static AdviceScope start(Request request, Response response) {
78+
Context parentContext = Context.current();
79+
if (!helper().shouldStart(parentContext, request)) {
80+
return null;
81+
}
82+
83+
Context context = helper().start(parentContext, request);
84+
85+
Scope scope = context.makeCurrent();
86+
87+
HttpServerResponseCustomizerHolder.getCustomizer()
88+
.customize(context, response, Tomcat7ResponseMutator.INSTANCE);
89+
90+
return new AdviceScope(context, scope);
91+
}
92+
93+
public void end(Request request, Response response, Throwable throwable) {
94+
helper().end(request, response, throwable, context, scope);
95+
}
96+
}
97+
98+
@Nullable
99+
@Advice.OnMethodEnter(suppress = Throwable.class, inline = false)
100+
public static AdviceScope onEnter(
101+
@Advice.Argument(0) Request request, @Advice.Argument(1) Response response) {
102+
return AdviceScope.start(request, response);
103+
}
104+
105+
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class, inline = false)
106+
public static void stopSpan(
107+
@Advice.Argument(0) Request request,
108+
@Advice.Argument(1) Response response,
109+
@Advice.Thrown @Nullable Throwable throwable,
110+
@Advice.Enter @Nullable AdviceScope adviceScope) {
111+
if (adviceScope != null) {
112+
adviceScope.end(request, response, throwable);
113+
}
114+
}
41115
}
42116
}

instrumentation/tomcat/tomcat-7.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/tomcat/v7_0/Tomcat7ServerHandlerAdvice.java

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

instrumentation/tomcat/tomcat-7.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/v7_0/ErrorHandlerValve.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ protected void report(Request request, Response response, Throwable t) {
1818
return;
1919
}
2020
try {
21-
response.getWriter().print(t != null ? t.getCause().getMessage() : response.getMessage());
21+
Throwable cause = t != null ? t.getCause() : null;
22+
response.getWriter().print(cause != null ? cause.getMessage() : response.getMessage());
2223
} catch (IOException ignored) {
2324
// Ignore exception when writing exception message to response fails on IO - same as is done
2425
// by the superclass itself and by other built-in ErrorReportValve implementations.

instrumentation/twilio-6.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/twilio/v6_6/TwilioAsyncInstrumentation.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ public SpanFinishingCallback(Context context, String spanName) {
147147
}
148148

149149
@Override
150-
public void onSuccess(Object result) {
150+
public void onSuccess(T result) {
151151
instrumenter().end(context, spanName, result, null);
152152
}
153153

instrumentation/twilio-6.6/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/twilio/v6_6/TwilioExperimentalAttributesExtractor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ private static void setTagIfPresent(
9292
attributes.put(tag, value.toString());
9393
}
9494

95-
} catch (Exception e) {
95+
} catch (Exception ignored) {
9696
// Expected that this won't work for all result types
9797
}
9898
}

instrumentation/twilio-6.6/metadata.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ description: >
55
library_link: https://github.com/twilio/twilio-java
66
configurations:
77
- name: otel.instrumentation.twilio.experimental-span-attributes
8+
declarative_name: java.twilio.experimental_span_attributes/development
89
description: >
910
Enables experimental span attributes `twilio.type`, `twilio.account`, `twilio.sid`, and
1011
`twilio.status`.

instrumentation/undertow-1.4/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/undertow/v1_4/UndertowHttpAttributesGetter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
import java.util.List;
1616
import javax.annotation.Nullable;
1717

18-
public class UndertowHttpAttributesGetter
18+
class UndertowHttpAttributesGetter
1919
implements HttpServerAttributesGetter<HttpServerExchange, HttpServerExchange> {
2020

2121
@Override

instrumentation/undertow-1.4/metadata.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,24 @@ semantic_conventions:
66
- HTTP_SERVER_METRICS
77
configurations:
88
- name: otel.instrumentation.http.known-methods
9+
declarative_name: java.common.http.known_methods
910
description: >
1011
Configures the instrumentation to recognize an alternative set of HTTP request methods. All
1112
other methods will be treated as `_OTHER`.
1213
type: list
1314
default: "CONNECT,DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT,TRACE"
1415
- name: otel.instrumentation.http.server.capture-request-headers
16+
declarative_name: general.http.server.request_captured_headers
1517
description: List of HTTP request headers to capture in HTTP server telemetry.
1618
type: list
1719
default: ""
1820
- name: otel.instrumentation.http.server.capture-response-headers
21+
declarative_name: general.http.server.response_captured_headers
1922
description: List of HTTP response headers to capture in HTTP server telemetry.
2023
type: list
2124
default: ""
2225
- name: otel.instrumentation.http.server.emit-experimental-telemetry
26+
declarative_name: java.common.http.server.emit_experimental_telemetry/development
2327
description: >
2428
Enable the capture of experimental HTTP server telemetry. Adds the `http.request.body.size`
2529
and `http.response.body.size` attributes to spans, and records `http.server.request.body.size`

instrumentation/vaadin-14.2/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/vaadin/v14_2/VaadinServiceContext.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55

66
package io.opentelemetry.javaagent.instrumentation.vaadin.v14_2;
77

8+
import javax.annotation.Nullable;
9+
810
public class VaadinServiceContext {
911
private boolean requestHandled;
10-
private String spanNameCandidate;
12+
@Nullable private String spanNameCandidate;
1113

1214
void setRequestHandled() {
1315
requestHandled = true;
@@ -21,6 +23,7 @@ void setSpanNameCandidate(String spanNameCandidate) {
2123
this.spanNameCandidate = spanNameCandidate;
2224
}
2325

26+
@Nullable
2427
String getSpanNameCandidate() {
2528
return spanNameCandidate;
2629
}

0 commit comments

Comments
 (0)