Update google-http-client to new agent api
This commit is contained in:
parent
9775a94132
commit
120937d175
|
@ -1,7 +1,10 @@
|
||||||
package datadog.trace.instrumentation.googlehttpclient;
|
package datadog.trace.instrumentation.googlehttpclient;
|
||||||
|
|
||||||
|
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.googlehttpclient.GoogleHttpClientDecorator.DECORATE;
|
import static datadog.trace.instrumentation.googlehttpclient.GoogleHttpClientDecorator.DECORATE;
|
||||||
import static java.util.Collections.singletonMap;
|
import static datadog.trace.instrumentation.googlehttpclient.HeadersInjectAdapter.SETTER;
|
||||||
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
|
import static net.bytebuddy.matcher.ElementMatchers.isMethod;
|
||||||
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
|
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
|
||||||
import static net.bytebuddy.matcher.ElementMatchers.named;
|
import static net.bytebuddy.matcher.ElementMatchers.named;
|
||||||
|
@ -14,16 +17,10 @@ import com.google.auto.service.AutoService;
|
||||||
import datadog.trace.agent.tooling.Instrumenter;
|
import datadog.trace.agent.tooling.Instrumenter;
|
||||||
import datadog.trace.bootstrap.ContextStore;
|
import datadog.trace.bootstrap.ContextStore;
|
||||||
import datadog.trace.bootstrap.InstrumentationContext;
|
import datadog.trace.bootstrap.InstrumentationContext;
|
||||||
import io.opentracing.Scope;
|
import datadog.trace.instrumentation.api.AgentScope;
|
||||||
import io.opentracing.Span;
|
import datadog.trace.instrumentation.api.AgentSpan;
|
||||||
import io.opentracing.log.Fields;
|
|
||||||
import io.opentracing.propagation.Format;
|
|
||||||
import io.opentracing.propagation.TextMap;
|
|
||||||
import io.opentracing.tag.Tags;
|
|
||||||
import io.opentracing.util.GlobalTracer;
|
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Iterator;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import net.bytebuddy.asm.Advice;
|
import net.bytebuddy.asm.Advice;
|
||||||
import net.bytebuddy.description.method.MethodDescription;
|
import net.bytebuddy.description.method.MethodDescription;
|
||||||
|
@ -58,7 +55,7 @@ public class GoogleHttpClientInstrumentation extends Instrumenter.Default {
|
||||||
packageName + ".RequestState",
|
packageName + ".RequestState",
|
||||||
getClass().getName() + "$GoogleHttpClientAdvice",
|
getClass().getName() + "$GoogleHttpClientAdvice",
|
||||||
getClass().getName() + "$GoogleHttpClientAsyncAdvice",
|
getClass().getName() + "$GoogleHttpClientAsyncAdvice",
|
||||||
getClass().getName() + "$HeadersInjectAdapter"
|
packageName + ".HeadersInjectAdapter"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,17 +87,16 @@ public class GoogleHttpClientInstrumentation extends Instrumenter.Default {
|
||||||
RequestState state = contextStore.get(request);
|
RequestState state = contextStore.get(request);
|
||||||
|
|
||||||
if (state == null) {
|
if (state == null) {
|
||||||
state = new RequestState(GlobalTracer.get().buildSpan("http.request").start());
|
state = new RequestState(startSpan("http.request"));
|
||||||
contextStore.put(request, state);
|
contextStore.put(request, state);
|
||||||
}
|
}
|
||||||
|
|
||||||
final Span span = state.getSpan();
|
final AgentSpan span = state.getSpan();
|
||||||
|
|
||||||
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
|
try (final AgentScope scope = activateSpan(span, false)) {
|
||||||
DECORATE.afterStart(span);
|
DECORATE.afterStart(span);
|
||||||
DECORATE.onRequest(span, request);
|
DECORATE.onRequest(span, request);
|
||||||
GlobalTracer.get()
|
propagate().inject(span, request, SETTER);
|
||||||
.inject(span.context(), Format.Builtin.HTTP_HEADERS, new HeadersInjectAdapter(request));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -115,17 +111,17 @@ public class GoogleHttpClientInstrumentation extends Instrumenter.Default {
|
||||||
final RequestState state = contextStore.get(request);
|
final RequestState state = contextStore.get(request);
|
||||||
|
|
||||||
if (state != null) {
|
if (state != null) {
|
||||||
final Span span = state.getSpan();
|
final AgentSpan span = state.getSpan();
|
||||||
|
|
||||||
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
|
try (final AgentScope scope = activateSpan(span, false)) {
|
||||||
DECORATE.onResponse(span, response);
|
DECORATE.onResponse(span, response);
|
||||||
DECORATE.onError(span, throwable);
|
DECORATE.onError(span, throwable);
|
||||||
|
|
||||||
// If HttpRequest.setThrowExceptionOnExecuteError is set to false, there are no exceptions
|
// If HttpRequest.setThrowExceptionOnExecuteError is set to false, there are no exceptions
|
||||||
// for a failed request. Thus, check the response code
|
// for a failed request. Thus, check the response code
|
||||||
if (response != null && !response.isSuccessStatusCode()) {
|
if (response != null && !response.isSuccessStatusCode()) {
|
||||||
Tags.ERROR.set(span, true);
|
span.setError(true);
|
||||||
span.log(singletonMap(Fields.MESSAGE, response.getStatusMessage()));
|
span.setErrorMessage(response.getStatusMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
DECORATE.beforeFinish(span);
|
DECORATE.beforeFinish(span);
|
||||||
|
@ -139,7 +135,7 @@ public class GoogleHttpClientInstrumentation extends Instrumenter.Default {
|
||||||
|
|
||||||
@Advice.OnMethodEnter(suppress = Throwable.class)
|
@Advice.OnMethodEnter(suppress = Throwable.class)
|
||||||
public static void methodEnter(@Advice.This final HttpRequest request) {
|
public static void methodEnter(@Advice.This final HttpRequest request) {
|
||||||
final Span span = GlobalTracer.get().buildSpan("http.request").start();
|
final AgentSpan span = startSpan("http.request");
|
||||||
|
|
||||||
final ContextStore<HttpRequest, RequestState> contextStore =
|
final ContextStore<HttpRequest, RequestState> contextStore =
|
||||||
InstrumentationContext.get(HttpRequest.class, RequestState.class);
|
InstrumentationContext.get(HttpRequest.class, RequestState.class);
|
||||||
|
@ -159,9 +155,9 @@ public class GoogleHttpClientInstrumentation extends Instrumenter.Default {
|
||||||
final RequestState state = contextStore.get(request);
|
final RequestState state = contextStore.get(request);
|
||||||
|
|
||||||
if (state != null) {
|
if (state != null) {
|
||||||
final Span span = state.getSpan();
|
final AgentSpan span = state.getSpan();
|
||||||
|
|
||||||
try (final Scope scope = GlobalTracer.get().scopeManager().activate(span, false)) {
|
try (final AgentScope scope = activateSpan(span, false)) {
|
||||||
DECORATE.onError(span, throwable);
|
DECORATE.onError(span, throwable);
|
||||||
|
|
||||||
DECORATE.beforeFinish(span);
|
DECORATE.beforeFinish(span);
|
||||||
|
@ -171,23 +167,4 @@ public class GoogleHttpClientInstrumentation extends Instrumenter.Default {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static class HeadersInjectAdapter implements TextMap {
|
|
||||||
private final HttpRequest request;
|
|
||||||
|
|
||||||
public HeadersInjectAdapter(final HttpRequest request) {
|
|
||||||
this.request = request;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public Iterator<Map.Entry<String, String>> iterator() {
|
|
||||||
throw new UnsupportedOperationException(
|
|
||||||
"This class should be used only with tracer#inject()");
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void put(final String key, final String value) {
|
|
||||||
request.getHeaders().put(key, value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,14 @@
|
||||||
|
package datadog.trace.instrumentation.googlehttpclient;
|
||||||
|
|
||||||
|
import com.google.api.client.http.HttpRequest;
|
||||||
|
import datadog.trace.instrumentation.api.AgentPropagation;
|
||||||
|
|
||||||
|
public class HeadersInjectAdapter implements AgentPropagation.Setter<HttpRequest> {
|
||||||
|
|
||||||
|
public static final HeadersInjectAdapter SETTER = new HeadersInjectAdapter();
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void set(final HttpRequest carrier, final String key, final String value) {
|
||||||
|
carrier.getHeaders().put(key, value);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,10 +1,10 @@
|
||||||
package datadog.trace.instrumentation.googlehttpclient;
|
package datadog.trace.instrumentation.googlehttpclient;
|
||||||
|
|
||||||
import io.opentracing.Span;
|
import datadog.trace.instrumentation.api.AgentSpan;
|
||||||
import lombok.Data;
|
import lombok.Data;
|
||||||
import lombok.NonNull;
|
import lombok.NonNull;
|
||||||
|
|
||||||
@Data
|
@Data
|
||||||
public class RequestState {
|
public class RequestState {
|
||||||
@NonNull public Span span;
|
@NonNull public AgentSpan span;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue