Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.getRootContext;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.spanFromContext;
import static datadog.trace.bootstrap.instrumentation.decorator.HttpServerDecorator.DD_CONTEXT_ATTRIBUTE;
import static datadog.trace.instrumentation.springweb.SpringWebHttpServerDecorator.DD_HANDLER_SPAN_CONTINUE_SUFFIX;
import static datadog.trace.instrumentation.springweb.SpringWebHttpServerDecorator.DD_HANDLER_SPAN_PREFIX_KEY;
import static datadog.trace.instrumentation.springweb.SpringWebHttpServerDecorator.DECORATE;
import static datadog.trace.instrumentation.springweb.SpringWebHttpServerDecorator.handlerSpanContinueKey;
import static datadog.trace.instrumentation.springweb.SpringWebHttpServerDecorator.handlerSpanKey;
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
Expand Down Expand Up @@ -71,8 +71,8 @@ public static class ControllerAdvice {
public static ContextScope nameResourceAndStartSpan(
@Advice.Argument(0) final HttpServletRequest request,
@Advice.Argument(2) final Object handler,
@Advice.Local("handlerSpanKey") String handlerSpanKey) {
handlerSpanKey = "";
@Advice.Local("handlerClass") Class handlerClass) {
handlerClass = null;

// Name the parent span based on the matching pattern
Object contextObj = request.getAttribute(DD_CONTEXT_ATTRIBUTE);
Expand All @@ -90,13 +90,12 @@ public static ContextScope nameResourceAndStartSpan(

// Now create a span for handler/controller execution.

final String handlerKey;
if (handler instanceof HandlerMethod) {
handlerKey = ((HandlerMethod) handler).getBean().getClass().getName();
handlerClass = ((HandlerMethod) handler).getBean().getClass();
} else {
handlerKey = handler.getClass().getName();
handlerClass = handler.getClass();
}
handlerSpanKey = DD_HANDLER_SPAN_PREFIX_KEY + handlerKey;
final String handlerSpanKey = handlerSpanKey(handlerClass);

// If the context already exists, return it
final Object existingContext = request.getAttribute(handlerSpanKey);
Expand All @@ -117,13 +116,12 @@ public static void stopSpan(
@Advice.Argument(0) final HttpServletRequest request,
@Advice.Enter final ContextScope scope,
@Advice.Thrown final Throwable throwable,
@Advice.Local("handlerSpanKey") String handlerSpanKey) {
@Advice.Local("handlerClass") Class handlerClass) {
if (scope == null) {
return;
}
boolean finish =
!Boolean.TRUE.equals(
request.getAttribute(handlerSpanKey + DD_HANDLER_SPAN_CONTINUE_SUFFIX));
!Boolean.TRUE.equals(request.getAttribute(handlerSpanContinueKey(handlerClass)));
final AgentSpan span = spanFromContext(scope.context());
scope.close();
if (throwable != null) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package datadog.trace.instrumentation.springweb;

import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named;
import static datadog.trace.instrumentation.springweb.SpringWebHttpServerDecorator.DD_HANDLER_SPAN_CONTINUE_SUFFIX;
import static datadog.trace.instrumentation.springweb.SpringWebHttpServerDecorator.DD_HANDLER_SPAN_PREFIX_KEY;
import static datadog.trace.instrumentation.springweb.SpringWebHttpServerDecorator.handlerSpanContinueKey;
import static datadog.trace.instrumentation.springweb.SpringWebHttpServerDecorator.handlerSpanKey;

import com.google.auto.service.AutoService;
import datadog.trace.agent.tooling.Instrumenter;
Expand Down Expand Up @@ -53,12 +53,12 @@ public static void after(
return;
}
ServletWebRequest servletWebRequest = (ServletWebRequest) nativeWebRequest;
final String handlerSpanKey =
DD_HANDLER_SPAN_PREFIX_KEY + self.getBean().getClass().getName();
final Class<?> handlerClass = self.getBean().getClass();
final String handlerSpanKey = handlerSpanKey(handlerClass);

if (Boolean.TRUE.equals(
servletWebRequest.getAttribute(
handlerSpanKey + DD_HANDLER_SPAN_CONTINUE_SUFFIX, ServletWebRequest.SCOPE_REQUEST))) {
handlerSpanContinueKey(handlerClass), ServletWebRequest.SCOPE_REQUEST))) {
return;
}

Expand All @@ -67,7 +67,7 @@ public static void after(
return;
}
servletWebRequest.setAttribute(
handlerSpanKey + DD_HANDLER_SPAN_CONTINUE_SUFFIX, true, ServletWebRequest.SCOPE_REQUEST);
handlerSpanContinueKey(handlerClass), true, ServletWebRequest.SCOPE_REQUEST);
result =
((CompletionStage<?>) result)
.whenComplete(AsyncResultExtensions.finishSpan((AgentSpan) span));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static datadog.trace.bootstrap.instrumentation.decorator.http.HttpResourceDecorator.HTTP_RESOURCE_DECORATOR;

import datadog.context.Context;
import datadog.trace.api.GenericClassValue;
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.URIDataAdapter;
Expand Down Expand Up @@ -33,6 +34,21 @@ public class SpringWebHttpServerDecorator
public static final String DD_HANDLER_SPAN_PREFIX_KEY = "dd.handler.span.";
public static final String DD_HANDLER_SPAN_CONTINUE_SUFFIX = ".continue";

private static final ClassValue<String> HANDLER_SPAN_KEY_CACHE =
GenericClassValue.of(type -> DD_HANDLER_SPAN_PREFIX_KEY + type.getName());

private static final ClassValue<String> HANDLER_SPAN_CONTINUE_KEY_CACHE =
GenericClassValue.of(
type -> DD_HANDLER_SPAN_PREFIX_KEY + type.getName() + DD_HANDLER_SPAN_CONTINUE_SUFFIX);

public static String handlerSpanKey(Class<?> handlerClass) {
return HANDLER_SPAN_KEY_CACHE.get(handlerClass);
}

public static String handlerSpanContinueKey(Class<?> handlerClass) {
return HANDLER_SPAN_CONTINUE_KEY_CACHE.get(handlerClass);
}

public SpringWebHttpServerDecorator(CharSequence component) {
this.component = component;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.getCurrentContext;
import static datadog.trace.bootstrap.instrumentation.api.Java8BytecodeBridge.spanFromContext;
import static datadog.trace.bootstrap.instrumentation.decorator.HttpServerDecorator.DD_CONTEXT_ATTRIBUTE;
import static datadog.trace.instrumentation.springweb6.SpringWebHttpServerDecorator.DD_HANDLER_SPAN_CONTINUE_SUFFIX;
import static datadog.trace.instrumentation.springweb6.SpringWebHttpServerDecorator.DD_HANDLER_SPAN_PREFIX_KEY;
import static datadog.trace.instrumentation.springweb6.SpringWebHttpServerDecorator.DECORATE;
import static datadog.trace.instrumentation.springweb6.SpringWebHttpServerDecorator.handlerSpanContinueKey;
import static datadog.trace.instrumentation.springweb6.SpringWebHttpServerDecorator.handlerSpanKey;

import datadog.context.Context;
import datadog.context.ContextScope;
Expand All @@ -23,8 +23,8 @@ public class ControllerAdvice {
public static ContextScope nameResourceAndStartSpan(
@Advice.Argument(0) final HttpServletRequest request,
@Advice.Argument(2) final Object handler,
@Advice.Local("handlerSpanKey") String handlerSpanKey) {
handlerSpanKey = "";
@Advice.Local("handlerClass") Class handlerClass) {
handlerClass = null;
// Name the parent span based on the matching pattern
Object contextObj = request.getAttribute(DD_CONTEXT_ATTRIBUTE);
if (contextObj instanceof Context) {
Expand All @@ -41,13 +41,12 @@ public static ContextScope nameResourceAndStartSpan(

// Now create a span for handler/controller execution.

final String handlerKey;
if (handler instanceof HandlerMethod) {
handlerKey = ((HandlerMethod) handler).getBean().getClass().getName();
handlerClass = ((HandlerMethod) handler).getBean().getClass();
} else {
handlerKey = handler.getClass().getName();
handlerClass = handler.getClass();
}
handlerSpanKey = DD_HANDLER_SPAN_PREFIX_KEY + handlerKey;
final String handlerSpanKey = handlerSpanKey(handlerClass);

// If the context already exists, return it
final Object existingContext = request.getAttribute(handlerSpanKey);
Expand All @@ -68,13 +67,12 @@ public static void stopSpan(
@Advice.Enter final ContextScope scope,
@Advice.Argument(0) final HttpServletRequest request,
@Advice.Thrown final Throwable throwable,
@Advice.Local("handlerSpanKey") String handlerSpanKey) {
@Advice.Local("handlerClass") Class handlerClass) {
if (scope == null) {
return;
}
boolean finish =
!Boolean.TRUE.equals(
request.getAttribute(handlerSpanKey + DD_HANDLER_SPAN_CONTINUE_SUFFIX));
!Boolean.TRUE.equals(request.getAttribute(handlerSpanContinueKey(handlerClass)));
final AgentSpan span = spanFromContext(scope.context());
scope.close();
if (throwable != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static datadog.trace.bootstrap.instrumentation.decorator.http.HttpResourceDecorator.HTTP_RESOURCE_DECORATOR;

import datadog.context.Context;
import datadog.trace.api.GenericClassValue;
import datadog.trace.bootstrap.instrumentation.api.AgentPropagation;
import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.api.URIDataAdapter;
Expand Down Expand Up @@ -37,6 +38,21 @@ public class SpringWebHttpServerDecorator
public static final String DD_HANDLER_SPAN_PREFIX_KEY = "dd.handler.span.";
public static final String DD_HANDLER_SPAN_CONTINUE_SUFFIX = ".continue";

private static final ClassValue<String> HANDLER_SPAN_KEY_CACHE =
GenericClassValue.of(type -> DD_HANDLER_SPAN_PREFIX_KEY + type.getName());

private static final ClassValue<String> HANDLER_SPAN_CONTINUE_KEY_CACHE =
GenericClassValue.of(
type -> DD_HANDLER_SPAN_PREFIX_KEY + type.getName() + DD_HANDLER_SPAN_CONTINUE_SUFFIX);

public static String handlerSpanKey(Class<?> handlerClass) {
return HANDLER_SPAN_KEY_CACHE.get(handlerClass);
}

public static String handlerSpanContinueKey(Class<?> handlerClass) {
return HANDLER_SPAN_CONTINUE_KEY_CACHE.get(handlerClass);
}

public SpringWebHttpServerDecorator(CharSequence component) {
this.component = component;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package datadog.trace.instrumentation.springweb6;

import static datadog.trace.instrumentation.springweb6.SpringWebHttpServerDecorator.DD_HANDLER_SPAN_CONTINUE_SUFFIX;
import static datadog.trace.instrumentation.springweb6.SpringWebHttpServerDecorator.DD_HANDLER_SPAN_PREFIX_KEY;
import static datadog.trace.instrumentation.springweb6.SpringWebHttpServerDecorator.handlerSpanContinueKey;
import static datadog.trace.instrumentation.springweb6.SpringWebHttpServerDecorator.handlerSpanKey;

import datadog.trace.bootstrap.instrumentation.api.AgentSpan;
import datadog.trace.bootstrap.instrumentation.java.concurrent.AsyncResultExtensions;
Expand All @@ -24,19 +24,20 @@ public static void after(
}

ServletWebRequest servletWebRequest = (ServletWebRequest) nativeWebRequest;
final String handlerSpanKey = DD_HANDLER_SPAN_PREFIX_KEY + self.getBean().getClass().getName();
final Class<?> handlerClass = self.getBean().getClass();
final String handlerSpanKey = handlerSpanKey(handlerClass);

if (Boolean.TRUE.equals(
servletWebRequest.getAttribute(
handlerSpanKey + DD_HANDLER_SPAN_CONTINUE_SUFFIX, ServletWebRequest.SCOPE_REQUEST))) {
handlerSpanContinueKey(handlerClass), ServletWebRequest.SCOPE_REQUEST))) {
return;
}
Object span = servletWebRequest.getAttribute(handlerSpanKey, ServletWebRequest.SCOPE_REQUEST);
if (!(span instanceof AgentSpan)) {
return;
}
servletWebRequest.setAttribute(
handlerSpanKey + DD_HANDLER_SPAN_CONTINUE_SUFFIX, true, ServletWebRequest.SCOPE_REQUEST);
handlerSpanContinueKey(handlerClass), true, ServletWebRequest.SCOPE_REQUEST);
result =
((CompletionStage<?>) result)
.whenComplete(AsyncResultExtensions.finishSpan((AgentSpan) span));
Expand Down
Loading