Update jetty-8 to new agent api
This commit is contained in:
parent
1e81929aa8
commit
b3a86085cb
|
@ -1,96 +1,23 @@
|
|||
package datadog.trace.instrumentation.jetty8;
|
||||
|
||||
import io.opentracing.propagation.TextMap;
|
||||
import java.util.AbstractMap;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import datadog.trace.instrumentation.api.AgentPropagation;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
/**
|
||||
* Tracer extract adapter for {@link HttpServletRequest}.
|
||||
*
|
||||
* @author Pavol Loffay
|
||||
*/
|
||||
// FIXME: This code is duplicated in several places. Extract to a common dependency.
|
||||
public class HttpServletRequestExtractAdapter implements TextMap {
|
||||
public class HttpServletRequestExtractAdapter
|
||||
implements AgentPropagation.Getter<HttpServletRequest> {
|
||||
|
||||
private final Map<String, List<String>> headers;
|
||||
public static final HttpServletRequestExtractAdapter GETTER =
|
||||
new HttpServletRequestExtractAdapter();
|
||||
|
||||
public HttpServletRequestExtractAdapter(final HttpServletRequest httpServletRequest) {
|
||||
headers = servletHeadersToMultiMap(httpServletRequest);
|
||||
@Override
|
||||
public List<String> keys(final HttpServletRequest carrier) {
|
||||
return Collections.list(carrier.getHeaderNames());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Map.Entry<String, String>> iterator() {
|
||||
return new MultivaluedMapFlatIterator<>(headers.entrySet());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void put(final String key, final String value) {
|
||||
throw new UnsupportedOperationException("This class should be used only with Tracer.inject()!");
|
||||
}
|
||||
|
||||
protected Map<String, List<String>> servletHeadersToMultiMap(
|
||||
final HttpServletRequest httpServletRequest) {
|
||||
final Map<String, List<String>> headersResult = new HashMap<>();
|
||||
|
||||
final Enumeration<String> headerNamesIt = httpServletRequest.getHeaderNames();
|
||||
while (headerNamesIt.hasMoreElements()) {
|
||||
final String headerName = headerNamesIt.nextElement();
|
||||
|
||||
final Enumeration<String> valuesIt = httpServletRequest.getHeaders(headerName);
|
||||
final List<String> valuesList = new ArrayList<>(1);
|
||||
while (valuesIt.hasMoreElements()) {
|
||||
valuesList.add(valuesIt.nextElement());
|
||||
}
|
||||
|
||||
headersResult.put(headerName, valuesList);
|
||||
}
|
||||
|
||||
return headersResult;
|
||||
}
|
||||
|
||||
public static final class MultivaluedMapFlatIterator<K, V> implements Iterator<Map.Entry<K, V>> {
|
||||
|
||||
private final Iterator<Map.Entry<K, List<V>>> mapIterator;
|
||||
private Map.Entry<K, List<V>> mapEntry;
|
||||
private Iterator<V> listIterator;
|
||||
|
||||
public MultivaluedMapFlatIterator(final Set<Map.Entry<K, List<V>>> multiValuesEntrySet) {
|
||||
mapIterator = multiValuesEntrySet.iterator();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
if (listIterator != null && listIterator.hasNext()) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return mapIterator.hasNext();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Map.Entry<K, V> next() {
|
||||
if (mapEntry == null || (!listIterator.hasNext() && mapIterator.hasNext())) {
|
||||
mapEntry = mapIterator.next();
|
||||
listIterator = mapEntry.getValue().iterator();
|
||||
}
|
||||
|
||||
if (listIterator.hasNext()) {
|
||||
return new AbstractMap.SimpleImmutableEntry<>(mapEntry.getKey(), listIterator.next());
|
||||
} else {
|
||||
return new AbstractMap.SimpleImmutableEntry<>(mapEntry.getKey(), null);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw new UnsupportedOperationException();
|
||||
}
|
||||
public String get(final HttpServletRequest carrier, final String key) {
|
||||
return carrier.getHeader(key);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
package datadog.trace.instrumentation.jetty8;
|
||||
|
||||
import datadog.trace.agent.decorator.HttpServerDecorator;
|
||||
import io.opentracing.Span;
|
||||
import datadog.trace.instrumentation.api.AgentSpan;
|
||||
import java.net.URI;
|
||||
import java.net.URISyntaxException;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
@ -52,7 +52,7 @@ public class JettyDecorator
|
|||
}
|
||||
|
||||
@Override
|
||||
public Span onRequest(final Span span, final HttpServletRequest request) {
|
||||
public AgentSpan onRequest(final AgentSpan span, final HttpServletRequest request) {
|
||||
assert span != null;
|
||||
if (request != null) {
|
||||
span.setTag("servlet.context", request.getContextPath());
|
||||
|
|
|
@ -1,16 +1,16 @@
|
|||
package datadog.trace.instrumentation.jetty8;
|
||||
|
||||
import static datadog.trace.agent.decorator.HttpServerDecorator.DD_SPAN_ATTRIBUTE;
|
||||
import static datadog.trace.instrumentation.api.AgentTracer.activateSpan;
|
||||
import static datadog.trace.instrumentation.api.AgentTracer.propagate;
|
||||
import static datadog.trace.instrumentation.api.AgentTracer.startSpan;
|
||||
import static datadog.trace.instrumentation.jetty8.HttpServletRequestExtractAdapter.GETTER;
|
||||
import static datadog.trace.instrumentation.jetty8.JettyDecorator.DECORATE;
|
||||
|
||||
import datadog.trace.api.DDTags;
|
||||
import datadog.trace.context.TraceScope;
|
||||
import io.opentracing.Scope;
|
||||
import io.opentracing.Span;
|
||||
import io.opentracing.SpanContext;
|
||||
import io.opentracing.propagation.Format;
|
||||
import datadog.trace.instrumentation.api.AgentScope;
|
||||
import datadog.trace.instrumentation.api.AgentSpan;
|
||||
import io.opentracing.tag.Tags;
|
||||
import io.opentracing.util.GlobalTracer;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
@ -19,7 +19,7 @@ import net.bytebuddy.asm.Advice;
|
|||
public class JettyHandlerAdvice {
|
||||
|
||||
@Advice.OnMethodEnter(suppress = Throwable.class)
|
||||
public static Scope startSpan(
|
||||
public static AgentScope onEnter(
|
||||
@Advice.This final Object source, @Advice.Argument(2) final HttpServletRequest req) {
|
||||
|
||||
if (req.getAttribute(DD_SPAN_ATTRIBUTE) != null) {
|
||||
|
@ -27,27 +27,19 @@ public class JettyHandlerAdvice {
|
|||
return null;
|
||||
}
|
||||
|
||||
final SpanContext extractedContext =
|
||||
GlobalTracer.get()
|
||||
.extract(Format.Builtin.HTTP_HEADERS, new HttpServletRequestExtractAdapter(req));
|
||||
final Scope scope =
|
||||
GlobalTracer.get()
|
||||
.buildSpan("jetty.request")
|
||||
.ignoreActiveSpan()
|
||||
.asChildOf(extractedContext)
|
||||
.withTag("span.origin.type", source.getClass().getName())
|
||||
.startActive(false);
|
||||
final AgentSpan.Context extractedContext = propagate().extract(req, GETTER);
|
||||
|
||||
final Span span = scope.span();
|
||||
final AgentSpan span =
|
||||
startSpan("jetty.request", extractedContext)
|
||||
.setTag("span.origin.type", source.getClass().getName());
|
||||
DECORATE.afterStart(span);
|
||||
DECORATE.onConnection(span, req);
|
||||
DECORATE.onRequest(span, req);
|
||||
final String resourceName = req.getMethod() + " " + source.getClass().getName();
|
||||
span.setTag(DDTags.RESOURCE_NAME, resourceName);
|
||||
|
||||
if (scope instanceof TraceScope) {
|
||||
((TraceScope) scope).setAsyncPropagation(true);
|
||||
}
|
||||
final AgentScope scope = activateSpan(span, false);
|
||||
scope.setAsyncPropagation(true);
|
||||
req.setAttribute(DD_SPAN_ATTRIBUTE, span);
|
||||
return scope;
|
||||
}
|
||||
|
@ -56,10 +48,10 @@ public class JettyHandlerAdvice {
|
|||
public static void stopSpan(
|
||||
@Advice.Argument(2) final HttpServletRequest req,
|
||||
@Advice.Argument(3) final HttpServletResponse resp,
|
||||
@Advice.Enter final Scope scope,
|
||||
@Advice.Enter final AgentScope scope,
|
||||
@Advice.Thrown final Throwable throwable) {
|
||||
if (scope != null) {
|
||||
final Span span = scope.span();
|
||||
final AgentSpan span = scope.span();
|
||||
if (req.getUserPrincipal() != null) {
|
||||
span.setTag(DDTags.USER_NAME, req.getUserPrincipal().getName());
|
||||
}
|
||||
|
@ -67,7 +59,7 @@ public class JettyHandlerAdvice {
|
|||
DECORATE.onResponse(span, resp);
|
||||
if (resp.getStatus() == HttpServletResponse.SC_OK) {
|
||||
// exception is thrown in filter chain, but status code is incorrect
|
||||
Tags.HTTP_STATUS.set(span, 500);
|
||||
span.setTag(Tags.HTTP_STATUS.getKey(), 500);
|
||||
}
|
||||
DECORATE.onError(span, throwable);
|
||||
DECORATE.beforeFinish(span);
|
||||
|
|
|
@ -42,7 +42,6 @@ public final class JettyHandlerInstrumentation extends Instrumenter.Default {
|
|||
"datadog.trace.agent.decorator.HttpServerDecorator",
|
||||
packageName + ".JettyDecorator",
|
||||
packageName + ".HttpServletRequestExtractAdapter",
|
||||
packageName + ".HttpServletRequestExtractAdapter$MultivaluedMapFlatIterator",
|
||||
packageName + ".TagSettingAsyncListener"
|
||||
};
|
||||
}
|
||||
|
|
|
@ -2,7 +2,7 @@ package datadog.trace.instrumentation.jetty8;
|
|||
|
||||
import static datadog.trace.instrumentation.jetty8.JettyDecorator.DECORATE;
|
||||
|
||||
import io.opentracing.Span;
|
||||
import datadog.trace.instrumentation.api.AgentSpan;
|
||||
import io.opentracing.tag.Tags;
|
||||
import java.io.IOException;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
@ -12,9 +12,9 @@ import javax.servlet.http.HttpServletResponse;
|
|||
|
||||
public class TagSettingAsyncListener implements AsyncListener {
|
||||
private final AtomicBoolean activated;
|
||||
private final Span span;
|
||||
private final AgentSpan span;
|
||||
|
||||
public TagSettingAsyncListener(final AtomicBoolean activated, final Span span) {
|
||||
public TagSettingAsyncListener(final AtomicBoolean activated, final AgentSpan span) {
|
||||
this.activated = activated;
|
||||
this.span = span;
|
||||
}
|
||||
|
@ -31,7 +31,7 @@ public class TagSettingAsyncListener implements AsyncListener {
|
|||
@Override
|
||||
public void onTimeout(final AsyncEvent event) throws IOException {
|
||||
if (activated.compareAndSet(false, true)) {
|
||||
Tags.ERROR.set(span, Boolean.TRUE);
|
||||
span.setError(true);
|
||||
span.setTag("timeout", event.getAsyncContext().getTimeout());
|
||||
DECORATE.beforeFinish(span);
|
||||
span.finish();
|
||||
|
@ -41,10 +41,11 @@ public class TagSettingAsyncListener implements AsyncListener {
|
|||
@Override
|
||||
public void onError(final AsyncEvent event) throws IOException {
|
||||
if (event.getThrowable() != null && activated.compareAndSet(false, true)) {
|
||||
DECORATE.onResponse(span, (HttpServletResponse) event.getSuppliedResponse());
|
||||
if (((HttpServletResponse) event.getSuppliedResponse()).getStatus()
|
||||
== HttpServletResponse.SC_OK) {
|
||||
// exception is thrown in filter chain, but status code is incorrect
|
||||
Tags.HTTP_STATUS.set(span, 500);
|
||||
span.setTag(Tags.HTTP_STATUS.getKey(), 500);
|
||||
}
|
||||
DECORATE.onError(span, event.getThrowable());
|
||||
DECORATE.beforeFinish(span);
|
||||
|
|
Loading…
Reference in New Issue