Skip to content

Commit e7d1a77

Browse files
committed
fix nits
1 parent e89c230 commit e7d1a77

File tree

6 files changed

+110
-97
lines changed

6 files changed

+110
-97
lines changed

dd-java-agent/instrumentation/jetty/jetty-server/jetty-server-10.0/src/main/java11/datadog/trace/instrumentation/jetty10/ResetAdvice.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,15 @@ public class ResetAdvice {
1919
public static void stopSpan(@Advice.This final HttpChannel channel) {
2020
Request req = channel.getRequest();
2121
Object contextObj = req.getAttribute(DD_CONTEXT_ATTRIBUTE);
22-
if (contextObj instanceof Context) {
23-
final Context context = (Context) contextObj;
24-
final AgentSpan span = spanFromContext(context);
25-
if (span != null) {
26-
JettyDecorator.OnResponse.onResponse(span, channel);
27-
DECORATE.beforeFinish(context);
28-
span.finish();
29-
} else {
30-
DECORATE.beforeFinish(context);
31-
}
22+
if (!(contextObj instanceof Context)) {
23+
return;
3224
}
25+
final Context context = (Context) contextObj;
26+
final AgentSpan span = spanFromContext(context);
27+
if (span != null) {
28+
JettyDecorator.OnResponse.onResponse(span, channel);
29+
span.finish();
30+
}
31+
DECORATE.beforeFinish(context);
3332
}
3433
}

dd-java-agent/instrumentation/jetty/jetty-server/jetty-server-11.0/src/main/java11/datadog/trace/instrumentation/jetty11/SetContextPathAdvice.java

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,24 @@ public static void updateContextPath(
2626
@Advice.Argument(1) final String pathInContext) {
2727
Object contextObj = req.getAttribute(DD_CONTEXT_ATTRIBUTE);
2828
// Don't want to update while being dispatched to new servlet
29-
if (contextObj instanceof Context && req.getAttribute(DD_DISPATCH_SPAN_ATTRIBUTE) == null) {
30-
Context ctx = (Context) contextObj;
31-
AgentSpan span = spanFromContext(ctx);
32-
if (span != null) {
33-
if (context != null && context.getContextPath() != null) {
34-
final String servletContext = context.getContextPath();
35-
span.setTag(SERVLET_CONTEXT, servletContext);
36-
req.setAttribute(DD_CONTEXT_PATH_ATTRIBUTE, servletContext);
37-
if (pathInContext != null) {
38-
final String relativePath =
39-
pathInContext.startsWith(servletContext)
40-
? pathInContext.substring(servletContext.length())
41-
: pathInContext;
42-
span.setTag(SERVLET_PATH, relativePath);
43-
req.setAttribute(DD_SERVLET_PATH_ATTRIBUTE, relativePath);
44-
}
45-
}
46-
}
29+
if (!(contextObj instanceof Context) || req.getAttribute(DD_DISPATCH_SPAN_ATTRIBUTE) != null) {
30+
return;
31+
}
32+
Context ctx = (Context) contextObj;
33+
AgentSpan span = spanFromContext(ctx);
34+
if (span == null || context == null || context.getContextPath() == null) {
35+
return;
36+
}
37+
final String servletContext = context.getContextPath();
38+
span.setTag(SERVLET_CONTEXT, servletContext);
39+
req.setAttribute(DD_CONTEXT_PATH_ATTRIBUTE, servletContext);
40+
if (pathInContext != null) {
41+
final String relativePath =
42+
pathInContext.startsWith(servletContext)
43+
? pathInContext.substring(servletContext.length())
44+
: pathInContext;
45+
span.setTag(SERVLET_PATH, relativePath);
46+
req.setAttribute(DD_SERVLET_PATH_ATTRIBUTE, relativePath);
4747
}
4848
}
4949
}

dd-java-agent/instrumentation/jetty/jetty-server/jetty-server-12.0/src/main/java17/datadog/trace/instrumentation/jetty12/SetContextPathAdvice.java

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,37 @@ public static void updateContextPath(
2525
@Advice.This final ContextHandler contextHandler, @Advice.Argument(0) final Request req) {
2626
Object contextObj = req.getAttribute(DD_CONTEXT_ATTRIBUTE);
2727
// Don't want to update while being dispatched to new servlet
28-
if (contextObj instanceof Context && req.getAttribute(DD_DISPATCH_SPAN_ATTRIBUTE) == null) {
29-
Context context = (Context) contextObj;
30-
AgentSpan span = spanFromContext(context);
31-
if (span != null) {
32-
if (contextHandler != null && contextHandler.getContextPath() != null) {
33-
final String servletContext = contextHandler.getContextPath();
34-
final String pathInContext;
35-
final HttpURI uri = req.getHttpURI();
36-
if (contextHandler.getContext() != null && uri != null && uri.getDecodedPath() != null) {
37-
pathInContext = contextHandler.getContext().getPathInContext(uri.getDecodedPath());
38-
} else {
39-
pathInContext = null;
40-
}
41-
span.setTag(SERVLET_CONTEXT, servletContext);
42-
req.setAttribute(DD_CONTEXT_PATH_ATTRIBUTE, servletContext);
43-
if (pathInContext != null) {
44-
// the following can be cached however than can be issues for application having
45-
// dynamically generated URL
46-
// since a bounded cache might collide
47-
String relativePath =
48-
pathInContext.startsWith(servletContext)
49-
? pathInContext.substring(servletContext.length())
50-
: pathInContext;
51-
if (relativePath.isEmpty() || relativePath.charAt(0) != '/') {
52-
relativePath = "/" + relativePath;
53-
}
54-
span.setTag(SERVLET_PATH, relativePath);
55-
req.setAttribute(DD_SERVLET_PATH_ATTRIBUTE, relativePath);
56-
}
57-
}
28+
if (!(contextObj instanceof Context) || req.getAttribute(DD_DISPATCH_SPAN_ATTRIBUTE) != null) {
29+
return;
30+
}
31+
Context context = (Context) contextObj;
32+
AgentSpan span = spanFromContext(context);
33+
if (span == null || contextHandler == null || contextHandler.getContextPath() == null) {
34+
return;
35+
}
36+
final String servletContext = contextHandler.getContextPath();
37+
final String pathInContext;
38+
final HttpURI uri = req.getHttpURI();
39+
if (contextHandler.getContext() != null && uri != null && uri.getDecodedPath() != null) {
40+
pathInContext = contextHandler.getContext().getPathInContext(uri.getDecodedPath());
41+
} else {
42+
pathInContext = null;
43+
}
44+
span.setTag(SERVLET_CONTEXT, servletContext);
45+
req.setAttribute(DD_CONTEXT_PATH_ATTRIBUTE, servletContext);
46+
if (pathInContext != null) {
47+
// the following can be cached however than can be issues for application having
48+
// dynamically generated URL
49+
// since a bounded cache might collide
50+
String relativePath =
51+
pathInContext.startsWith(servletContext)
52+
? pathInContext.substring(servletContext.length())
53+
: pathInContext;
54+
if (relativePath.isEmpty() || relativePath.charAt(0) != '/') {
55+
relativePath = "/" + relativePath;
5856
}
57+
span.setTag(SERVLET_PATH, relativePath);
58+
req.setAttribute(DD_SERVLET_PATH_ATTRIBUTE, relativePath);
5959
}
6060
}
6161
}

dd-java-agent/instrumentation/jetty/jetty-server/jetty-server-7.0/src/main/java/datadog/trace/instrumentation/jetty70/JettyCommitResponseInstrumentation.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,11 @@ static class CommitResponseAdvice {
7878
}
7979
Context context = (Context) contextObj;
8080
AgentSpan span = spanFromContext(context);
81-
RequestContext requestContext;
82-
if (span == null || (requestContext = span.getRequestContext()) == null) {
81+
if (span == null) {
82+
return false;
83+
}
84+
RequestContext requestContext = span.getRequestContext();
85+
if (requestContext == null) {
8386
return false;
8487
}
8588

dd-java-agent/instrumentation/jetty/jetty-server/jetty-server-9.0.4/src/main/java/datadog/trace/instrumentation/jetty904/JettyCommitResponseHelper.java

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,23 @@ public class JettyCommitResponseHelper {
5050

5151
Request req = connection.getRequest();
5252

53-
Object contextObj;
54-
Context context;
55-
AgentSpan span;
56-
RequestContext requestContext;
57-
if (req.getAttribute(DD_IGNORE_COMMIT_ATTRIBUTE) != null
58-
|| !((contextObj = req.getAttribute(DD_CONTEXT_ATTRIBUTE)) instanceof Context)
59-
|| (span = spanFromContext(context = (Context) contextObj)) == null
60-
|| (requestContext = span.getRequestContext()) == null) {
53+
if (req.getAttribute(DD_IGNORE_COMMIT_ATTRIBUTE) != null) {
54+
_committed.set(false);
55+
return false;
56+
}
57+
Object contextObj = req.getAttribute(DD_CONTEXT_ATTRIBUTE);
58+
if (!(contextObj instanceof Context)) {
59+
_committed.set(false);
60+
return false;
61+
}
62+
Context context = (Context) contextObj;
63+
AgentSpan span = spanFromContext(context);
64+
if (span == null) {
65+
_committed.set(false);
66+
return false;
67+
}
68+
RequestContext requestContext = span.getRequestContext();
69+
if (requestContext == null) {
6170
_committed.set(false);
6271
return false;
6372
}

dd-java-agent/instrumentation/servlet/javax-servlet/javax-servlet-3.0/src/main/java/datadog/trace/instrumentation/servlet3/AsyncContextInstrumentation.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -87,36 +87,38 @@ public static boolean enter(
8787

8888
final ServletRequest request = context.getRequest();
8989
final Object contextAttr = request.getAttribute(DD_CONTEXT_ATTRIBUTE);
90-
if (contextAttr instanceof Context) {
91-
final Context ctxt = (Context) contextAttr;
92-
final AgentSpan parent = spanFromContext(ctxt);
93-
if (parent != null) {
94-
95-
final AgentSpan span = startSpan(SERVLET_DISPATCH, parent.context());
96-
// This span should get finished by Servlet3Advice
97-
// However, when using Jetty without servlets (directly org.eclipse.jetty.server.Handler),
98-
// that's not the case (see jetty's HandleAdvice)
99-
DECORATE.afterStart(span);
100-
101-
// These are pulled from attributes because jetty clears them from the request too early.
102-
span.setTag(SERVLET_CONTEXT, request.getAttribute(DD_CONTEXT_PATH_ATTRIBUTE));
103-
span.setTag(SERVLET_PATH, request.getAttribute(DD_SERVLET_PATH_ATTRIBUTE));
104-
105-
synchronized (request) {
106-
request.setAttribute(DD_DISPATCH_SPAN_ATTRIBUTE, span);
107-
}
108-
109-
if (args.length == 1 && args[0] instanceof String) {
110-
span.setResourceName((String) args[0]);
111-
} else if (args.length == 2 && args[1] instanceof String) {
112-
span.setResourceName((String) args[1]);
113-
}
114-
115-
// We can't register FinishAsyncDispatchListener here.
116-
// The dispatch may happen on an onTimeout/onError, and adding listeners
117-
// when listeners are being iterated on causes a ConcurrentModificationException on jetty
118-
}
90+
if (!(contextAttr instanceof Context)) {
91+
return true;
11992
}
93+
final Context ctxt = (Context) contextAttr;
94+
final AgentSpan parent = spanFromContext(ctxt);
95+
if (parent == null) {
96+
return true;
97+
}
98+
99+
final AgentSpan span = startSpan(SERVLET_DISPATCH, parent.context());
100+
// This span should get finished by Servlet3Advice
101+
// However, when using Jetty without servlets (directly org.eclipse.jetty.server.Handler),
102+
// that's not the case (see jetty's HandleAdvice)
103+
DECORATE.afterStart(span);
104+
105+
// These are pulled from attributes because jetty clears them from the request too early.
106+
span.setTag(SERVLET_CONTEXT, request.getAttribute(DD_CONTEXT_PATH_ATTRIBUTE));
107+
span.setTag(SERVLET_PATH, request.getAttribute(DD_SERVLET_PATH_ATTRIBUTE));
108+
109+
synchronized (request) {
110+
request.setAttribute(DD_DISPATCH_SPAN_ATTRIBUTE, span);
111+
}
112+
113+
if (args.length == 1 && args[0] instanceof String) {
114+
span.setResourceName((String) args[0]);
115+
} else if (args.length == 2 && args[1] instanceof String) {
116+
span.setResourceName((String) args[1]);
117+
}
118+
119+
// We can't register FinishAsyncDispatchListener here.
120+
// The dispatch may happen on an onTimeout/onError, and adding listeners
121+
// when listeners are being iterated on causes a ConcurrentModificationException on jetty
120122
return true;
121123
}
122124

0 commit comments

Comments
 (0)