Skip to content

Commit edc40f6

Browse files
committed
Promise response method exit handling logic updated
1 parent ad7692a commit edc40f6

File tree

2 files changed

+57
-7
lines changed

2 files changed

+57
-7
lines changed

instrumentation/activej-http-6.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/activejhttp/ActivejHttpServerConnectionInstrumentation.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public static void methodEnter(
7171
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
7272
public static void methodExit(
7373
@Advice.This AsyncServlet asyncServlet,
74-
@Advice.Return Promise<HttpResponse> responsePromise,
74+
@Advice.Return(readOnly = false) Promise<HttpResponse> responsePromise,
7575
@Advice.Thrown Throwable throwable,
7676
@Advice.Local("otelContext") Context context,
7777
@Advice.Local("otelScope") Scope scope,
@@ -80,12 +80,11 @@ public static void methodExit(
8080
return;
8181
}
8282
scope.close();
83-
instrumenter()
84-
.end(
85-
context,
86-
httpRequest,
87-
responsePromise == null ? null : responsePromise.getResult(),
88-
throwable);
83+
if (throwable != null) {
84+
instrumenter().end(context, httpRequest, null, throwable);
85+
} else {
86+
responsePromise = PromiseWrapper.wrap(responsePromise, httpRequest, context);
87+
}
8988
}
9089
}
9190
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.activejhttp;
7+
8+
import static io.opentelemetry.javaagent.instrumentation.activejhttp.ActivejHttpServerConnectionSingletons.instrumenter;
9+
10+
import io.activej.http.HttpRequest;
11+
import io.activej.http.HttpResponse;
12+
import io.activej.promise.Promise;
13+
import io.activej.promise.SettablePromise;
14+
import io.opentelemetry.context.Context;
15+
import io.opentelemetry.context.Scope;
16+
17+
public class PromiseWrapper {
18+
19+
public static Promise<HttpResponse> wrap(
20+
Promise<HttpResponse> promise, HttpRequest httpRequest, Context context) {
21+
SettablePromise<HttpResponse> settablePromise = new SettablePromise<>();
22+
promise
23+
.whenResult(
24+
result -> {
25+
Exception error = null;
26+
try (Scope ignored = context.makeCurrent()) {
27+
settablePromise.set(result);
28+
} catch (RuntimeException exception) {
29+
error = exception;
30+
settablePromise.setException(
31+
new RuntimeException("Context management failed", exception));
32+
} finally {
33+
instrumenter().end(context, httpRequest, result, error);
34+
}
35+
})
36+
.whenException(
37+
throwable -> {
38+
try (Scope ignored = context.makeCurrent()) {
39+
settablePromise.setException(throwable);
40+
} catch (RuntimeException exception) {
41+
settablePromise.setException(
42+
new RuntimeException("Context management failed", exception));
43+
} finally {
44+
instrumenter().end(context, httpRequest, null, throwable);
45+
}
46+
});
47+
return settablePromise;
48+
}
49+
50+
private PromiseWrapper() {}
51+
}

0 commit comments

Comments
 (0)