diff --git a/instrumentation/servlet/README.md b/instrumentation/servlet/README.md index cfb87a1658f1..511eb4b5cea5 100644 --- a/instrumentation/servlet/README.md +++ b/instrumentation/servlet/README.md @@ -3,7 +3,7 @@ ## Settings | System property | Type | Default | Description | -| ---------------------------------------------------------------------- | ------- | ------- | --------------------------------------------------- | +|------------------------------------------------------------------------| ------- | ------- |-----------------------------------------------------| | `otel.instrumentation.servlet.experimental-span-attributes` | Boolean | `false` | Enable the capture of experimental span attributes. | | `otel.instrumentation.servlet.experimental.capture-request-parameters` | List | Empty | Request parameters to be captured (experimental). | diff --git a/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/BaseServletHelper.java b/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/BaseServletHelper.java index e032094c6313..401518e7210d 100644 --- a/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/BaseServletHelper.java +++ b/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/BaseServletHelper.java @@ -14,6 +14,7 @@ import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; import io.opentelemetry.instrumentation.api.instrumenter.LocalRootSpan; import io.opentelemetry.instrumentation.api.instrumenter.http.HttpServerRoute; +import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig; import io.opentelemetry.javaagent.bootstrap.servlet.AppServerBridge; import io.opentelemetry.javaagent.bootstrap.servlet.MappingResolver; import io.opentelemetry.javaagent.bootstrap.servlet.ServletContextPath; @@ -135,6 +136,10 @@ private void captureRequestParameters(Span serverSpan, REQUEST request) { * created by servlet instrumentation we call this method on exit from the last servlet or filter. */ private void captureEnduserId(Span serverSpan, REQUEST request) { + if (!CommonConfig.get().shouldCaptureEnduser()) { + return; + } + Principal principal = accessor.getRequestUserPrincipal(request); if (principal != null) { String name = principal.getName(); diff --git a/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletAdditionalAttributesExtractor.java b/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletAdditionalAttributesExtractor.java index 8422d8af8775..8b68d8b3bb75 100644 --- a/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletAdditionalAttributesExtractor.java +++ b/instrumentation/servlet/servlet-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/ServletAdditionalAttributesExtractor.java @@ -11,6 +11,7 @@ import io.opentelemetry.api.common.AttributesBuilder; import io.opentelemetry.context.Context; import io.opentelemetry.instrumentation.api.instrumenter.AttributesExtractor; +import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig; import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig; import io.opentelemetry.semconv.SemanticAttributes; import java.security.Principal; @@ -43,11 +44,13 @@ public void onEnd( ServletRequestContext requestContext, @Nullable ServletResponseContext responseContext, @Nullable Throwable error) { - Principal principal = accessor.getRequestUserPrincipal(requestContext.request()); - if (principal != null) { - String name = principal.getName(); - if (name != null) { - attributes.put(SemanticAttributes.ENDUSER_ID, name); + if (CommonConfig.get().shouldCaptureEnduser()) { + Principal principal = accessor.getRequestUserPrincipal(requestContext.request()); + if (principal != null) { + String name = principal.getName(); + if (name != null) { + attributes.put(SemanticAttributes.ENDUSER_ID, name); + } } } if (!CAPTURE_EXPERIMENTAL_SPAN_ATTRIBUTES) { diff --git a/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java b/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java index 930942fe73db..5d4451086286 100644 --- a/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java +++ b/javaagent-extension-api/src/main/java/io/opentelemetry/javaagent/bootstrap/internal/CommonConfig.java @@ -35,6 +35,7 @@ public static CommonConfig get() { private final boolean statementSanitizationEnabled; private final boolean emitExperimentalHttpClientMetrics; private final boolean emitExperimentalHttpServerMetrics; + private final boolean captureEnduser; CommonConfig(InstrumentationConfig config) { peerServiceResolver = @@ -73,6 +74,8 @@ public static CommonConfig get() { config.getBoolean("otel.instrumentation.http.client.emit-experimental-metrics", false); emitExperimentalHttpServerMetrics = config.getBoolean("otel.instrumentation.http.server.emit-experimental-metrics", false); + captureEnduser = + config.getBoolean("otel.instrumentation.common.capture-enduser.enabled", false); } public PeerServiceResolver getPeerServiceResolver() { @@ -110,4 +113,8 @@ public boolean shouldEmitExperimentalHttpClientMetrics() { public boolean shouldEmitExperimentalHttpServerMetrics() { return emitExperimentalHttpServerMetrics; } + + public boolean shouldCaptureEnduser() { + return captureEnduser; + } }