HttpClientTracer cleanup: apache-httpclient (#1907)
This commit is contained in:
parent
8235b345eb
commit
a20986c9d9
|
@ -74,7 +74,7 @@ public class ApacheHttpClientInstrumentationModule extends InstrumentationModule
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
context = tracer().startSpan(parentContext, httpMethod, httpMethod);
|
context = tracer().startSpan(parentContext, httpMethod);
|
||||||
scope = context.makeCurrent();
|
scope = context.makeCurrent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,11 +89,7 @@ public class ApacheHttpClientInstrumentationModule extends InstrumentationModule
|
||||||
}
|
}
|
||||||
|
|
||||||
scope.close();
|
scope.close();
|
||||||
if (throwable == null) {
|
tracer().endMaybeExceptionally(context, httpMethod, throwable);
|
||||||
tracer().end(context, httpMethod);
|
|
||||||
} else {
|
|
||||||
tracer().endExceptionally(context, httpMethod, throwable);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,7 +5,8 @@
|
||||||
|
|
||||||
package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v2_0;
|
package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v2_0;
|
||||||
|
|
||||||
import io.opentelemetry.context.propagation.TextMapPropagator.Setter;
|
import io.opentelemetry.context.Context;
|
||||||
|
import io.opentelemetry.context.propagation.TextMapPropagator;
|
||||||
import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer;
|
import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
|
@ -21,6 +22,10 @@ public class CommonsHttpClientTracer extends HttpClientTracer<HttpMethod, HttpMe
|
||||||
return TRACER;
|
return TRACER;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Context startSpan(Context parentContext, HttpMethod request) {
|
||||||
|
return super.startSpan(parentContext, request, request);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String getInstrumentationName() {
|
protected String getInstrumentationName() {
|
||||||
return "io.opentelemetry.javaagent.apache-httpclient";
|
return "io.opentelemetry.javaagent.apache-httpclient";
|
||||||
|
@ -60,7 +65,7 @@ public class CommonsHttpClientTracer extends HttpClientTracer<HttpMethod, HttpMe
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Setter<HttpMethod> getSetter() {
|
protected TextMapPropagator.Setter<HttpMethod> getSetter() {
|
||||||
return HttpHeadersInjectAdapter.SETTER;
|
return HttpHeadersInjectAdapter.SETTER;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,21 +7,18 @@ package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v4_0;
|
||||||
|
|
||||||
import static io.opentelemetry.javaagent.instrumentation.apachehttpclient.v4_0.ApacheHttpClientTracer.tracer;
|
import static io.opentelemetry.javaagent.instrumentation.apachehttpclient.v4_0.ApacheHttpClientTracer.tracer;
|
||||||
|
|
||||||
import io.opentelemetry.api.trace.Span;
|
|
||||||
import io.opentelemetry.context.Context;
|
import io.opentelemetry.context.Context;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
|
|
||||||
public class ApacheHttpClientHelper {
|
public class ApacheHttpClientHelper {
|
||||||
|
|
||||||
public static void doMethodExit(Context context, Object result, Throwable throwable) {
|
public static void doMethodExit(Context context, Object result, Throwable throwable) {
|
||||||
if (result instanceof HttpResponse) {
|
|
||||||
tracer().onResponse(Span.fromContext(context), (HttpResponse) result);
|
|
||||||
} // else they probably provided a ResponseHandler
|
|
||||||
|
|
||||||
if (throwable != null) {
|
if (throwable != null) {
|
||||||
tracer().endExceptionally(context, throwable);
|
tracer().endExceptionally(context, throwable);
|
||||||
|
} else if (result instanceof HttpResponse) {
|
||||||
|
tracer().end(context, (HttpResponse) result);
|
||||||
} else {
|
} else {
|
||||||
tracer().end(context);
|
// ended in WrappingStatusSettingResponseHandler
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -157,7 +157,7 @@ public class ApacheHttpClientInstrumentationModule extends InstrumentationModule
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
context = tracer().startSpan(parentContext, request, request);
|
context = tracer().startSpan(parentContext, request);
|
||||||
scope = context.makeCurrent();
|
scope = context.makeCurrent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -194,12 +194,12 @@ public class ApacheHttpClientInstrumentationModule extends InstrumentationModule
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
context = tracer().startSpan(parentContext, request, request);
|
context = tracer().startSpan(parentContext, request);
|
||||||
scope = context.makeCurrent();
|
scope = context.makeCurrent();
|
||||||
|
|
||||||
// Wrap the handler so we capture the status code
|
// Wrap the handler so we capture the status code
|
||||||
if (handler instanceof ResponseHandler) {
|
if (handler instanceof ResponseHandler) {
|
||||||
handler = new WrappingStatusSettingResponseHandler(context, (ResponseHandler) handler);
|
handler = new WrappingStatusSettingResponseHandler(context, (ResponseHandler<?>) handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -230,13 +230,7 @@ public class ApacheHttpClientInstrumentationModule extends InstrumentationModule
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpUriRequest httpUriRequest;
|
context = tracer().startSpan(parentContext, host, request);
|
||||||
if (request instanceof HttpUriRequest) {
|
|
||||||
httpUriRequest = (HttpUriRequest) request;
|
|
||||||
} else {
|
|
||||||
httpUriRequest = new HostAndRequestAsHttpUriRequest(host, request);
|
|
||||||
}
|
|
||||||
context = tracer().startSpan(parentContext, httpUriRequest, httpUriRequest);
|
|
||||||
scope = context.makeCurrent();
|
scope = context.makeCurrent();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -274,18 +268,12 @@ public class ApacheHttpClientInstrumentationModule extends InstrumentationModule
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
HttpUriRequest httpUriRequest;
|
context = tracer().startSpan(parentContext, host, request);
|
||||||
if (request instanceof HttpUriRequest) {
|
|
||||||
httpUriRequest = (HttpUriRequest) request;
|
|
||||||
} else {
|
|
||||||
httpUriRequest = new HostAndRequestAsHttpUriRequest(host, request);
|
|
||||||
}
|
|
||||||
context = tracer().startSpan(parentContext, httpUriRequest, httpUriRequest);
|
|
||||||
scope = context.makeCurrent();
|
scope = context.makeCurrent();
|
||||||
|
|
||||||
// Wrap the handler so we capture the status code
|
// Wrap the handler so we capture the status code
|
||||||
if (handler instanceof ResponseHandler) {
|
if (handler instanceof ResponseHandler) {
|
||||||
handler = new WrappingStatusSettingResponseHandler(context, (ResponseHandler) handler);
|
handler = new WrappingStatusSettingResponseHandler(context, (ResponseHandler<?>) handler);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -7,12 +7,14 @@ package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v4_0;
|
||||||
|
|
||||||
import static io.opentelemetry.javaagent.instrumentation.apachehttpclient.v4_0.HttpHeadersInjectAdapter.SETTER;
|
import static io.opentelemetry.javaagent.instrumentation.apachehttpclient.v4_0.HttpHeadersInjectAdapter.SETTER;
|
||||||
|
|
||||||
import io.opentelemetry.api.trace.Span;
|
import io.opentelemetry.context.Context;
|
||||||
import io.opentelemetry.context.propagation.TextMapPropagator.Setter;
|
import io.opentelemetry.context.propagation.TextMapPropagator;
|
||||||
import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer;
|
import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import org.apache.http.Header;
|
import org.apache.http.Header;
|
||||||
|
import org.apache.http.HttpHost;
|
||||||
import org.apache.http.HttpMessage;
|
import org.apache.http.HttpMessage;
|
||||||
|
import org.apache.http.HttpRequest;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.methods.HttpUriRequest;
|
import org.apache.http.client.methods.HttpUriRequest;
|
||||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||||
|
@ -26,6 +28,20 @@ public class ApacheHttpClientTracer
|
||||||
return TRACER;
|
return TRACER;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Context startSpan(Context parentContext, HttpHost host, HttpRequest request) {
|
||||||
|
HttpUriRequest httpUriRequest;
|
||||||
|
if (request instanceof HttpUriRequest) {
|
||||||
|
httpUriRequest = (HttpUriRequest) request;
|
||||||
|
} else {
|
||||||
|
httpUriRequest = new HostAndRequestAsHttpUriRequest(host, request);
|
||||||
|
}
|
||||||
|
return startSpan(parentContext, httpUriRequest);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Context startSpan(Context parentContext, HttpUriRequest request) {
|
||||||
|
return startSpan(parentContext, request, request);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected String method(HttpUriRequest httpRequest) {
|
protected String method(HttpUriRequest httpRequest) {
|
||||||
return httpRequest.getMethod();
|
return httpRequest.getMethod();
|
||||||
|
@ -57,7 +73,7 @@ public class ApacheHttpClientTracer
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected Setter<HttpUriRequest> getSetter() {
|
protected TextMapPropagator.Setter<HttpUriRequest> getSetter() {
|
||||||
return SETTER;
|
return SETTER;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -70,10 +86,4 @@ public class ApacheHttpClientTracer
|
||||||
protected String getInstrumentationName() {
|
protected String getInstrumentationName() {
|
||||||
return "io.opentelemetry.javaagent.apache-httpclient";
|
return "io.opentelemetry.javaagent.apache-httpclient";
|
||||||
}
|
}
|
||||||
|
|
||||||
/** This method is overridden to allow other classes in this package to call it. */
|
|
||||||
@Override
|
|
||||||
protected void onResponse(Span span, HttpResponse httpResponse) {
|
|
||||||
super.onResponse(span, httpResponse);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,27 +0,0 @@
|
||||||
/*
|
|
||||||
* Copyright The OpenTelemetry Authors
|
|
||||||
* SPDX-License-Identifier: Apache-2.0
|
|
||||||
*/
|
|
||||||
|
|
||||||
package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v4_0;
|
|
||||||
|
|
||||||
import io.opentelemetry.context.Context;
|
|
||||||
import io.opentelemetry.context.Scope;
|
|
||||||
|
|
||||||
public class ContextScopePair {
|
|
||||||
private final Context context;
|
|
||||||
private final Scope scope;
|
|
||||||
|
|
||||||
public ContextScopePair(Context context, Scope scope) {
|
|
||||||
this.context = context;
|
|
||||||
this.scope = scope;
|
|
||||||
}
|
|
||||||
|
|
||||||
public Context getContext() {
|
|
||||||
return context;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void closeScope() {
|
|
||||||
scope.close();
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -7,26 +7,23 @@ package io.opentelemetry.javaagent.instrumentation.apachehttpclient.v4_0;
|
||||||
|
|
||||||
import static io.opentelemetry.javaagent.instrumentation.apachehttpclient.v4_0.ApacheHttpClientTracer.tracer;
|
import static io.opentelemetry.javaagent.instrumentation.apachehttpclient.v4_0.ApacheHttpClientTracer.tracer;
|
||||||
|
|
||||||
import io.opentelemetry.api.trace.Span;
|
|
||||||
import io.opentelemetry.context.Context;
|
import io.opentelemetry.context.Context;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import org.apache.http.HttpResponse;
|
import org.apache.http.HttpResponse;
|
||||||
import org.apache.http.client.ResponseHandler;
|
import org.apache.http.client.ResponseHandler;
|
||||||
|
|
||||||
public class WrappingStatusSettingResponseHandler implements ResponseHandler {
|
public class WrappingStatusSettingResponseHandler<T> implements ResponseHandler<T> {
|
||||||
final Context context;
|
final Context context;
|
||||||
final ResponseHandler handler;
|
final ResponseHandler<T> handler;
|
||||||
|
|
||||||
public WrappingStatusSettingResponseHandler(Context context, ResponseHandler handler) {
|
public WrappingStatusSettingResponseHandler(Context context, ResponseHandler<T> handler) {
|
||||||
this.context = context;
|
this.context = context;
|
||||||
this.handler = handler;
|
this.handler = handler;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object handleResponse(HttpResponse response) throws IOException {
|
public T handleResponse(HttpResponse response) throws IOException {
|
||||||
if (context != null) {
|
tracer().end(context, response);
|
||||||
tracer().onResponse(Span.fromContext(context), response);
|
|
||||||
}
|
|
||||||
return handler.handleResponse(response);
|
return handler.handleResponse(response);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue