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();
|
||||
}
|
||||
|
||||
// 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) {
|
||||
return Context.current().with(span).makeCurrent();
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ public abstract class HttpClientTracer<REQUEST, CARRIER, RESPONSE> extends BaseT
|
|||
}
|
||||
|
||||
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) {
|
||||
|
@ -71,14 +71,18 @@ public abstract class HttpClientTracer<REQUEST, CARRIER, RESPONSE> extends BaseT
|
|||
Context parentContext, REQUEST request, CARRIER carrier, long startTimeNanos) {
|
||||
Span span =
|
||||
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();
|
||||
if (setter == null) {
|
||||
throw new IllegalStateException(
|
||||
"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);
|
||||
return context;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
// 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(
|
||||
Context parentContext, REQUEST request, String name, long startTimeNanos) {
|
||||
SpanBuilder spanBuilder =
|
||||
|
@ -124,7 +140,7 @@ public abstract class HttpClientTracer<REQUEST, CARRIER, RESPONSE> extends BaseT
|
|||
return span;
|
||||
}
|
||||
|
||||
protected Span onRequest(Span span, REQUEST request) {
|
||||
protected void onRequest(Span span, REQUEST request) {
|
||||
assert span != null;
|
||||
if (request != null) {
|
||||
span.setAttribute(SemanticAttributes.NET_TRANSPORT, "IP.TCP");
|
||||
|
@ -134,7 +150,6 @@ public abstract class HttpClientTracer<REQUEST, CARRIER, RESPONSE> extends BaseT
|
|||
setFlavor(span, request);
|
||||
setUrl(span, request);
|
||||
}
|
||||
return span;
|
||||
}
|
||||
|
||||
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;
|
||||
if (response != null) {
|
||||
Integer status = status(response);
|
||||
|
@ -172,7 +187,6 @@ public abstract class HttpClientTracer<REQUEST, CARRIER, RESPONSE> extends BaseT
|
|||
span.setStatus(HttpStatusConverter.statusFromHttpStatus(status));
|
||||
}
|
||||
}
|
||||
return span;
|
||||
}
|
||||
|
||||
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. */
|
||||
@Override
|
||||
public Span onRequest(Span span, HttpRequest httpRequest) {
|
||||
return super.onRequest(span, httpRequest);
|
||||
protected void onRequest(Span span, HttpRequest 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. */
|
||||
@Override
|
||||
protected Span onResponse(Span span, HttpResponse httpResponse) {
|
||||
return super.onResponse(span, httpResponse);
|
||||
protected void onResponse(Span span, HttpResponse httpResponse) {
|
||||
super.onResponse(span, httpResponse);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -64,12 +64,12 @@ public class AwsSdkClientTracer extends HttpClientTracer<Request<?>, Request<?>,
|
|||
}
|
||||
|
||||
@Override
|
||||
public Span onResponse(Span span, Response<?> response) {
|
||||
public void onResponse(Span span, Response<?> response) {
|
||||
if (response != null && response.getAwsResponse() instanceof AmazonWebServiceResponse) {
|
||||
AmazonWebServiceResponse awsResp = (AmazonWebServiceResponse) response.getAwsResponse();
|
||||
span.setAttribute("aws.requestId", awsResp.getRequestId());
|
||||
}
|
||||
return super.onResponse(span, response);
|
||||
super.onResponse(span, response);
|
||||
}
|
||||
|
||||
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. */
|
||||
@Override
|
||||
protected Span onRequest(Span span, SdkHttpRequest sdkHttpRequest) {
|
||||
return super.onRequest(span, sdkHttpRequest);
|
||||
protected void onRequest(Span span, SdkHttpRequest sdkHttpRequest) {
|
||||
super.onRequest(span, sdkHttpRequest);
|
||||
}
|
||||
|
||||
public Context startSpan(Context parentContext, String name, Tracer tracer, Kind kind) {
|
||||
|
|
|
@ -61,16 +61,14 @@ public class JdkHttpClientTracer
|
|||
}
|
||||
|
||||
@Override
|
||||
protected Span onResponse(Span span, HttpResponse<?> httpResponse) {
|
||||
span = super.onResponse(span, httpResponse);
|
||||
protected void onResponse(Span span, HttpResponse<?> httpResponse) {
|
||||
super.onResponse(span, httpResponse);
|
||||
|
||||
if (httpResponse != null) {
|
||||
span.setAttribute(
|
||||
SemanticAttributes.HTTP_FLAVOR,
|
||||
httpResponse.version() == Version.HTTP_1_1 ? "1.1" : "2.0");
|
||||
}
|
||||
|
||||
return span;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -45,10 +45,6 @@ public class KHttpAdvice {
|
|||
}
|
||||
|
||||
scope.close();
|
||||
if (throwable == null) {
|
||||
tracer().end(context, response);
|
||||
} else {
|
||||
tracer().endExceptionally(context, response, throwable);
|
||||
}
|
||||
tracer().endMaybeExceptionally(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. */
|
||||
@Override
|
||||
protected Span onRequest(Span span, Request request) {
|
||||
return super.onRequest(span, request);
|
||||
protected void onRequest(Span span, Request request) {
|
||||
super.onRequest(span, request);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue