Skip to content

Commit

Permalink
move servlet context path handling
Browse files Browse the repository at this point in the history
  • Loading branch information
laurit committed Feb 10, 2021
1 parent bb98885 commit d03281e
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import io.opentelemetry.context.Context;
import io.opentelemetry.context.propagation.TextMapPropagator.Getter;
import io.opentelemetry.instrumentation.api.servlet.AppServerBridge;
import io.opentelemetry.instrumentation.api.servlet.ServletContextPath;
import io.opentelemetry.instrumentation.api.tracer.HttpServerTracer;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.net.URI;
Expand All @@ -25,7 +26,16 @@ public abstract class ServletHttpServerTracer<RESPONSE>
private static final Logger log = LoggerFactory.getLogger(ServletHttpServerTracer.class);

public Context startSpan(HttpServletRequest request) {
return startSpan(request, request, request, getSpanName(request));
Context context = startSpan(request, request, request, getSpanName(request));
return addServletContextPath(context, request);
}

private static Context addServletContextPath(Context context, HttpServletRequest request) {
String contextPath = request.getContextPath();
if (contextPath != null && !contextPath.isEmpty() && !contextPath.equals("/")) {
return context.with(ServletContextPath.CONTEXT_KEY, contextPath);
}
return context;
}

@Override
Expand Down Expand Up @@ -153,11 +163,13 @@ public static String getSpanName(HttpServletRequest request) {
* forward and other scenarios, where servlet path may change, but we don't want this to be
* reflected in the span name.
*/
public void updateServerSpanNameOnce(Context attachedContext, HttpServletRequest request) {
if (AppServerBridge.shouldUpdateServerSpanName(attachedContext)) {
updateSpanName(Span.fromContext(attachedContext), request);
AppServerBridge.setServletUpdatedServerSpanName(attachedContext, true);
public Context runOnceUnderAppServer(Context context, HttpServletRequest request) {
if (AppServerBridge.shouldUpdateServerSpanName(context)) {
updateSpanName(Span.fromContext(context), request);
AppServerBridge.setServletUpdatedServerSpanName(context, true);
return addServletContextPath(context, request);
}
return context;
}

public void updateSpanName(HttpServletRequest request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ public static void onEnter(

Context serverContext = tracer().getServerContext(httpServletRequest);
if (serverContext != null) {
tracer().updateServerSpanNameOnce(serverContext, httpServletRequest);
Context updatedContext = tracer().runOnceUnderAppServer(serverContext, httpServletRequest);
if (updatedContext != serverContext) {
// runOnceUnderAppServer updated context, need to re-scope
scope = updatedContext.makeCurrent();
}
return;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,35 @@ public static void onEnter(

Context attachedContext = tracer().getServerContext(httpServletRequest);
if (attachedContext != null) {
// We are inside nested servlet/filter/app-server span, don't create new span
if (Servlet3HttpServerTracer.needsRescoping(attachedContext)) {
attachedContext = tracer().runOnceUnderAppServer(attachedContext, httpServletRequest);
scope = attachedContext.makeCurrent();
return;
}

tracer().updateServerSpanNameOnce(attachedContext, httpServletRequest);
// We are inside nested servlet/filter/app-server span, don't create new span
// We already have attached context to request but this could have been done by app server
// instrumentation, if needed update span with info from current request.
Context currentContext = Java8BytecodeBridge.currentContext();
Context updatedContext = tracer().runOnceUnderAppServer(currentContext, httpServletRequest);
if (updatedContext != currentContext) {
// runOnceUnderAppServer updated context, need to re-scope
scope = updatedContext.makeCurrent();
}
return;
}

Context parentContext = Java8BytecodeBridge.currentContext();
if (parentContext != null && Java8BytecodeBridge.spanFromContext(parentContext).isRecording()) {
tracer().updateServerSpanNameOnce(parentContext, httpServletRequest);
// We are inside nested servlet/filter/app-server span, don't create new span
Context currentContext = Java8BytecodeBridge.currentContext();
if (currentContext != null
&& Java8BytecodeBridge.spanFromContext(currentContext).isRecording()) {
// We already have a span but it was not created by servlet instrumentation.
// In case it was created by app server integration we need to update it with info from
// current request.
Context updatedContext = tracer().runOnceUnderAppServer(currentContext, httpServletRequest);
if (currentContext != updatedContext) {
// runOnceUnderAppServer updated context, need to re-scope
scope = updatedContext.makeCurrent();
}
return;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

package io.opentelemetry.javaagent.instrumentation.tomcat7

import static io.opentelemetry.api.trace.Span.Kind.INTERNAL
import static io.opentelemetry.api.trace.SpanKind.INTERNAL
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.ERROR
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.EXCEPTION
import static io.opentelemetry.instrumentation.test.base.HttpServerTest.ServerEndpoint.REDIRECT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -445,23 +445,23 @@ abstract class HttpServerTest<SERVER> extends AgentInstrumentationSpecification
void redirectSpan(TraceAssert trace, int index, Object parent) {
trace.span(index) {
name ~/\.sendRedirect$/
kind Span.Kind.INTERNAL
kind SpanKind.INTERNAL
childOf((SpanData) parent)
}
}

void sendErrorSpan(TraceAssert trace, int index, Object parent) {
trace.span(index) {
name ~/\.sendError$/
kind Span.Kind.INTERNAL
kind SpanKind.INTERNAL
childOf((SpanData) parent)
}
}

void forwardSpan(TraceAssert trace, int index, Object parent, String errorMessage = null, Class exceptionClass = Exception) {
trace.span(index) {
name ~/\.forward$/
kind Span.Kind.INTERNAL
kind SpanKind.INTERNAL
errored errorMessage != null
if (errorMessage) {
errorEvent(exceptionClass, errorMessage)
Expand All @@ -473,7 +473,7 @@ abstract class HttpServerTest<SERVER> extends AgentInstrumentationSpecification
void includeSpan(TraceAssert trace, int index, Object parent, String errorMessage = null, Class exceptionClass = Exception) {
trace.span(index) {
name ~/\.include$/
kind Span.Kind.INTERNAL
kind SpanKind.INTERNAL
errored errorMessage != null
if (errorMessage) {
errorEvent(exceptionClass, errorMessage)
Expand Down

0 comments on commit d03281e

Please sign in to comment.