Convert Pair class to AutoValue
MODIFY
instrumentation/async-http-client/async-http-client-1.9/javaagent/build.gradle.kts
instrumentation/async-http-client/async-http-client-1.9/javaagent/build.gradle.kts
@@ -13,4 +13,7 @@ muzzle {
dependencies {
library("com.ning:async-http-client:1.9.0")
+
+ compileOnly("com.google.auto.value:auto-value-annotations")
+ annotationProcessor("com.google.auto.value:auto-value")
}

ADD
instrumentation/async-http-client/async-http-client-1.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_9/Contexts.java
+/*
+ * Copyright The OpenTelemetry Authors
+ * SPDX-License-Identifier: Apache-2.0
+ */
+
+package io.opentelemetry.javaagent.instrumentation.asynchttpclient.v1_9;
+
+import com.google.auto.value.AutoValue;
+import io.opentelemetry.context.Context;
+
+@AutoValue
+public abstract class Contexts {
+
+ public static Contexts create(Context parentContext, Context context) {
+ return new AutoValue_Contexts(parentContext, context);
+ }
+
+ public abstract Context getParentContext();
+
+ public abstract Context getContext();
+
+ Contexts() {}
+}

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
@@ -18,7 +18,6 @@ import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext;
-import io.opentelemetry.javaagent.instrumentation.api.Pair;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
@@ -54,8 +53,8 @@ public class RequestInstrumentation implements TypeInstrumentation {
}
Context context = tracer().startSpan(parentContext, request, request);
- InstrumentationContext.get(AsyncHandler.class, Pair.class)
- .put(handler, Pair.of(parentContext, context));
+ InstrumentationContext.get(AsyncHandler.class, Contexts.class)
+ .put(handler, Contexts.create(parentContext, context));
scope = context.makeCurrent();
}

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
@@ -14,13 +14,11 @@ import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.AsyncHandler;
import com.ning.http.client.Response;
-import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.ContextStore;
import io.opentelemetry.javaagent.instrumentation.api.InstrumentationContext;
-import io.opentelemetry.javaagent.instrumentation.api.Pair;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
@@ -56,15 +54,15 @@ public class ResponseInstrumentation implements TypeInstrumentation {
public static Scope onEnter(
@Advice.This AsyncCompletionHandler<?> handler, @Advice.Argument(0) Response response) {
- ContextStore<AsyncHandler, Pair> contextStore =
- InstrumentationContext.get(AsyncHandler.class, Pair.class);
- Pair<Context, Context> parentAndChildContext = contextStore.get(handler);
- if (parentAndChildContext == null) {
+ ContextStore<AsyncHandler<?>, Contexts> contextStore =
+ InstrumentationContext.get(AsyncHandler.class, Contexts.class);
+ Contexts contexts = contextStore.get(handler);
+ if (contexts == null) {
return null;
}
contextStore.put(handler, null);
- AsyncHttpClientTracer.tracer().end(parentAndChildContext.getRight(), response);
- return parentAndChildContext.getLeft().makeCurrent();
+ AsyncHttpClientTracer.tracer().end(contexts.getContext(), response);
+ return contexts.getParentContext().makeCurrent();
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
@@ -82,15 +80,15 @@ public class ResponseInstrumentation implements TypeInstrumentation {
public static Scope onEnter(
@Advice.This AsyncCompletionHandler<?> handler, @Advice.Argument(0) Throwable throwable) {
- ContextStore<AsyncHandler<?>, Pair> contextStore =
- InstrumentationContext.get(AsyncHandler.class, Pair.class);
- Pair<Context, Context> parentAndChildContext = contextStore.get(handler);
- if (parentAndChildContext == null) {
+ ContextStore<AsyncHandler<?>, Contexts> contextStore =
+ InstrumentationContext.get(AsyncHandler.class, Contexts.class);
+ Contexts contexts = contextStore.get(handler);
+ if (contexts == null) {
return null;
}
contextStore.put(handler, null);
- AsyncHttpClientTracer.tracer().endExceptionally(parentAndChildContext.getRight(), throwable);
- return parentAndChildContext.getLeft().makeCurrent();
+ AsyncHttpClientTracer.tracer().endExceptionally(contexts.getContext(), throwable);
+ return contexts.getParentContext().makeCurrent();
}
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)

DELETE
javaagent-api/src/main/java/io/opentelemetry/javaagent/instrumentation/api/Pair.java
-/*
- * Copyright The OpenTelemetry Authors
- * SPDX-License-Identifier: Apache-2.0
- */
-
-package io.opentelemetry.javaagent.instrumentation.api;
-
-public final class Pair<T, U> {
-
- public static <T, U> Pair<T, U> of(T left, U right) {
- return new Pair<>(left, right);
- }
-
- private final T left;
- private final U right;
-
- Pair(T left, U right) {
- this.left = left;
- this.right = right;
- }
-
- public T getLeft() {
- return left;
- }
-
- public U getRight() {
- return right;
- }
-
- public boolean hasLeft() {
- return null != left;
- }
-
- public boolean hasRight() {
- return null != right;
- }
-}

Convert Pair class to AutoValue (#3496)