Update async-http-client-1.9 to Instrumenter API
RENAME
instrumentation/async-http-client/async-http-client-1.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_9/Contexts.java
instrumentation/async-http-client/async-http-client-1.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_9/AsyncHandlerData.java
package io.opentelemetry.javaagent.instrumentation.asynchttpclient.v1_9;
import com.google.auto.value.AutoValue;
+import com.ning.http.client.Request;
import io.opentelemetry.context.Context;
@AutoValue
-public abstract class Contexts {
+public abstract class AsyncHandlerData {
- public static Contexts create(Context parentContext, Context context) {
- return new AutoValue_Contexts(parentContext, context);
+ public static AsyncHandlerData create(Context parentContext, Context context, Request request) {
+ return new AutoValue_AsyncHandlerData(parentContext, context, request);
}
public abstract Context getParentContext();
public abstract Context getContext();
- Contexts() {}
+ public abstract Request getRequest();
}

ADD
instrumentation/async-http-client/async-http-client-1.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_9/AsyncHttpClientHttpAttributesExtractor.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.asynchttpclient.v1_9;
+
+import com.ning.http.client.Request;
+import com.ning.http.client.Response;
+import com.ning.http.client.uri.Uri;
+import io.opentelemetry.instrumentation.api.instrumenter.http.HttpAttributesExtractor;
+import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+final class AsyncHttpClientHttpAttributesExtractor
+ extends HttpAttributesExtractor<Request, Response> {
+
+ @Override
+ protected String method(Request request) {
+ return request.getMethod();
+ }
+
+ @Override
+ protected String url(Request request) {
+ return request.getUri().toUrl();
+ }
+
+ @Override
+ protected String target(Request request) {
+ Uri uri = request.getUri();
+ String query = uri.getQuery();
+ return query != null ? uri.getPath() + "?" + query : uri.getPath();
+ }
+
+ @Override
+ @Nullable
+ protected String host(Request request) {
+ String host = request.getHeaders().getFirstValue("Host");
+ if (host != null) {
+ return host;
+ }
+ return request.getVirtualHost();
+ }
+
+ @Override
+ @Nullable
+ protected String scheme(Request request) {
+ return request.getUri().getScheme();
+ }
+
+ @Override
+ @Nullable
+ protected String userAgent(Request request) {
+ return null;
+ }
+
+ @Override
+ @Nullable
+ protected Long requestContentLength(Request request, @Nullable Response response) {
+ return null;
+ }
+
+ @Override
+ @Nullable
+ protected Long requestContentLengthUncompressed(Request request, @Nullable Response response) {
+ return null;
+ }
+
+ @Override
+ protected Integer statusCode(Request request, Response response) {
+ return response.getStatusCode();
+ }
+
+ @Override
+ protected String flavor(Request request, @Nullable Response response) {
+ return SemanticAttributes.HttpFlavorValues.HTTP_1_1;
+ }
+
+ @Override
+ @Nullable
+ protected Long responseContentLength(Request request, Response response) {
+ return null;
+ }
+
+ @Override
+ @Nullable
+ protected Long responseContentLengthUncompressed(Request request, Response response) {
+ return null;
+ }
+
+ @Override
+ @Nullable
+ protected String serverName(Request request, @Nullable Response response) {
+ return null;
+ }
+
+ @Override
+ @Nullable
+ protected String route(Request request) {
+ return null;
+ }
+}

ADD
instrumentation/async-http-client/async-http-client-1.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_9/AsyncHttpClientNetAttributesExtractor.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.asynchttpclient.v1_9;
+
+import com.ning.http.client.Request;
+import com.ning.http.client.Response;
+import io.opentelemetry.instrumentation.api.instrumenter.net.NetAttributesExtractor;
+import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
+import org.checkerframework.checker.nullness.qual.Nullable;
+
+final class AsyncHttpClientNetAttributesExtractor
+ extends NetAttributesExtractor<Request, Response> {
+
+ @Override
+ public String transport(Request request) {
+ return SemanticAttributes.NetTransportValues.IP_TCP;
+ }
+
+ @Override
+ public String peerName(Request request, @Nullable Response response) {
+ return request.getUri().getHost();
+ }
+
+ @Override
+ public Integer peerPort(Request request, @Nullable Response response) {
+ return request.getUri().getPort();
+ }
+
+ @Override
+ @Nullable
+ public String peerIp(Request request, @Nullable Response response) {
+ return null;
+ }
+}

ADD
instrumentation/async-http-client/async-http-client-1.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_9/AsyncHttpClientSingletons.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.asynchttpclient.v1_9;
+
+import com.ning.http.client.Request;
+import com.ning.http.client.Response;
+import io.opentelemetry.api.GlobalOpenTelemetry;
+import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
+import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
+import io.opentelemetry.instrumentation.api.instrumenter.SpanStatusExtractor;
+import io.opentelemetry.instrumentation.api.instrumenter.http.HttpAttributesExtractor;
+import io.opentelemetry.instrumentation.api.instrumenter.http.HttpClientMetrics;
+import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanNameExtractor;
+import io.opentelemetry.instrumentation.api.instrumenter.http.HttpSpanStatusExtractor;
+import io.opentelemetry.javaagent.instrumentation.api.instrumenter.PeerServiceAttributesExtractor;
+
+public final class AsyncHttpClientSingletons {
+ private static final String INSTRUMENTATION_NAME = "io.opentelemetry.async-http-client-1.9";
+
+ private static final Instrumenter<Request, Response> INSTRUMENTER;
+
+ static {
+ HttpAttributesExtractor<Request, Response> httpAttributesExtractor =
+ new AsyncHttpClientHttpAttributesExtractor();
+ SpanNameExtractor<? super Request> spanNameExtractor =
+ HttpSpanNameExtractor.create(httpAttributesExtractor);
+ SpanStatusExtractor<? super Request, ? super Response> spanStatusExtractor =
+ HttpSpanStatusExtractor.create(httpAttributesExtractor);
+ AsyncHttpClientNetAttributesExtractor netAttributesExtractor =
+ new AsyncHttpClientNetAttributesExtractor();
+
+ INSTRUMENTER =
+ Instrumenter.<Request, Response>newBuilder(
+ GlobalOpenTelemetry.get(), INSTRUMENTATION_NAME, spanNameExtractor)
+ .setSpanStatusExtractor(spanStatusExtractor)
+ .addAttributesExtractor(httpAttributesExtractor)
+ .addAttributesExtractor(netAttributesExtractor)
+ .addAttributesExtractor(PeerServiceAttributesExtractor.create(netAttributesExtractor))
+ .addRequestMetrics(HttpClientMetrics.get())
+ .newClientInstrumenter(new HttpHeaderSetter());
+ }
+
+ public static Instrumenter<Request, Response> instrumenter() {
+ return INSTRUMENTER;
+ }
+
+ private AsyncHttpClientSingletons() {}
+}

DELETE
instrumentation/async-http-client/async-http-client-1.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_9/AsyncHttpClientTracer.java
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.javaagent.instrumentation.asynchttpclient.v1_9;
-
-import com.ning.http.client.Request;
-import com.ning.http.client.Response;
-import io.opentelemetry.context.propagation.TextMapSetter;
-import io.opentelemetry.instrumentation.api.tracer.HttpClientTracer;
-import io.opentelemetry.instrumentation.api.tracer.net.NetPeerAttributes;
-import java.net.URI;
-import java.net.URISyntaxException;
-
-public class AsyncHttpClientTracer extends HttpClientTracer<Request, Request, Response> {
-
- private static final AsyncHttpClientTracer TRACER = new AsyncHttpClientTracer();
-
- private AsyncHttpClientTracer() {
- super(NetPeerAttributes.INSTANCE);
- }
-
- public static AsyncHttpClientTracer tracer() {
- return TRACER;
- }
-
- @Override
- protected String method(Request request) {
- return request.getMethod();
- }
-
- @Override
- protected URI url(Request request) throws URISyntaxException {
- return request.getUri().toJavaNetURI();
- }
-
- @Override
- protected Integer status(Response response) {
- return response.getStatusCode();
- }
-
- @Override
- protected String requestHeader(Request request, String name) {
- return request.getHeaders().getFirstValue(name);
- }
-
- @Override
- protected String responseHeader(Response response, String name) {
- return response.getHeaders().getFirstValue(name);
- }
-
- @Override
- protected TextMapSetter<Request> getSetter() {
- return AsyncHttpClientInjectAdapter.SETTER;
- }
-
- @Override
- protected String getInstrumentationName() {
- return "io.opentelemetry.async-http-client-1.9";
- }
-}

RENAME
instrumentation/async-http-client/async-http-client-1.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_9/AsyncHttpClientInjectAdapter.java
instrumentation/async-http-client/async-http-client-1.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_9/HttpHeaderSetter.java
@@ -8,9 +8,7 @@ package io.opentelemetry.javaagent.instrumentation.asynchttpclient.v1_9;
import com.ning.http.client.Request;
import io.opentelemetry.context.propagation.TextMapSetter;
-public class AsyncHttpClientInjectAdapter implements TextMapSetter<Request> {
-
- public static final AsyncHttpClientInjectAdapter SETTER = new AsyncHttpClientInjectAdapter();
+public class HttpHeaderSetter implements TextMapSetter<Request> {
@Override
public void set(Request carrier, String key, String value) {

MODIFY
instrumentation/async-http-client/async-http-client-1.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_9/RequestInstrumentation.java
instrumentation/async-http-client/async-http-client-1.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_9/RequestInstrumentation.java
package io.opentelemetry.javaagent.instrumentation.asynchttpclient.v1_9;
import static io.opentelemetry.javaagent.instrumentation.api.Java8BytecodeBridge.currentContext;
-import static io.opentelemetry.javaagent.instrumentation.asynchttpclient.v1_9.AsyncHttpClientTracer.tracer;
+import static io.opentelemetry.javaagent.instrumentation.asynchttpclient.v1_9.AsyncHttpClientSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
@@ -48,13 +48,13 @@ public class RequestInstrumentation implements TypeInstrumentation {
@Advice.Argument(1) AsyncHandler<?> handler,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
- if (!tracer().shouldStartSpan(parentContext)) {
+ if (!instrumenter().shouldStart(parentContext, request)) {
return;
}
- Context context = tracer().startSpan(parentContext, request, request);
- InstrumentationContext.get(AsyncHandler.class, Contexts.class)
- .put(handler, Contexts.create(parentContext, context));
+ Context context = instrumenter().start(parentContext, request);
+ InstrumentationContext.get(AsyncHandler.class, AsyncHandlerData.class)
+ .put(handler, AsyncHandlerData.create(parentContext, context, request));
scope = context.makeCurrent();
}
@@ -63,7 +63,7 @@ public class RequestInstrumentation implements TypeInstrumentation {
if (scope != null) {
scope.close();
}
- // span ended in ResponseAdvice or ResponseFailureAdvice
+ // span ended in OnCompletedAdvice or OnThrowableAdvice
}
}
}

MODIFY
instrumentation/async-http-client/async-http-client-1.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_9/ResponseInstrumentation.java
instrumentation/async-http-client/async-http-client-1.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_9/ResponseInstrumentation.java
package io.opentelemetry.javaagent.instrumentation.asynchttpclient.v1_9;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
+import static io.opentelemetry.javaagent.instrumentation.asynchttpclient.v1_9.AsyncHttpClientSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.hasSuperClass;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.named;
@@ -54,15 +55,15 @@ public class ResponseInstrumentation implements TypeInstrumentation {
public static Scope onEnter(
@Advice.This AsyncCompletionHandler<?> handler, @Advice.Argument(0) Response response) {
- ContextStore<AsyncHandler<?>, Contexts> contextStore =
- InstrumentationContext.get(AsyncHandler.class, Contexts.class);
- Contexts contexts = contextStore.get(handler);
- if (contexts == null) {
+ ContextStore<AsyncHandler<?>, AsyncHandlerData> contextStore =
+ InstrumentationContext.get(AsyncHandler.class, AsyncHandlerData.class);
+ AsyncHandlerData data = contextStore.get(handler);
+ if (data == null) {
return null;
}
contextStore.put(handler, null);
- AsyncHttpClientTracer.tracer().end(contexts.getContext(), response);
- return contexts.getParentContext().makeCurrent();
+ instrumenter().end(data.getContext(), data.getRequest(), response, null);
+ return data.getParentContext().makeCurrent();
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
@@ -80,15 +81,15 @@ public class ResponseInstrumentation implements TypeInstrumentation {
public static Scope onEnter(
@Advice.This AsyncCompletionHandler<?> handler, @Advice.Argument(0) Throwable throwable) {
- ContextStore<AsyncHandler<?>, Contexts> contextStore =
- InstrumentationContext.get(AsyncHandler.class, Contexts.class);
- Contexts contexts = contextStore.get(handler);
- if (contexts == null) {
+ ContextStore<AsyncHandler<?>, AsyncHandlerData> contextStore =
+ InstrumentationContext.get(AsyncHandler.class, AsyncHandlerData.class);
+ AsyncHandlerData data = contextStore.get(handler);
+ if (data == null) {
return null;
}
contextStore.put(handler, null);
- AsyncHttpClientTracer.tracer().endExceptionally(contexts.getContext(), throwable);
- return contexts.getParentContext().makeCurrent();
+ instrumenter().end(data.getContext(), data.getRequest(), null, throwable);
+ return data.getParentContext().makeCurrent();
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)

MODIFY
instrumentation/async-http-client/async-http-client-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v2_0/HttpHeaderSetter.java
instrumentation/async-http-client/async-http-client-2.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v2_0/HttpHeaderSetter.java
@@ -10,8 +10,6 @@ import org.asynchttpclient.Request;
public class HttpHeaderSetter implements TextMapSetter<Request> {
- public static final HttpHeaderSetter SETTER = new HttpHeaderSetter();
-
@Override
public void set(Request carrier, String key, String value) {
carrier.getHeaders().set(key, value);

Update async-http-client-1.9 to Instrumenter API (#3794)