Skip to content

Commit

Permalink
Move capturing enduser.id attribute behind a flag (#9751)
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit authored Oct 31, 2023
1 parent ec089f5 commit 05ae636
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion instrumentation/servlet/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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). |

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,11 +44,13 @@ public void onEnd(
ServletRequestContext<REQUEST> requestContext,
@Nullable ServletResponseContext<RESPONSE> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -110,4 +113,8 @@ public boolean shouldEmitExperimentalHttpClientMetrics() {
public boolean shouldEmitExperimentalHttpServerMetrics() {
return emitExperimentalHttpServerMetrics;
}

public boolean shouldCaptureEnduser() {
return captureEnduser;
}
}

0 comments on commit 05ae636

Please sign in to comment.