Skip to content

Commit 0005119

Browse files
authored
Always add trace_id and span_id attributes to servlet requests (#15485)
1 parent ffe5bb2 commit 0005119

File tree

4 files changed

+40
-12
lines changed
  • instrumentation
    • servlet
    • tomcat
      • tomcat-10.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/v10_0
      • tomcat-7.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/v7_0

4 files changed

+40
-12
lines changed

instrumentation/servlet/README.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
## Settings
44

5-
| System property | Type | Default | Description |
6-
|------------------------------------------------------------------------| ------- | ------- |-----------------------------------------------------|
7-
| `otel.instrumentation.servlet.experimental-span-attributes` | Boolean | `false` | Enable the capture of experimental span attributes. |
8-
| `otel.instrumentation.servlet.experimental.capture-request-parameters` | List | Empty | Request parameters to be captured (experimental). |
5+
| System property | Type | Default | Description |
6+
|----------------------------------------------------------------------------|---------|---------|-----------------------------------------------------|
7+
| `otel.instrumentation.servlet.experimental-span-attributes` | Boolean | `false` | Enable the capture of experimental span attributes. |
8+
| `otel.instrumentation.servlet.experimental.capture-request-parameters` | List | Empty | Request parameters to be captured (experimental). |
9+
| `otel.instrumentation.servlet.experimental.add-trace-id-request-attribute` | Boolean | `true` | Add `trace_id` and `span_id` as request attributes. |
910

1011
### A word about version
1112

instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/BaseServletHelper.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import io.opentelemetry.instrumentation.api.instrumenter.LocalRootSpan;
1616
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute;
1717
import io.opentelemetry.javaagent.bootstrap.internal.AgentCommonConfig;
18+
import io.opentelemetry.javaagent.bootstrap.internal.AgentInstrumentationConfig;
1819
import io.opentelemetry.javaagent.bootstrap.servlet.AppServerBridge;
1920
import io.opentelemetry.javaagent.bootstrap.servlet.MappingResolver;
2021
import io.opentelemetry.javaagent.bootstrap.servlet.ServletAsyncContext;
@@ -23,8 +24,12 @@
2324
import java.security.Principal;
2425
import java.util.function.Function;
2526

26-
@SuppressWarnings("deprecation") // using deprecated semconv
2727
public abstract class BaseServletHelper<REQUEST, RESPONSE> {
28+
private static final boolean ADD_TRACE_ID_REQUEST_ATTRIBUTE =
29+
AgentInstrumentationConfig.get()
30+
.getBoolean(
31+
"otel.instrumentation.servlet.experimental.add-trace-id-request-attribute", true);
32+
2833
protected final Instrumenter<ServletRequestContext<REQUEST>, ServletResponseContext<RESPONSE>>
2934
instrumenter;
3035
protected final ServletAccessor<REQUEST, RESPONSE> accessor;
@@ -53,13 +58,7 @@ public Context start(Context parentContext, ServletRequestContext<REQUEST> reque
5358
Context context = instrumenter.start(parentContext, requestContext);
5459

5560
REQUEST request = requestContext.request();
56-
SpanContext spanContext = Span.fromContext(context).getSpanContext();
57-
// we do this e.g. so that servlet containers can use these values in their access logs
58-
// TODO: These are only available when using servlet instrumentation or when server
59-
// instrumentation extends servlet instrumentation e.g. jetty. Either remove or make sure they
60-
// also work on tomcat and wildfly.
61-
accessor.setRequestAttribute(request, "trace_id", spanContext.getTraceId());
62-
accessor.setRequestAttribute(request, "span_id", spanContext.getSpanId());
61+
addRequestAttributes(request, context);
6362

6463
context = addServletContextPath(context, request);
6564
context = addAsyncContext(context);
@@ -69,6 +68,16 @@ public Context start(Context parentContext, ServletRequestContext<REQUEST> reque
6968
return context;
7069
}
7170

71+
private void addRequestAttributes(REQUEST request, Context context) {
72+
if (!ADD_TRACE_ID_REQUEST_ATTRIBUTE) {
73+
return;
74+
}
75+
76+
SpanContext spanContext = Span.fromContext(context).getSpanContext();
77+
accessor.setRequestAttribute(request, "trace_id", spanContext.getTraceId());
78+
accessor.setRequestAttribute(request, "span_id", spanContext.getSpanId());
79+
}
80+
7281
protected Context addServletContextPath(Context context, REQUEST request) {
7382
return ServletContextPath.init(context, contextPathExtractor, request);
7483
}
@@ -100,6 +109,8 @@ public Context updateContext(
100109
result, servlet ? SERVER : SERVER_FILTER, spanNameProvider, mappingResolver, request);
101110
}
102111

112+
addRequestAttributes(request, context);
113+
103114
return result;
104115
}
105116

instrumentation/tomcat/tomcat-10.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/tomcat/v10_0/TestServlet.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ class TestServlet extends HttpServlet {
1818
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
1919
String path = req.getServletPath();
2020

21+
// these are set by servlet instrumentation
22+
if (req.getAttribute("trace_id") == null) {
23+
throw new IllegalStateException("trace_id attribute not found");
24+
}
25+
if (req.getAttribute("span_id") == null) {
26+
throw new IllegalStateException("span_id attribute not found");
27+
}
28+
2129
ServerEndpoint serverEndpoint = ServerEndpoint.forPath(path);
2230
if (serverEndpoint != null) {
2331
AbstractHttpServerTest.controller(

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ class TestServlet extends HttpServlet {
1818
protected void service(HttpServletRequest req, HttpServletResponse resp) throws IOException {
1919
String path = req.getServletPath();
2020

21+
// these are set by servlet instrumentation
22+
if (req.getAttribute("trace_id") == null) {
23+
throw new IllegalStateException("trace_id attribute not found");
24+
}
25+
if (req.getAttribute("span_id") == null) {
26+
throw new IllegalStateException("span_id attribute not found");
27+
}
28+
2129
ServerEndpoint serverEndpoint = ServerEndpoint.forPath(path);
2230
if (serverEndpoint != null) {
2331
AbstractHttpServerTest.controller(

0 commit comments

Comments
 (0)