Skip to content

Commit 28ce215

Browse files
committed
move tapir code to separate package, move tapir tests to separate suite
1 parent 6783f9f commit 28ce215

File tree

6 files changed

+173
-32
lines changed

6 files changed

+173
-32
lines changed

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

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,14 +54,35 @@ muzzle {
5454
dependencies {
5555
library("org.apache.pekko:pekko-http_2.12:1.0.0")
5656
library("org.apache.pekko:pekko-stream_2.12:1.0.1")
57-
library("com.softwaremill.sttp.tapir:tapir-pekko-http-server_2.12:1.7.0")
57+
compileOnly("com.softwaremill.sttp.tapir:tapir-pekko-http-server_2.12:1.7.0")
5858

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

6262
latestDepTestLibrary("org.apache.pekko:pekko-http_2.13:latest.release")
6363
latestDepTestLibrary("org.apache.pekko:pekko-stream_2.13:latest.release")
64-
latestDepTestLibrary("com.softwaremill.sttp.tapir:tapir-pekko-http-server_2.13:latest.release")
64+
}
65+
66+
testing {
67+
suites {
68+
val tapirTest by registering(JvmTestSuite::class) {
69+
dependencies {
70+
// this only exists to make Intellij happy since it doesn't (currently at least) understand our
71+
// inclusion of this artifact inside :testing-common
72+
compileOnly(project.dependencies.project(":testing:armeria-shaded-for-testing", configuration = "shadow"))
73+
74+
if (findProperty("testLatestDeps") as Boolean) {
75+
implementation("com.typesafe.akka:akka-http_2.13:latest.release")
76+
implementation("com.typesafe.akka:akka-stream_2.13:latest.release")
77+
implementation("com.softwaremill.sttp.tapir:tapir-pekko-http-server_2.13:latest.release")
78+
} else {
79+
implementation("org.apache.pekko:pekko-http_2.12:1.0.0")
80+
implementation("org.apache.pekko:pekko-stream_2.12:1.0.1")
81+
implementation("com.softwaremill.sttp.tapir:tapir-pekko-http-server_2.12:1.7.0")
82+
}
83+
}
84+
}
85+
}
6586
}
6687

6788
tasks {
@@ -74,6 +95,10 @@ tasks {
7495

7596
systemProperty("testLatestDeps", findProperty("testLatestDeps") as Boolean)
7697
}
98+
99+
check {
100+
dependsOn(testing.suites)
101+
}
77102
}
78103

79104
if (findProperty("testLatestDeps") as Boolean) {
@@ -82,7 +107,6 @@ if (findProperty("testLatestDeps") as Boolean) {
82107
testImplementation {
83108
exclude("org.apache.pekko", "pekko-http_2.12")
84109
exclude("org.apache.pekko", "pekko-stream_2.12")
85-
exclude("com.softwaremill.sttp.tapir", "tapir-pekko-http-server_2.12")
86110
}
87111
}
88112
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.route;
6+
package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.tapir;
77

8+
import io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.route.PekkoRouteHolder;
89
import org.apache.pekko.http.scaladsl.server.RequestContext;
910
import org.apache.pekko.http.scaladsl.server.RouteResult;
1011
import scala.Function1;
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.route;
6+
package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.tapir;
77

88
import static net.bytebuddy.matcher.ElementMatchers.named;
99
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;
Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.route;
6+
package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0.server.tapir;
77

8-
import static java.util.Arrays.asList;
8+
import static java.util.Collections.singletonList;
99

1010
import com.google.auto.service.AutoService;
1111
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
@@ -18,7 +18,12 @@ public class TapirPekkoHttpServerRouteInstrumentationModule extends Instrumentat
1818
implements ExperimentalInstrumentationModule {
1919
public TapirPekkoHttpServerRouteInstrumentationModule() {
2020
super(
21-
"pekko-http", "pekko-http-1.0", "tapir-pekko-http-server", "tapir-pekko-http-server-route");
21+
"pekko-http",
22+
"pekko-http-1.0",
23+
"pekko-http-server",
24+
"pekko-http-server-route",
25+
"tapir-pekko-http-server",
26+
"tapir-pekko-http-server-route");
2227
}
2328

2429
@Override
@@ -28,6 +33,6 @@ public String getModuleGroup() {
2833

2934
@Override
3035
public List<TypeInstrumentation> typeInstrumentations() {
31-
return asList(new TapirPathInstrumentation(), new RouteConcatenationInstrumentation());
36+
return singletonList(new TapirPathInstrumentation());
3237
}
3338
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
/*
2+
* Copyright The OpenTelemetry Authors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
package io.opentelemetry.javaagent.instrumentation.pekkohttp.v1_0
7+
8+
import io.opentelemetry.instrumentation.test.utils.PortUtils
9+
import io.opentelemetry.instrumentation.testing.junit.AgentInstrumentationExtension
10+
import io.opentelemetry.sdk.testing.assertj.{SpanDataAssert, TraceAssert}
11+
import io.opentelemetry.testing.internal.armeria.client.WebClient
12+
import io.opentelemetry.testing.internal.armeria.common.{
13+
AggregatedHttpRequest,
14+
HttpMethod
15+
}
16+
import org.apache.pekko.actor.ActorSystem
17+
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+
}
27+
import org.apache.pekko.http.scaladsl.server.Route
28+
import org.assertj.core.api.Assertions.assertThat
29+
import org.junit.jupiter.api.extension.RegisterExtension
30+
import org.junit.jupiter.api.{AfterAll, Test, TestInstance}
31+
import sttp.tapir._
32+
import sttp.tapir.server.pekkohttp.PekkoHttpServerInterpreter
33+
34+
import java.net.{URI, URISyntaxException}
35+
import java.util.function.Consumer
36+
import scala.concurrent.duration.DurationInt
37+
import scala.concurrent.{Await, Future}
38+
39+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
40+
class TapirHttpServerRouteTest {
41+
@RegisterExtension private val testing: AgentInstrumentationExtension =
42+
AgentInstrumentationExtension.create
43+
private val client: WebClient = WebClient.of()
44+
45+
implicit val system: ActorSystem = ActorSystem("my-system")
46+
47+
private def buildAddress(port: Int): URI = try
48+
new URI("http://localhost:" + port + "/")
49+
catch {
50+
case exception: URISyntaxException =>
51+
throw new IllegalStateException(exception)
52+
}
53+
54+
@Test def testSimple(): Unit = {
55+
val route = path("test") {
56+
complete("ok")
57+
}
58+
59+
test(route, "/test", "GET /test")
60+
}
61+
62+
@Test def testRoute(): Unit = {
63+
val route = concat(
64+
pathEndOrSingleSlash {
65+
complete("root")
66+
},
67+
pathPrefix("test") {
68+
concat(
69+
pathSingleSlash {
70+
complete("test")
71+
},
72+
path(IntNumber) { _ =>
73+
complete("ok")
74+
}
75+
)
76+
}
77+
)
78+
79+
test(route, "/test/1", "GET /test/*")
80+
}
81+
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 /test/4")
100+
}
101+
102+
def test(route: Route, path: String, spanName: String): Unit = {
103+
val port = PortUtils.findOpenPort
104+
val address: URI = buildAddress(port)
105+
val binding =
106+
Await.result(Http().bindAndHandle(route, "localhost", port), 10.seconds)
107+
try {
108+
val request = AggregatedHttpRequest.of(
109+
HttpMethod.GET,
110+
address.resolve(path).toString
111+
)
112+
val response = client.execute(request).aggregate.join
113+
assertThat(response.status.code).isEqualTo(200)
114+
assertThat(response.contentUtf8).isEqualTo("ok")
115+
116+
testing.waitAndAssertTraces(new Consumer[TraceAssert] {
117+
override def accept(trace: TraceAssert): Unit =
118+
trace.hasSpansSatisfyingExactly(new Consumer[SpanDataAssert] {
119+
override def accept(span: SpanDataAssert): Unit = {
120+
span.hasName(spanName)
121+
}
122+
})
123+
})
124+
} finally {
125+
binding.unbind()
126+
}
127+
}
128+
129+
@AfterAll
130+
def cleanUp(): Unit = {
131+
system.terminate()
132+
}
133+
}

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

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,8 @@ 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, ExecutionContext, Future}
34+
import scala.concurrent.Await
3535
import scala.concurrent.duration.DurationInt
36-
import sttp.tapir._
37-
import sttp.tapir.server.pekkohttp.PekkoHttpServerInterpreter
3836

3937
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
4038
class PekkoHttpServerRouteTest {
@@ -79,26 +77,6 @@ class PekkoHttpServerRouteTest {
7977
test(route, "/test/1", "GET /test/*")
8078
}
8179

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 /test/4")
100-
}
101-
10280
def test(route: Route, path: String, spanName: String): Unit = {
10381
val port = PortUtils.findOpenPort
10482
val address: URI = buildAddress(port)

0 commit comments

Comments
 (0)