Skip to content

Commit 0fe64a2

Browse files
authored
Run openserch asyn callbacks with parent context (#19027)
1 parent 23da760 commit 0fe64a2

3 files changed

Lines changed: 26 additions & 5 deletions

File tree

instrumentation/opensearch/opensearch-java-3.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/opensearch/v3_0/OpenSearchTransportInstrumentation.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,14 @@ public void transform(TypeTransformer transformer) {
5858

5959
public static class AdviceScope {
6060
private final OpenSearchRequest otelRequest;
61+
private final Context parentContext;
6162
private final Context context;
6263
private final Scope scope;
6364

64-
private AdviceScope(OpenSearchRequest otelRequest, Context context, Scope scope) {
65+
private AdviceScope(
66+
OpenSearchRequest otelRequest, Context parentContext, Context context, Scope scope) {
6567
this.otelRequest = otelRequest;
68+
this.parentContext = parentContext;
6669
this.context = context;
6770
this.scope = scope;
6871
}
@@ -87,11 +90,29 @@ public static AdviceScope start(
8790
return null;
8891
}
8992
Context context = instrumenter().start(parentContext, otelRequest);
90-
return new AdviceScope(otelRequest, context, context.makeCurrent());
93+
return new AdviceScope(otelRequest, parentContext, context, context.makeCurrent());
9194
}
9295

9396
public CompletableFuture<Object> wrapFuture(CompletableFuture<Object> future) {
94-
return future.whenComplete(new OpenSearchResponseHandler(context, otelRequest));
97+
return wrapFuture(
98+
future.whenComplete(new OpenSearchResponseHandler(context, otelRequest)), parentContext);
99+
}
100+
101+
private static <T> CompletableFuture<T> wrapFuture(
102+
CompletableFuture<T> future, Context context) {
103+
CompletableFuture<T> result = new CompletableFuture<>();
104+
future.whenComplete(
105+
(T value, Throwable throwable) -> {
106+
try (Scope ignored = context.makeCurrent()) {
107+
if (throwable != null) {
108+
result.completeExceptionally(throwable);
109+
} else {
110+
result.complete(value);
111+
}
112+
}
113+
});
114+
115+
return result;
95116
}
96117

97118
public void end(@Nullable Throwable throwable) {

instrumentation/opensearch/opensearch-java-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/opensearch/v3_0/AbstractOpenSearchTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ void shouldGetStatusAsyncWithTraces() throws Exception {
159159
span ->
160160
span.hasName("callback")
161161
.hasKind(SpanKind.INTERNAL)
162-
.hasParent(trace.getSpan(1))));
162+
.hasParent(trace.getSpan(0))));
163163
}
164164

165165
@Test

instrumentation/opensearch/opensearch-java-3.0/javaagent/src/test/java/io/opentelemetry/javaagent/instrumentation/opensearch/v3_0/OpenSearchAwsSdk2TransportTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,6 @@ void shouldGetStatusAsyncWithTraces() throws Exception {
203203
span ->
204204
span.hasName("callback")
205205
.hasKind(SpanKind.INTERNAL)
206-
.hasParent(trace.getSpan(1))));
206+
.hasParent(trace.getSpan(0))));
207207
}
208208
}

0 commit comments

Comments
 (0)