-
Notifications
You must be signed in to change notification settings - Fork 870
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
Improve akka route handling with java dsl #11926
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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()); | ||
} | ||
} | ||
} |
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps add a comment why the test instance is per class. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here I think it is because of the |
||
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() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you. 🙏🏻