Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure tilde$1 onExit is run in correct order #13360

Merged
merged 4 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,14 @@ dependencies {
library("org.apache.pekko:pekko-http_2.12:1.0.0")
library("org.apache.pekko:pekko-stream_2.12:1.0.1")

testImplementation("com.softwaremill.sttp.tapir:tapir-pekko-http-server_2.12:1.7.0")

testInstrumentation(project(":instrumentation:pekko:pekko-actor-1.0:javaagent"))
testInstrumentation(project(":instrumentation:executors:javaagent"))

latestDepTestLibrary("org.apache.pekko:pekko-http_2.13:+")
latestDepTestLibrary("org.apache.pekko:pekko-stream_2.13:+")
latestDepTestLibrary("org.apache.pekko:pekko-http_2.13:latest.release")
latestDepTestLibrary("org.apache.pekko:pekko-stream_2.13:latest.release")
latestDepTestLibrary("com.softwaremill.sttp.tapir:tapir-pekko-http-server_2.13:latest.release")
}

tasks {
Expand All @@ -56,6 +59,7 @@ if (findProperty("testLatestDeps") as Boolean) {
testImplementation {
exclude("org.apache.pekko", "pekko-http_2.12")
exclude("org.apache.pekko", "pekko-stream_2.12")
exclude("com.softwaremill.sttp.tapir", "tapir-pekko-http-server_2.12")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.route;

import org.apache.pekko.http.scaladsl.server.RouteResult;
import scala.PartialFunction;
import scala.Unit;
import scala.util.Try;

public class RestoreOnExit implements PartialFunction<Try<RouteResult>, Unit> {
@Override
public boolean isDefinedAt(Try<RouteResult> x) {
return true;
}

@Override
public Unit apply(Try<RouteResult> v1) {
PekkoRouteHolder.restore();
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
import org.apache.pekko.http.scaladsl.server.RequestContext;
import org.apache.pekko.http.scaladsl.server.RouteResult;
import scala.concurrent.Future;

public class RouteConcatenationInstrumentation implements TypeInstrumentation {
@Override
Expand Down Expand Up @@ -39,8 +42,15 @@ public static void onEnter() {
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void onExit() {
PekkoRouteHolder.restore();
public static void onExit(
@Advice.Argument(value = 2) RequestContext ctx,
@Advice.Return(readOnly = false) Future<RouteResult> future,
@Advice.Thrown Throwable throwable) {
if (throwable != null) {
PekkoRouteHolder.restore();
} else {
future = future.andThen(new RestoreOnExit(), ctx.executionContext());
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ import org.junit.jupiter.api.{AfterAll, Test, TestInstance}

import java.net.{URI, URISyntaxException}
import java.util.function.Consumer
import scala.concurrent.Await
import scala.concurrent.{Await, ExecutionContext, Future}
import scala.concurrent.duration.DurationInt
import sttp.tapir._
import sttp.tapir.server.pekkohttp.PekkoHttpServerInterpreter

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
class PekkoHttpServerRouteTest {
Expand Down Expand Up @@ -77,6 +79,26 @@ class PekkoHttpServerRouteTest {
test(route, "/test/1", "GET /test/*")
}

@Test def testTapirRoutes(): Unit = {
val interpreter = PekkoHttpServerInterpreter()(system.dispatcher)
def makeRoute(input: EndpointInput[Unit]) = {
interpreter.toRoute(
endpoint.get
.in(input)
.errorOut(stringBody)
.out(stringBody)
.serverLogicPure[Future](_ => Right("ok"))
)
}

val routes = concat(
concat(makeRoute("test" / "1"), makeRoute("test" / "2")),
concat(makeRoute("test" / "3"), makeRoute("test" / "4"))
)

test(routes, "/test/4", "GET")
}

def test(route: Route, path: String, spanName: String): Unit = {
val port = PortUtils.findOpenPort
val address: URI = buildAddress(port)
Expand Down
Loading