-
Notifications
You must be signed in to change notification settings - Fork 292
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #582 from DataDog/tyler/servlet-dispatch
Fix servlet async dispatch
- Loading branch information
Showing
13 changed files
with
703 additions
and
395 deletions.
There are no files selected for viewing
19 changes: 0 additions & 19 deletions
19
...src/main/java/datadog/trace/instrumentation/servlet3/AbstractServlet3Instrumentation.java
This file was deleted.
Oops, something went wrong.
97 changes: 97 additions & 0 deletions
97
...t-3/src/main/java/datadog/trace/instrumentation/servlet3/AsyncContextInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package datadog.trace.instrumentation.servlet3; | ||
|
||
import static datadog.trace.agent.tooling.ByteBuddyElementMatchers.safeHasSuperType; | ||
import static datadog.trace.instrumentation.servlet3.Servlet3Advice.SERVLET_SPAN; | ||
import static net.bytebuddy.matcher.ElementMatchers.isInterface; | ||
import static net.bytebuddy.matcher.ElementMatchers.isMethod; | ||
import static net.bytebuddy.matcher.ElementMatchers.isPublic; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.not; | ||
|
||
import com.google.auto.service.AutoService; | ||
import datadog.trace.agent.tooling.Instrumenter; | ||
import datadog.trace.bootstrap.CallDepthThreadLocalMap; | ||
import io.opentracing.Span; | ||
import io.opentracing.propagation.Format; | ||
import io.opentracing.util.GlobalTracer; | ||
import java.util.Collections; | ||
import java.util.Map; | ||
import javax.servlet.AsyncContext; | ||
import javax.servlet.ServletRequest; | ||
import javax.servlet.http.HttpServletRequest; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
@AutoService(Instrumenter.class) | ||
public final class AsyncContextInstrumentation extends Instrumenter.Default { | ||
|
||
public AsyncContextInstrumentation() { | ||
super("servlet", "servlet-3"); | ||
} | ||
|
||
@Override | ||
public String[] helperClassNames() { | ||
return new String[] {"datadog.trace.instrumentation.servlet3.HttpServletRequestInjectAdapter"}; | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return not(isInterface()).and(safeHasSuperType(named("javax.servlet.AsyncContext"))); | ||
} | ||
|
||
@Override | ||
public Map<? extends ElementMatcher, String> transformers() { | ||
return Collections.singletonMap( | ||
isMethod().and(isPublic()).and(named("dispatch")), DispatchAdvice.class.getName()); | ||
} | ||
|
||
/** | ||
* When a request is dispatched, we want to close out the existing request and replace the | ||
* propagation info in the headers with the closed span. | ||
*/ | ||
public static class DispatchAdvice { | ||
|
||
@Advice.OnMethodEnter(suppress = Throwable.class) | ||
public static boolean enter( | ||
@Advice.This final AsyncContext context, @Advice.AllArguments final Object[] args) { | ||
final int depth = CallDepthThreadLocalMap.incrementCallDepth(AsyncContext.class); | ||
if (depth > 0) { | ||
return false; | ||
} | ||
|
||
final ServletRequest request = context.getRequest(); | ||
final Object spanAttr = request.getAttribute(SERVLET_SPAN); | ||
if (spanAttr instanceof Span) { | ||
request.removeAttribute(SERVLET_SPAN); | ||
final Span span = (Span) spanAttr; | ||
// Override propagation headers by injecting attributes with new values. | ||
if (request instanceof HttpServletRequest) { | ||
GlobalTracer.get() | ||
.inject( | ||
span.context(), | ||
Format.Builtin.TEXT_MAP, | ||
new HttpServletRequestInjectAdapter((HttpServletRequest) request)); | ||
} | ||
final String path; | ||
if (args.length == 1 && args[0] instanceof String) { | ||
path = (String) args[0]; | ||
} else if (args.length == 2 && args[1] instanceof String) { | ||
path = (String) args[1]; | ||
} else { | ||
path = "true"; | ||
} | ||
span.setTag("servlet.dispatch", path); | ||
span.finish(); | ||
} | ||
return true; | ||
} | ||
|
||
@Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) | ||
public static void exit(@Advice.Enter final boolean topLevel) { | ||
if (topLevel) { | ||
CallDepthThreadLocalMap.reset(AsyncContext.class); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
...src/main/java/datadog/trace/instrumentation/servlet3/HttpServletRequestInjectAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package datadog.trace.instrumentation.servlet3; | ||
|
||
import io.opentracing.propagation.TextMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import javax.servlet.http.HttpServletRequest; | ||
|
||
/** Inject into request attributes since the request headers can't be modified. */ | ||
public class HttpServletRequestInjectAdapter implements TextMap { | ||
|
||
private final HttpServletRequest httpServletRequest; | ||
|
||
public HttpServletRequestInjectAdapter(final HttpServletRequest httpServletRequest) { | ||
this.httpServletRequest = httpServletRequest; | ||
} | ||
|
||
@Override | ||
public Iterator<Map.Entry<String, String>> iterator() { | ||
throw new UnsupportedOperationException( | ||
"This class should be used only with Tracer.extract()!"); | ||
} | ||
|
||
@Override | ||
public void put(final String key, final String value) { | ||
httpServletRequest.setAttribute(key, value); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.