Simplify setting of user.principal attribute
MODIFY
instrumentation/servlet/request-2.3/src/main/java/io/opentelemetry/auto/instrumentation/servlet/v2_3/Servlet2Advice.java
instrumentation/servlet/request-2.3/src/main/java/io/opentelemetry/auto/instrumentation/servlet/v2_3/Servlet2Advice.java
@@ -68,7 +68,7 @@ public class Servlet2Advice {
}
scope.close();
- TRACER.setPrincipal((HttpServletRequest) request);
+ TRACER.setPrincipal(span, (HttpServletRequest) request);
Integer responseStatus =
InstrumentationContext.get(ServletResponse.class, Integer.class).get(response);

MODIFY
instrumentation/servlet/request-3.0/src/main/java/io/opentelemetry/auto/instrumentation/servlet/v3_0/Servlet3Advice.java
instrumentation/servlet/request-3.0/src/main/java/io/opentelemetry/auto/instrumentation/servlet/v3_0/Servlet3Advice.java
@@ -80,7 +80,7 @@ public class Servlet3Advice {
return;
}
- TRACER.setPrincipal((HttpServletRequest) request);
+ TRACER.setPrincipal(span, (HttpServletRequest) request);
if (throwable != null) {
TRACER.endExceptionally(span, throwable, ((HttpServletResponse) response).getStatus());

MODIFY
instrumentation/servlet/servlet-common/src/main/java/io/opentelemetry/auto/instrumentation/servlet/ServletHttpServerTracer.java
instrumentation/servlet/servlet-common/src/main/java/io/opentelemetry/auto/instrumentation/servlet/ServletHttpServerTracer.java
@@ -100,13 +100,10 @@ public abstract class ServletHttpServerTracer extends HttpServerTracer<HttpServl
return super.unwrapThrowable(result);
}
- public void setPrincipal(HttpServletRequest request) {
- final Span existingSpan = getAttachedSpan(request);
- if (existingSpan != null) {
+ public void setPrincipal(Span span, HttpServletRequest request) {
final Principal principal = request.getUserPrincipal();
if (principal != null) {
- existingSpan.setAttribute(MoreTags.USER_NAME, principal.getName());
- }
+ span.setAttribute(MoreTags.USER_NAME, principal.getName());
}
}
}

Simplify setting of user.principal attribute (#509)