Skip to content

Commit 4e1de9f

Browse files
committed
Ensure tilde$1 onExit is run in correct order
1 parent 4c8f2dc commit 4e1de9f

File tree

1 file changed

+30
-4
lines changed

1 file changed

+30
-4
lines changed

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

+30-4
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,28 +33,48 @@ 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

3353
@Advice.OnMethodEnter(suppress = Throwable.class)
3454
public static void onEnter() {
35-
// when routing dsl uses concat(path(...) {...}, path(...) {...}) we'll restore the currently
36-
// matched route after each matcher so that match attempts that failed wouldn't get recorded
55+
// when routing dsl uses concat(path(...) {...}, path(...) {...}) we'll restore
56+
// the currently
57+
// matched route after each matcher so that match attempts that failed wouldn't
58+
// get recorded
3759
// in the route
60+
System.err.println("===tilde$1 onEnter===");
3861
PekkoRouteHolder.save();
3962
}
4063

4164
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
42-
public static void onExit() {
43-
PekkoRouteHolder.restore();
65+
public static void onExit(@Advice.Argument(value = 2) RequestContext ctx, @Advice.Return(readOnly = false) Future<RouteResult> fut) {
66+
fut = fut
67+
.andThen(new OnExitFinalizer(), ctx.executionContext());
4468
}
69+
4570
}
4671

4772
@SuppressWarnings("unused")
4873
public static class Apply2Advice {
4974

5075
@Advice.OnMethodEnter(suppress = Throwable.class)
5176
public static void onEnter() {
77+
System.err.println("===tilde$2 onEnter===");
5278
PekkoRouteHolder.reset();
5379
}
5480
}

0 commit comments

Comments
 (0)