Skip to content

Commit 0a6dbbf

Browse files
committed
fix(pekko): Ensure tilde$1 onExit is run in correct order
1 parent 6cd791b commit 0a6dbbf

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

instrumentation/pekko/pekko-http-1.0/javaagent/src/main/java/io/opentelemetry/javaagent/instrumentation/pekkohttp/v1_0/server/route/RouteConcatenationInstrumentation.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
import net.bytebuddy.asm.Advice;
1313
import net.bytebuddy.description.type.TypeDescription;
1414
import net.bytebuddy.matcher.ElementMatcher;
15+
import org.apache.pekko.http.javadsl.server.RouteResult;
16+
import org.apache.pekko.http.scaladsl.server.RequestContext;
17+
import scala.PartialFunction;
18+
import scala.Unit;
19+
import scala.concurrent.Future;
20+
import scala.util.Try;
1521

1622
public class RouteConcatenationInstrumentation implements TypeInstrumentation {
1723
@Override
@@ -27,6 +33,19 @@ public void transform(TypeTransformer transformer) {
2733
named("$anonfun$$tilde$2"), this.getClass().getName() + "$Apply2Advice");
2834
}
2935

36+
public static class OnExitFinalizer implements PartialFunction<Try<RouteResult>, Unit> {
37+
@Override
38+
public boolean isDefinedAt(Try<RouteResult> x) {
39+
return true;
40+
}
41+
42+
@Override
43+
public Unit apply(Try<RouteResult> v1) {
44+
PekkoRouteHolder.restore();
45+
return null;
46+
}
47+
}
48+
3049
@SuppressWarnings("unused")
3150
public static class ApplyAdvice {
3251

@@ -39,8 +58,10 @@ public static void onEnter() {
3958
}
4059

4160
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
42-
public static void onExit() {
43-
PekkoRouteHolder.restore();
61+
public static void onExit(
62+
@Advice.Argument(value = 2) RequestContext ctx,
63+
@Advice.Return(readOnly = false) Future<RouteResult> fut) {
64+
fut = fut.andThen(new OnExitFinalizer(), ctx.executionContext());
4465
}
4566
}
4667

@@ -49,6 +70,7 @@ public static class Apply2Advice {
4970

5071
@Advice.OnMethodEnter(suppress = Throwable.class)
5172
public static void onEnter() {
73+
System.err.println("===tilde$2 onEnter===");
5274
PekkoRouteHolder.reset();
5375
}
5476
}

0 commit comments

Comments
 (0)