Skip to content

Commit f0d80b2

Browse files
authored
Ensure tilde$1 onExit is run in correct order (#13360)
1 parent f5a0d2f commit f0d80b2

File tree

5 files changed

+66
-6
lines changed

5 files changed

+66
-6
lines changed

.github/scripts/check-latest-dep-test-overrides.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
# all missing version coverage should be documented in supported-libraries.md
44

55
if grep -r --include build.gradle.kts latestDepTestLibrary instrumentation \
6-
| grep -v :+\" \
6+
| grep -v -e :+\" -e :latest.release\" \
77
| grep -v "// see .* module" \
88
| grep -v "// see test suite below" \
99
| grep -v "// no longer applicable" \

instrumentation/pekko/pekko-http-1.0/javaagent/build.gradle.kts

+6-2
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,14 @@ dependencies {
3131
library("org.apache.pekko:pekko-http_2.12:1.0.0")
3232
library("org.apache.pekko:pekko-stream_2.12:1.0.1")
3333

34+
testImplementation("com.softwaremill.sttp.tapir:tapir-pekko-http-server_2.12:1.7.0")
35+
3436
testInstrumentation(project(":instrumentation:pekko:pekko-actor-1.0:javaagent"))
3537
testInstrumentation(project(":instrumentation:executors:javaagent"))
3638

37-
latestDepTestLibrary("org.apache.pekko:pekko-http_2.13:+")
38-
latestDepTestLibrary("org.apache.pekko:pekko-stream_2.13:+")
39+
latestDepTestLibrary("org.apache.pekko:pekko-http_2.13:latest.release")
40+
latestDepTestLibrary("org.apache.pekko:pekko-stream_2.13:latest.release")
41+
latestDepTestLibrary("com.softwaremill.sttp.tapir:tapir-pekko-http-server_2.13:latest.release")
3942
}
4043

4144
tasks {
@@ -56,6 +59,7 @@ if (findProperty("testLatestDeps") as Boolean) {
5659
testImplementation {
5760
exclude("org.apache.pekko", "pekko-http_2.12")
5861
exclude("org.apache.pekko", "pekko-stream_2.12")
62+
exclude("com.softwaremill.sttp.tapir", "tapir-pekko-http-server_2.12")
5963
}
6064
}
6165
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.route;
7+
8+
import org.apache.pekko.http.scaladsl.server.RouteResult;
9+
import scala.PartialFunction;
10+
import scala.Unit;
11+
import scala.util.Try;
12+
13+
public class RestoreOnExit implements PartialFunction<Try<RouteResult>, Unit> {
14+
@Override
15+
public boolean isDefinedAt(Try<RouteResult> x) {
16+
return true;
17+
}
18+
19+
@Override
20+
public Unit apply(Try<RouteResult> v1) {
21+
PekkoRouteHolder.restore();
22+
return null;
23+
}
24+
}

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

+12-2
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
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.scaladsl.server.RequestContext;
16+
import org.apache.pekko.http.scaladsl.server.RouteResult;
17+
import scala.concurrent.Future;
1518

1619
public class RouteConcatenationInstrumentation implements TypeInstrumentation {
1720
@Override
@@ -39,8 +42,15 @@ public static void onEnter() {
3942
}
4043

4144
@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
42-
public static void onExit() {
43-
PekkoRouteHolder.restore();
45+
public static void onExit(
46+
@Advice.Argument(value = 2) RequestContext ctx,
47+
@Advice.Return(readOnly = false) Future<RouteResult> future,
48+
@Advice.Thrown Throwable throwable) {
49+
if (throwable != null) {
50+
PekkoRouteHolder.restore();
51+
} else {
52+
future = future.andThen(new RestoreOnExit(), ctx.executionContext());
53+
}
4454
}
4555
}
4656

instrumentation/pekko/pekko-http-1.0/javaagent/src/test/scala/io/opentelemetry/javaagent/instrumentation/pekkohttp/v1_0/PekkoHttpServerRouteTest.scala

+23-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ import org.junit.jupiter.api.{AfterAll, Test, TestInstance}
3131

3232
import java.net.{URI, URISyntaxException}
3333
import java.util.function.Consumer
34-
import scala.concurrent.Await
34+
import scala.concurrent.{Await, ExecutionContext, Future}
3535
import scala.concurrent.duration.DurationInt
36+
import sttp.tapir._
37+
import sttp.tapir.server.pekkohttp.PekkoHttpServerInterpreter
3638

3739
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
3840
class PekkoHttpServerRouteTest {
@@ -77,6 +79,26 @@ class PekkoHttpServerRouteTest {
7779
test(route, "/test/1", "GET /test/*")
7880
}
7981

82+
@Test def testTapirRoutes(): Unit = {
83+
val interpreter = PekkoHttpServerInterpreter()(system.dispatcher)
84+
def makeRoute(input: EndpointInput[Unit]) = {
85+
interpreter.toRoute(
86+
endpoint.get
87+
.in(input)
88+
.errorOut(stringBody)
89+
.out(stringBody)
90+
.serverLogicPure[Future](_ => Right("ok"))
91+
)
92+
}
93+
94+
val routes = concat(
95+
concat(makeRoute("test" / "1"), makeRoute("test" / "2")),
96+
concat(makeRoute("test" / "3"), makeRoute("test" / "4"))
97+
)
98+
99+
test(routes, "/test/4", "GET")
100+
}
101+
80102
def test(route: Route, path: String, spanName: String): Unit = {
81103
val port = PortUtils.findOpenPort
82104
val address: URI = buildAddress(port)

0 commit comments

Comments
 (0)