Skip to content

Commit 36f3b4d

Browse files
committed
use PekkoRouteHolder in Tapir instrumentation + test
1 parent 703c9da commit 36f3b4d

File tree

2 files changed

+32
-14
lines changed
  • instrumentation/pekko/pekko-http-1.0/javaagent/src

2 files changed

+32
-14
lines changed

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

+14-5
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@
66
package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.tapir;
77

88
import io.opentelemetry.context.Context;
9-
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRoute;
10-
import io.opentelemetry.instrumentation.api.semconv.http.HttpServerRouteSource;
119
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
10+
import io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.route.PekkoRouteHolder;
11+
import java.nio.charset.Charset;
12+
import org.apache.pekko.http.scaladsl.model.Uri;
1213
import org.apache.pekko.http.scaladsl.server.RequestContext;
1314
import org.apache.pekko.http.scaladsl.server.RouteResult;
1415
import scala.Function1;
@@ -22,6 +23,7 @@
2223
import sttp.tapir.server.ServerEndpoint;
2324

2425
public class RouteWrapper implements Function1<RequestContext, Future<RouteResult>> {
26+
private static final Uri.Path EMPTY = Uri.Path$.MODULE$.apply("", Charset.defaultCharset());
2527
private final Function1<RequestContext, Future<RouteResult>> route;
2628
private final ServerEndpoint<?, ?> serverEndpoint;
2729

@@ -32,6 +34,12 @@ public RouteWrapper(
3234
}
3335

3436
public class Finalizer implements PartialFunction<Try<RouteResult>, Unit> {
37+
private final Uri.Path beforeMatch;
38+
39+
public Finalizer(Uri.Path beforeMatch) {
40+
this.beforeMatch = beforeMatch;
41+
}
42+
3543
@Override
3644
public boolean isDefinedAt(Try<RouteResult> tryResult) {
3745
return tryResult.isSuccess();
@@ -40,7 +48,8 @@ public boolean isDefinedAt(Try<RouteResult> tryResult) {
4048
@Override
4149
public Unit apply(Try<RouteResult> tryResult) {
4250
Context context = Java8BytecodeBridge.currentContext();
43-
if (tryResult.isSuccess()) {
51+
PekkoRouteHolder routeHolder = PekkoRouteHolder.get(context);
52+
if (routeHolder != null && tryResult.isSuccess()) {
4453
RouteResult result = tryResult.get();
4554
if (result.getClass() == RouteResult.Complete.class) {
4655
String path =
@@ -54,7 +63,7 @@ public Unit apply(Try<RouteResult> tryResult) {
5463
"*",
5564
Option.apply("*"),
5665
Option.apply("*"));
57-
HttpServerRoute.update(context, HttpServerRouteSource.NESTED_CONTROLLER, path);
66+
routeHolder.push(beforeMatch, EMPTY, path);
5867
}
5968
}
6069
return null;
@@ -63,6 +72,6 @@ public Unit apply(Try<RouteResult> tryResult) {
6372

6473
@Override
6574
public Future<RouteResult> apply(RequestContext ctx) {
66-
return route.apply(ctx).andThen(new Finalizer(), ctx.executionContext());
75+
return route.apply(ctx).andThen(new Finalizer(ctx.unmatchedPath()), ctx.executionContext());
6776
}
6877
}

instrumentation/pekko/pekko-http-1.0/javaagent/src/tapirTest/scala/io/opentelemetry/javaagent/instrumentation/pekkohttp/v1_0/TapirHttpServerRouteTest.scala

+18-9
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,7 @@ import io.opentelemetry.testing.internal.armeria.common.{
1515
}
1616
import org.apache.pekko.actor.ActorSystem
1717
import org.apache.pekko.http.scaladsl.Http
18-
import org.apache.pekko.http.scaladsl.server.Directives.{
19-
IntNumber,
20-
complete,
21-
concat,
22-
path,
23-
pathEndOrSingleSlash,
24-
pathPrefix,
25-
pathSingleSlash
26-
}
18+
import org.apache.pekko.http.scaladsl.server.Directives.{concat, pathPrefix}
2719
import org.apache.pekko.http.scaladsl.server.Route
2820
import org.assertj.core.api.Assertions.assertThat
2921
import org.junit.jupiter.api.extension.RegisterExtension
@@ -52,6 +44,7 @@ class TapirHttpServerRouteTest {
5244
}
5345

5446
@Test def testSimple(): Unit = {
47+
import org.apache.pekko.http.scaladsl.server.Directives._
5548
val route = path("test") {
5649
complete("ok")
5750
}
@@ -60,6 +53,7 @@ class TapirHttpServerRouteTest {
6053
}
6154

6255
@Test def testRoute(): Unit = {
56+
import org.apache.pekko.http.scaladsl.server.Directives._
6357
val route = concat(
6458
pathEndOrSingleSlash {
6559
complete("root")
@@ -99,6 +93,21 @@ class TapirHttpServerRouteTest {
9993
test(routes, "/test/4", "GET /test/4")
10094
}
10195

96+
@Test def testTapirWithPathPrefix(): Unit = {
97+
val interpreter = PekkoHttpServerInterpreter()(system.dispatcher)
98+
val tapirRoute = interpreter.toRoute(
99+
endpoint.get
100+
.in(path[Int]("i") / "bar")
101+
.errorOut(stringBody)
102+
.out(stringBody)
103+
.serverLogicPure[Future](_ => Right("ok"))
104+
)
105+
106+
val prefixedRoute = pathPrefix("foo") { tapirRoute }
107+
test(prefixedRoute, "/foo/123/bar", "GET /foo/{i}/bar")
108+
109+
}
110+
102111
def test(route: Route, path: String, spanName: String): Unit = {
103112
val port = PortUtils.findOpenPort
104113
val address: URI = buildAddress(port)

0 commit comments

Comments
 (0)