Small reduction in context lookups
MODIFY
instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java
instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java
@@ -58,8 +58,9 @@ public class Servlet3Advice {
.get((Filter) servletOrFilter);
}
+ Context currentContext = Java8BytecodeBridge.currentContext();
Context attachedContext = tracer().getServerContext(httpServletRequest);
- if (attachedContext != null && tracer().needsRescoping(attachedContext)) {
+ if (attachedContext != null && tracer().needsRescoping(currentContext, attachedContext)) {
attachedContext =
tracer().updateContext(attachedContext, httpServletRequest, mappingResolver, servlet);
scope = attachedContext.makeCurrent();
@@ -67,7 +68,6 @@ public class Servlet3Advice {
return;
}
- Context currentContext = Java8BytecodeBridge.currentContext();
if (attachedContext != null || ServerSpan.fromContextOrNull(currentContext) != null) {
// Update context with info from current request to ensure that server span gets the best
// possible name.

MODIFY
instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/service/JakartaServletServiceAdvice.java
instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/service/JakartaServletServiceAdvice.java
@@ -58,8 +58,9 @@ public class JakartaServletServiceAdvice {
.get((Filter) servletOrFilter);
}
+ Context currentContext = Java8BytecodeBridge.currentContext();
Context attachedContext = tracer().getServerContext(httpServletRequest);
- if (attachedContext != null && tracer().needsRescoping(attachedContext)) {
+ if (attachedContext != null && tracer().needsRescoping(currentContext, attachedContext)) {
attachedContext =
tracer().updateContext(attachedContext, httpServletRequest, mappingResolver, servlet);
scope = attachedContext.makeCurrent();
@@ -67,7 +68,6 @@ public class JakartaServletServiceAdvice {
return;
}
- Context currentContext = Java8BytecodeBridge.currentContext();
if (attachedContext != null || ServerSpan.fromContextOrNull(currentContext) != null) {
// Update context with info from current request to ensure that server span gets the best
// possible name.

MODIFY
instrumentation/servlet/servlet-common/library/src/main/java/io/opentelemetry/instrumentation/servlet/ServletHttpServerTracer.java
instrumentation/servlet/servlet-common/library/src/main/java/io/opentelemetry/instrumentation/servlet/ServletHttpServerTracer.java
@@ -266,8 +266,8 @@ public abstract class ServletHttpServerTracer<REQUEST, RESPONSE>
In this case we have to put the span from the request into current context before continuing.
*/
- public boolean needsRescoping(Context attachedContext) {
- return !sameTrace(Span.fromContext(Context.current()), Span.fromContext(attachedContext));
+ public boolean needsRescoping(Context currentContext, Context attachedContext) {
+ return !sameTrace(Span.fromContext(currentContext), Span.fromContext(attachedContext));
}
private static boolean sameTrace(Span oneSpan, Span otherSpan) {

Small reduction in context lookups (#4006)