From c91eda59b57cd9328db7e4eb81268efb4046b9e8 Mon Sep 17 00:00:00 2001 From: Trask Stalnaker Date: Mon, 30 Aug 2021 09:02:52 -0700 Subject: [PATCH] Small reduction in context lookups (#4006) --- .../instrumentation/servlet/v3_0/Servlet3Advice.java | 4 ++-- .../servlet/v5_0/service/JakartaServletServiceAdvice.java | 4 ++-- .../instrumentation/servlet/ServletHttpServerTracer.java | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java index ddd75bf8496a..296e349306f0 100644 --- a/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java +++ b/instrumentation/servlet/servlet-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v3_0/Servlet3Advice.java @@ -58,8 +58,9 @@ public static void onEnter( .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 static void onEnter( 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. diff --git a/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/service/JakartaServletServiceAdvice.java b/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/service/JakartaServletServiceAdvice.java index 3ae567ea3432..a36366195d22 100644 --- a/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/service/JakartaServletServiceAdvice.java +++ b/instrumentation/servlet/servlet-5.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/servlet/v5_0/service/JakartaServletServiceAdvice.java @@ -58,8 +58,9 @@ public static void onEnter( .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 static void onEnter( 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. diff --git a/instrumentation/servlet/servlet-common/library/src/main/java/io/opentelemetry/instrumentation/servlet/ServletHttpServerTracer.java b/instrumentation/servlet/servlet-common/library/src/main/java/io/opentelemetry/instrumentation/servlet/ServletHttpServerTracer.java index 82f2565a357c..853febc2741d 100644 --- a/instrumentation/servlet/servlet-common/library/src/main/java/io/opentelemetry/instrumentation/servlet/ServletHttpServerTracer.java +++ b/instrumentation/servlet/servlet-common/library/src/main/java/io/opentelemetry/instrumentation/servlet/ServletHttpServerTracer.java @@ -266,8 +266,8 @@ public void onTimeout(Context context, long timeout) { 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) {