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