|
| 1 | +package datadog.trace.instrumentation.jetty11; |
| 2 | + |
| 3 | +import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.named; |
| 4 | +import static datadog.trace.agent.tooling.bytebuddy.matcher.NameMatchers.namedOneOf; |
| 5 | +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; |
| 6 | +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.propagate; |
| 7 | +import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.startSpan; |
| 8 | +import static datadog.trace.bootstrap.instrumentation.decorator.HttpServerDecorator.DD_SPAN_ATTRIBUTE; |
| 9 | +import static datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter.ExcludeType.RUNNABLE; |
| 10 | +import static datadog.trace.instrumentation.jetty11.JettyDecorator.DECORATE; |
| 11 | +import static datadog.trace.instrumentation.jetty11.JettyDecorator.SERVLET_REQUEST; |
| 12 | +import static datadog.trace.instrumentation.jetty11.RequestExtractAdapter.GETTER; |
| 13 | +import static net.bytebuddy.matcher.ElementMatchers.declaresMethod; |
| 14 | +import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments; |
| 15 | + |
| 16 | +import com.google.auto.service.AutoService; |
| 17 | +import datadog.trace.agent.tooling.ExcludeFilterProvider; |
| 18 | +import datadog.trace.agent.tooling.Instrumenter; |
| 19 | +import datadog.trace.api.CorrelationIdentifier; |
| 20 | +import datadog.trace.api.GlobalTracer; |
| 21 | +import datadog.trace.bootstrap.instrumentation.api.AgentScope; |
| 22 | +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; |
| 23 | +import datadog.trace.bootstrap.instrumentation.java.concurrent.ExcludeFilter; |
| 24 | +import java.util.Arrays; |
| 25 | +import java.util.Collection; |
| 26 | +import java.util.Collections; |
| 27 | +import java.util.Map; |
| 28 | +import net.bytebuddy.asm.Advice; |
| 29 | +import net.bytebuddy.description.method.MethodDescription; |
| 30 | +import net.bytebuddy.description.type.TypeDescription; |
| 31 | +import net.bytebuddy.matcher.ElementMatcher; |
| 32 | +import org.eclipse.jetty.server.HttpChannel; |
| 33 | +import org.eclipse.jetty.server.Request; |
| 34 | + |
| 35 | +@AutoService(Instrumenter.class) |
| 36 | +public final class JettyServerInstrumentation extends Instrumenter.Tracing |
| 37 | + implements ExcludeFilterProvider { |
| 38 | + |
| 39 | + public JettyServerInstrumentation() { |
| 40 | + super("jetty"); |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public ElementMatcher<TypeDescription> typeMatcher() { |
| 45 | + return named("org.eclipse.jetty.server.HttpChannel"); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public String[] helperClassNames() { |
| 50 | + return new String[] { |
| 51 | + packageName + ".JettyDecorator", |
| 52 | + packageName + ".RequestExtractAdapter", |
| 53 | + packageName + ".RequestURIDataAdapter", |
| 54 | + }; |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void adviceTransformations(AdviceTransformation transformation) { |
| 59 | + transformation.applyAdvice( |
| 60 | + takesNoArguments() |
| 61 | + .and( |
| 62 | + named("handle") |
| 63 | + .or( |
| 64 | + // In 9.0.3 the handle logic was extracted out to "handle" |
| 65 | + // but we still want to instrument run in case handle is missing |
| 66 | + // (without the risk of double instrumenting). |
| 67 | + named("run") |
| 68 | + .and( |
| 69 | + new ElementMatcher.Junction.AbstractBase<MethodDescription>() { |
| 70 | + @Override |
| 71 | + public boolean matches(MethodDescription target) { |
| 72 | + // TODO this could probably be made into a nicer matcher. |
| 73 | + return !declaresMethod(named("handle")) |
| 74 | + .matches(target.getDeclaringType().asErasure()); |
| 75 | + } |
| 76 | + }))), |
| 77 | + JettyServerInstrumentation.class.getName() + "$HandleAdvice"); |
| 78 | + transformation.applyAdvice( |
| 79 | + // name changed to recycle in 9.3.0 |
| 80 | + namedOneOf("reset", "recycle").and(takesNoArguments()), |
| 81 | + JettyServerInstrumentation.class.getName() + "$ResetAdvice"); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public Map<ExcludeFilter.ExcludeType, ? extends Collection<String>> excludedClasses() { |
| 86 | + return Collections.singletonMap( |
| 87 | + RUNNABLE, |
| 88 | + Arrays.asList( |
| 89 | + "org.eclipse.jetty.util.thread.strategy.ProduceConsume", |
| 90 | + "org.eclipse.jetty.util.thread.strategy.ExecuteProduceConsume", |
| 91 | + "org.eclipse.jetty.io.ManagedSelector", |
| 92 | + "org.eclipse.jetty.util.thread.TimerScheduler", |
| 93 | + "org.eclipse.jetty.util.thread.TimerScheduler$SimpleTask")); |
| 94 | + } |
| 95 | + |
| 96 | + public static class HandleAdvice { |
| 97 | + |
| 98 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 99 | + public static AgentScope onEnter(@Advice.This final HttpChannel channel) { |
| 100 | + Request req = channel.getRequest(); |
| 101 | + |
| 102 | + Object existingSpan = req.getAttribute(DD_SPAN_ATTRIBUTE); |
| 103 | + if (existingSpan instanceof AgentSpan) { |
| 104 | + // Request already gone through initial processing, so just activate the span. |
| 105 | + return activateSpan((AgentSpan) existingSpan); |
| 106 | + } |
| 107 | + |
| 108 | + final AgentSpan.Context.Extracted extractedContext = propagate().extract(req, GETTER); |
| 109 | + |
| 110 | + final AgentSpan span = startSpan(SERVLET_REQUEST, extractedContext).setMeasured(true); |
| 111 | + DECORATE.afterStart(span); |
| 112 | + DECORATE.onRequest(span, req, req, extractedContext); |
| 113 | + |
| 114 | + final AgentScope scope = activateSpan(span); |
| 115 | + scope.setAsyncPropagation(true); |
| 116 | + req.setAttribute(DD_SPAN_ATTRIBUTE, span); |
| 117 | + req.setAttribute(CorrelationIdentifier.getTraceIdKey(), GlobalTracer.get().getTraceId()); |
| 118 | + req.setAttribute(CorrelationIdentifier.getSpanIdKey(), GlobalTracer.get().getSpanId()); |
| 119 | + return scope; |
| 120 | + } |
| 121 | + |
| 122 | + @Advice.OnMethodExit(suppress = Throwable.class, onThrowable = Throwable.class) |
| 123 | + public static void closeScope(@Advice.Enter final AgentScope scope) { |
| 124 | + scope.close(); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Jetty ensures that connections are reset immediately after the response is sent. This provides |
| 130 | + * a reliable point to finish the server span at the last possible moment. |
| 131 | + */ |
| 132 | + public static class ResetAdvice { |
| 133 | + @Advice.OnMethodEnter(suppress = Throwable.class) |
| 134 | + public static void stopSpan(@Advice.This final HttpChannel channel) { |
| 135 | + Request req = channel.getRequest(); |
| 136 | + Object spanObj = req.getAttribute(DD_SPAN_ATTRIBUTE); |
| 137 | + if (spanObj instanceof AgentSpan) { |
| 138 | + final AgentSpan span = (AgentSpan) spanObj; |
| 139 | + DECORATE.onResponse(span, channel); |
| 140 | + DECORATE.beforeFinish(span); |
| 141 | + span.finish(); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + private void muzzleCheck(HttpChannel connection) { |
| 146 | + connection.run(); |
| 147 | + } |
| 148 | + } |
| 149 | +} |
0 commit comments