-
Notifications
You must be signed in to change notification settings - Fork 869
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve akka route handling with java dsl (#11926)
- Loading branch information
Showing
10 changed files
with
432 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...java/io/opentelemetry/javaagent/instrumentation/akkahttp/AkkaHttpServerJavaRouteTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.akkahttp; | ||
|
||
import static akka.http.javadsl.server.PathMatchers.integerSegment; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import akka.actor.ActorSystem; | ||
import akka.http.javadsl.Http; | ||
import akka.http.javadsl.ServerBinding; | ||
import akka.http.javadsl.server.AllDirectives; | ||
import akka.http.javadsl.server.Route; | ||
import io.opentelemetry.instrumentation.test.utils.PortUtils; | ||
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import io.opentelemetry.testing.internal.armeria.client.WebClient; | ||
import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpRequest; | ||
import io.opentelemetry.testing.internal.armeria.common.AggregatedHttpResponse; | ||
import io.opentelemetry.testing.internal.armeria.common.HttpMethod; | ||
import java.util.concurrent.CompletionStage; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class AkkaHttpServerJavaRouteTest extends AllDirectives { | ||
@RegisterExtension | ||
private static final InstrumentationExtension testing = AgentInstrumentationExtension.create(); | ||
|
||
private final WebClient client = WebClient.of(); | ||
|
||
@Test | ||
void testRoute() { | ||
ActorSystem system = ActorSystem.create("my-system"); | ||
int port = PortUtils.findOpenPort(); | ||
Http http = Http.get(system); | ||
|
||
Route route = | ||
concat( | ||
pathEndOrSingleSlash(() -> complete("root")), | ||
pathPrefix( | ||
"test", | ||
() -> | ||
concat( | ||
pathSingleSlash(() -> complete("test")), | ||
path(integerSegment(), (i) -> complete("ok"))))); | ||
|
||
CompletionStage<ServerBinding> binding = http.newServerAt("localhost", port).bind(route); | ||
try { | ||
AggregatedHttpRequest request = | ||
AggregatedHttpRequest.of(HttpMethod.GET, "h1c://localhost:" + port + "/test/1"); | ||
AggregatedHttpResponse response = client.execute(request).aggregate().join(); | ||
|
||
assertThat(response.status().code()).isEqualTo(200); | ||
assertThat(response.contentUtf8()).isEqualTo("ok"); | ||
|
||
testing.waitAndAssertTraces( | ||
trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("GET /test/*"))); | ||
} finally { | ||
binding.thenCompose(ServerBinding::unbind).thenAccept(unbound -> system.terminate()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
...t/scala/io/opentelemetry/javaagent/instrumentation/akkahttp/AkkaHttpServerRouteTest.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.javaagent.instrumentation.akkahttp | ||
|
||
import akka.actor.ActorSystem | ||
import akka.http.scaladsl.Http | ||
import akka.http.scaladsl.server.Directives.{ | ||
IntNumber, | ||
complete, | ||
concat, | ||
path, | ||
pathEndOrSingleSlash, | ||
pathPrefix, | ||
pathSingleSlash | ||
} | ||
import akka.http.scaladsl.server.Route | ||
import akka.stream.ActorMaterializer | ||
import io.opentelemetry.instrumentation.test.utils.PortUtils | ||
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension | ||
import io.opentelemetry.sdk.testing.assertj.{SpanDataAssert, TraceAssert} | ||
import io.opentelemetry.testing.internal.armeria.client.WebClient | ||
import io.opentelemetry.testing.internal.armeria.common.{ | ||
AggregatedHttpRequest, | ||
HttpMethod | ||
} | ||
import org.assertj.core.api.Assertions.assertThat | ||
import org.junit.jupiter.api.{AfterAll, Test, TestInstance} | ||
import org.junit.jupiter.api.extension.RegisterExtension | ||
|
||
import java.net.{URI, URISyntaxException} | ||
import java.util.function.Consumer | ||
import scala.concurrent.duration.DurationInt | ||
import scala.concurrent.Await | ||
|
||
@TestInstance(TestInstance.Lifecycle.PER_CLASS) | ||
class AkkaHttpServerRouteTest { | ||
@RegisterExtension private val testing: AgentInstrumentationExtension = | ||
AgentInstrumentationExtension.create | ||
private val client: WebClient = WebClient.of() | ||
|
||
implicit val system: ActorSystem = ActorSystem("my-system") | ||
implicit val materializer: ActorMaterializer = ActorMaterializer() | ||
|
||
private def buildAddress(port: Int): URI = try | ||
new URI("http://localhost:" + port + "/") | ||
catch { | ||
case exception: URISyntaxException => | ||
throw new IllegalStateException(exception) | ||
} | ||
|
||
@Test def testSimple(): Unit = { | ||
val route = path("test") { | ||
complete("ok") | ||
} | ||
|
||
test(route, "/test", "GET /test") | ||
} | ||
|
||
@Test def testRoute(): Unit = { | ||
val route = concat( | ||
pathEndOrSingleSlash { | ||
complete("root") | ||
}, | ||
pathPrefix("test") { | ||
concat( | ||
pathSingleSlash { | ||
complete("test") | ||
}, | ||
path(IntNumber) { _ => | ||
complete("ok") | ||
} | ||
) | ||
} | ||
) | ||
|
||
test(route, "/test/1", "GET /test/*") | ||
} | ||
|
||
def test(route: Route, path: String, spanName: String): Unit = { | ||
val port = PortUtils.findOpenPort | ||
val address: URI = buildAddress(port) | ||
val binding = | ||
Await.result(Http().bindAndHandle(route, "localhost", port), 10.seconds) | ||
try { | ||
val request = AggregatedHttpRequest.of( | ||
HttpMethod.GET, | ||
address.resolve(path).toString | ||
) | ||
val response = client.execute(request).aggregate.join | ||
assertThat(response.status.code).isEqualTo(200) | ||
assertThat(response.contentUtf8).isEqualTo("ok") | ||
|
||
testing.waitAndAssertTraces(new Consumer[TraceAssert] { | ||
override def accept(trace: TraceAssert): Unit = | ||
trace.hasSpansSatisfyingExactly(new Consumer[SpanDataAssert] { | ||
override def accept(span: SpanDataAssert): Unit = { | ||
span.hasName(spanName) | ||
} | ||
}) | ||
}) | ||
} finally { | ||
binding.unbind() | ||
} | ||
} | ||
|
||
@AfterAll | ||
def cleanUp(): Unit = { | ||
system.terminate() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.