Skip to content

Commit f69e6b3

Browse files
committed
Enhance AsyncHandlerData to include Instrumenter
1 parent 45e9b8b commit f69e6b3

File tree

5 files changed

+15
-34
lines changed

5 files changed

+15
-34
lines changed

instrumentation/async-http-client/async-http-client-1-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/common/AsyncHandlerData.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,26 @@
77

88
import com.google.auto.value.AutoValue;
99
import com.ning.http.client.Request;
10+
import com.ning.http.client.Response;
1011
import io.opentelemetry.context.Context;
12+
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
1113

1214
@AutoValue
1315
public abstract class AsyncHandlerData {
1416

15-
public static AsyncHandlerData create(Context parentContext, Context context, Request request) {
16-
return new AutoValue_AsyncHandlerData(parentContext, context, request);
17+
public static AsyncHandlerData create(
18+
Context parentContext,
19+
Context context,
20+
Request request,
21+
Instrumenter<Request, Response> instrumenter) {
22+
return new AutoValue_AsyncHandlerData(parentContext, context, request, instrumenter);
1723
}
1824

1925
public abstract Context getParentContext();
2026

2127
public abstract Context getContext();
2228

2329
public abstract Request getRequest();
30+
31+
public abstract Instrumenter<Request, Response> getInstrumenter();
2432
}

instrumentation/async-http-client/async-http-client-1-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/common/InstrumenterContextKey.java

Lines changed: 0 additions & 16 deletions
This file was deleted.

instrumentation/async-http-client/async-http-client-1-common/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/common/ResponseInstrumentation.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ public static Scope onEnter(
6363
}
6464
asyncHandlerDataField.set(handler, null);
6565

66-
// Get instrumenter from the data context - we'll store it there
67-
@SuppressWarnings("unchecked")
68-
Instrumenter<Request, Response> instrumenter =
69-
(Instrumenter<Request, Response>) data.getContext().get(InstrumenterContextKey.KEY);
66+
Instrumenter<Request, Response> instrumenter = data.getInstrumenter();
7067
if (instrumenter != null) {
7168
instrumenter.end(data.getContext(), data.getRequest(), response, null);
7269
}
@@ -96,10 +93,8 @@ public static Scope onEnter(
9693
}
9794
asyncHandlerDataField.set(handler, null);
9895

99-
// Get instrumenter from the data context - we'll store it there
100-
@SuppressWarnings("unchecked")
101-
Instrumenter<Request, Response> instrumenter =
102-
(Instrumenter<Request, Response>) data.getContext().get(InstrumenterContextKey.KEY);
96+
// Get instrumenter directly from data - much cleaner!
97+
Instrumenter<Request, Response> instrumenter = data.getInstrumenter();
10398
if (instrumenter != null) {
10499
instrumenter.end(data.getContext(), data.getRequest(), null, throwable);
105100
}

instrumentation/async-http-client/async-http-client-1.8/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_8/AsyncHttpProviderInstrumentation.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2222
import javax.annotation.Nullable;
2323
import io.opentelemetry.javaagent.instrumentation.asynchttpclient.common.AsyncHandlerData;
24-
import io.opentelemetry.javaagent.instrumentation.asynchttpclient.common.InstrumenterContextKey;
2524
import net.bytebuddy.asm.Advice;
2625
import net.bytebuddy.description.type.TypeDescription;
2726
import net.bytebuddy.matcher.ElementMatcher;
@@ -57,9 +56,7 @@ public static Scope onEnter(
5756
}
5857

5958
Context context = instrumenter().start(parentContext, request);
60-
// Store instrumenter in context so ResponseInstrumentation can access it
61-
Context contextWithInstrumenter = context.with(InstrumenterContextKey.KEY, instrumenter());
62-
ASYNC_HANDLER_DATA.set(handler, AsyncHandlerData.create(parentContext, contextWithInstrumenter, request));
59+
ASYNC_HANDLER_DATA.set(handler, AsyncHandlerData.create(parentContext, context, request, instrumenter()));
6360
return context.makeCurrent();
6461
}
6562

instrumentation/async-http-client/async-http-client-1.9/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/asynchttpclient/v1_9/RequestInstrumentation.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
2020
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
2121
import io.opentelemetry.javaagent.instrumentation.asynchttpclient.common.AsyncHandlerData;
22-
import io.opentelemetry.javaagent.instrumentation.asynchttpclient.common.InstrumenterContextKey;
2322
import javax.annotation.Nullable;
2423
import net.bytebuddy.asm.Advice;
2524
import net.bytebuddy.description.type.TypeDescription;
@@ -55,9 +54,7 @@ public static Scope onEnter(
5554
}
5655

5756
Context context = instrumenter().start(parentContext, request);
58-
// Store instrumenter in context so ResponseInstrumentation can access it
59-
Context contextWithInstrumenter = context.with(InstrumenterContextKey.KEY, instrumenter());
60-
ASYNC_HANDLER_DATA.set(handler, AsyncHandlerData.create(parentContext, contextWithInstrumenter, request));
57+
ASYNC_HANDLER_DATA.set(handler, AsyncHandlerData.create(parentContext, context, request, instrumenter()));
6158
return context.makeCurrent();
6259
}
6360

0 commit comments

Comments
 (0)