Skip to content

Commit ebd0c56

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

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

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

+25-2
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,20 @@ 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+
System.err.println("===tilde$1 onExit===");
45+
PekkoRouteHolder.restore();
46+
return null;
47+
}
48+
}
49+
3050
@SuppressWarnings("unused")
3151
public static class ApplyAdvice {
3252

@@ -39,8 +59,10 @@ public static void onEnter() {
3959
}
4060

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

@@ -49,6 +71,7 @@ public static class Apply2Advice {
4971

5072
@Advice.OnMethodEnter(suppress = Throwable.class)
5173
public static void onEnter() {
74+
System.err.println("===tilde$2 onEnter===");
5275
PekkoRouteHolder.reset();
5376
}
5477
}

0 commit comments

Comments
 (0)