HTTP client instrumentation cleanup, part 0 (#1893)
This commit is contained in:
parent
c800c0dc87
commit
e2264222ce
|
@ -67,6 +67,16 @@ public abstract class BaseTracer {
|
||||||
return tracer.spanBuilder(spanName).setSpanKind(kind).startSpan();
|
return tracer.spanBuilder(spanName).setSpanKind(kind).startSpan();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO (trask) make the key private
|
||||||
|
protected final boolean inClientSpan(Context parentContext) {
|
||||||
|
return parentContext.get(CONTEXT_CLIENT_SPAN_KEY) != null;
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO (trask) make the key private
|
||||||
|
protected final Context withClientSpan(Context parentContext, Span span) {
|
||||||
|
return parentContext.with(span).with(CONTEXT_CLIENT_SPAN_KEY, span);
|
||||||
|
}
|
||||||
|
|
||||||
public Scope startScope(Span span) {
|
public Scope startScope(Span span) {
|
||||||
return Context.current().with(span).makeCurrent();
|
return Context.current().with(span).makeCurrent();
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,7 +60,7 @@ public abstract class HttpClientTracer<REQUEST, CARRIER, RESPONSE> extends BaseT
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean shouldStartSpan(Context parentContext) {
|
public boolean shouldStartSpan(Context parentContext) {
|
||||||
return parentContext.get(CONTEXT_CLIENT_SPAN_KEY) == null;
|
return !inClientSpan(parentContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Context startSpan(Context parentContext, REQUEST request, CARRIER carrier) {
|
public Context startSpan(Context parentContext, REQUEST request, CARRIER carrier) {
|
||||||
|
@ -71,14 +71,18 @@ public abstract class HttpClientTracer<REQUEST, CARRIER, RESPONSE> extends BaseT
|
||||||
Context parentContext, REQUEST request, CARRIER carrier, long startTimeNanos) {
|
Context parentContext, REQUEST request, CARRIER carrier, long startTimeNanos) {
|
||||||
Span span =
|
Span span =
|
||||||
internalStartSpan(parentContext, request, spanNameForRequest(request), startTimeNanos);
|
internalStartSpan(parentContext, request, spanNameForRequest(request), startTimeNanos);
|
||||||
|
Context context = withClientSpan(parentContext, span);
|
||||||
|
inject(context, carrier);
|
||||||
|
return context;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void inject(Context context, CARRIER carrier) {
|
||||||
Setter<CARRIER> setter = getSetter();
|
Setter<CARRIER> setter = getSetter();
|
||||||
if (setter == null) {
|
if (setter == null) {
|
||||||
throw new IllegalStateException(
|
throw new IllegalStateException(
|
||||||
"getSetter() not defined but calling startScope(), either getSetter must be implemented or the scope should be setup manually");
|
"getSetter() not defined but calling startScope(), either getSetter must be implemented or the scope should be setup manually");
|
||||||
}
|
}
|
||||||
Context context = parentContext.with(span).with(CONTEXT_CLIENT_SPAN_KEY, span);
|
|
||||||
OpenTelemetry.getGlobalPropagators().getTextMapPropagator().inject(context, carrier, setter);
|
OpenTelemetry.getGlobalPropagators().getTextMapPropagator().inject(context, carrier, setter);
|
||||||
return context;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void end(Context context, RESPONSE response) {
|
public void end(Context context, RESPONSE response) {
|
||||||
|
@ -112,6 +116,18 @@ public abstract class HttpClientTracer<REQUEST, CARRIER, RESPONSE> extends BaseT
|
||||||
super.endExceptionally(span, throwable, -1);
|
super.endExceptionally(span, throwable, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO (trask) see if we can reduce the number of end..() variants
|
||||||
|
// see https://github.com/open-telemetry/opentelemetry-java-instrumentation
|
||||||
|
// /pull/1893#discussion_r542111699
|
||||||
|
public void endMaybeExceptionally(
|
||||||
|
Context context, RESPONSE response, @Nullable Throwable throwable) {
|
||||||
|
if (throwable != null) {
|
||||||
|
endExceptionally(context, throwable);
|
||||||
|
} else {
|
||||||
|
end(context, response);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private Span internalStartSpan(
|
private Span internalStartSpan(
|
||||||
Context parentContext, REQUEST request, String name, long startTimeNanos) {
|
Context parentContext, REQUEST request, String name, long startTimeNanos) {
|
||||||
SpanBuilder spanBuilder =
|
SpanBuilder spanBuilder =
|
||||||
|
@ -124,7 +140,7 @@ public abstract class HttpClientTracer<REQUEST, CARRIER, RESPONSE> extends BaseT
|
||||||
return span;
|
return span;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Span onRequest(Span span, REQUEST request) {
|
protected void onRequest(Span span, REQUEST request) {
|
||||||
assert span != null;
|
assert span != null;
|
||||||
if (request != null) {
|
if (request != null) {
|
||||||
span.setAttribute(SemanticAttributes.NET_TRANSPORT, "IP.TCP");
|
span.setAttribute(SemanticAttributes.NET_TRANSPORT, "IP.TCP");
|
||||||
|
@ -134,7 +150,6 @@ public abstract class HttpClientTracer<REQUEST, CARRIER, RESPONSE> extends BaseT
|
||||||
setFlavor(span, request);
|
setFlavor(span, request);
|
||||||
setUrl(span, request);
|
setUrl(span, request);
|
||||||
}
|
}
|
||||||
return span;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setFlavor(Span span, REQUEST request) {
|
private void setFlavor(Span span, REQUEST request) {
|
||||||
|
@ -163,7 +178,7 @@ public abstract class HttpClientTracer<REQUEST, CARRIER, RESPONSE> extends BaseT
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Span onResponse(Span span, RESPONSE response) {
|
protected void onResponse(Span span, RESPONSE response) {
|
||||||
assert span != null;
|
assert span != null;
|
||||||
if (response != null) {
|
if (response != null) {
|
||||||
Integer status = status(response);
|
Integer status = status(response);
|
||||||
|
@ -172,7 +187,6 @@ public abstract class HttpClientTracer<REQUEST, CARRIER, RESPONSE> extends BaseT
|
||||||
span.setStatus(HttpStatusConverter.statusFromHttpStatus(status));
|
span.setStatus(HttpStatusConverter.statusFromHttpStatus(status));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return span;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String spanNameForRequest(REQUEST request) {
|
protected String spanNameForRequest(REQUEST request) {
|
||||||
|
|
|
@ -110,7 +110,7 @@ public class ApacheHttpAsyncClientTracer
|
||||||
|
|
||||||
/** This method is overridden to allow other classes in this package to call it. */
|
/** This method is overridden to allow other classes in this package to call it. */
|
||||||
@Override
|
@Override
|
||||||
public Span onRequest(Span span, HttpRequest httpRequest) {
|
protected void onRequest(Span span, HttpRequest httpRequest) {
|
||||||
return super.onRequest(span, httpRequest);
|
super.onRequest(span, httpRequest);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -73,7 +73,7 @@ public class ApacheHttpClientTracer
|
||||||
|
|
||||||
/** This method is overridden to allow other classes in this package to call it. */
|
/** This method is overridden to allow other classes in this package to call it. */
|
||||||
@Override
|
@Override
|
||||||
protected Span onResponse(Span span, HttpResponse httpResponse) {
|
protected void onResponse(Span span, HttpResponse httpResponse) {
|
||||||
return super.onResponse(span, httpResponse);
|
super.onResponse(span, httpResponse);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,12 +64,12 @@ public class AwsSdkClientTracer extends HttpClientTracer<Request<?>, Request<?>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Span onResponse(Span span, Response<?> response) {
|
public void onResponse(Span span, Response<?> response) {
|
||||||
if (response != null && response.getAwsResponse() instanceof AmazonWebServiceResponse) {
|
if (response != null && response.getAwsResponse() instanceof AmazonWebServiceResponse) {
|
||||||
AmazonWebServiceResponse awsResp = (AmazonWebServiceResponse) response.getAwsResponse();
|
AmazonWebServiceResponse awsResp = (AmazonWebServiceResponse) response.getAwsResponse();
|
||||||
span.setAttribute("aws.requestId", awsResp.getRequestId());
|
span.setAttribute("aws.requestId", awsResp.getRequestId());
|
||||||
}
|
}
|
||||||
return super.onResponse(span, response);
|
super.onResponse(span, response);
|
||||||
}
|
}
|
||||||
|
|
||||||
private String qualifiedOperation(String service, Class<?> operation) {
|
private String qualifiedOperation(String service, Class<?> operation) {
|
||||||
|
|
|
@ -74,8 +74,8 @@ final class AwsSdkHttpClientTracer
|
||||||
|
|
||||||
/** This method is overridden to allow other classes in this package to call it. */
|
/** This method is overridden to allow other classes in this package to call it. */
|
||||||
@Override
|
@Override
|
||||||
protected Span onRequest(Span span, SdkHttpRequest sdkHttpRequest) {
|
protected void onRequest(Span span, SdkHttpRequest sdkHttpRequest) {
|
||||||
return super.onRequest(span, sdkHttpRequest);
|
super.onRequest(span, sdkHttpRequest);
|
||||||
}
|
}
|
||||||
|
|
||||||
public Context startSpan(Context parentContext, String name, Tracer tracer, Kind kind) {
|
public Context startSpan(Context parentContext, String name, Tracer tracer, Kind kind) {
|
||||||
|
|
|
@ -61,16 +61,14 @@ public class JdkHttpClientTracer
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Span onResponse(Span span, HttpResponse<?> httpResponse) {
|
protected void onResponse(Span span, HttpResponse<?> httpResponse) {
|
||||||
span = super.onResponse(span, httpResponse);
|
super.onResponse(span, httpResponse);
|
||||||
|
|
||||||
if (httpResponse != null) {
|
if (httpResponse != null) {
|
||||||
span.setAttribute(
|
span.setAttribute(
|
||||||
SemanticAttributes.HTTP_FLAVOR,
|
SemanticAttributes.HTTP_FLAVOR,
|
||||||
httpResponse.version() == Version.HTTP_1_1 ? "1.1" : "2.0");
|
httpResponse.version() == Version.HTTP_1_1 ? "1.1" : "2.0");
|
||||||
}
|
}
|
||||||
|
|
||||||
return span;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -45,10 +45,6 @@ public class KHttpAdvice {
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.close();
|
scope.close();
|
||||||
if (throwable == null) {
|
tracer().endMaybeExceptionally(context, response, throwable);
|
||||||
tracer().end(context, response);
|
|
||||||
} else {
|
|
||||||
tracer().endExceptionally(context, response, throwable);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -82,7 +82,7 @@ public class KubernetesClientTracer extends HttpClientTracer<Request, Request, R
|
||||||
|
|
||||||
/** This method is overridden to allow other classes in this package to call it. */
|
/** This method is overridden to allow other classes in this package to call it. */
|
||||||
@Override
|
@Override
|
||||||
protected Span onRequest(Span span, Request request) {
|
protected void onRequest(Span span, Request request) {
|
||||||
return super.onRequest(span, request);
|
super.onRequest(span, request);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue