From 53a292199089ceb8197e65250bbb013f4c8b0739 Mon Sep 17 00:00:00 2001 From: lucasamoroso Date: Wed, 29 Sep 2021 20:31:51 -0300 Subject: [PATCH] update test routes --- .../src/test/resources/application.conf | 3 +- .../client/ArmeriaHttpClientTracingSpec.scala | 8 +- .../server/ArmeriaHttpServerTracingSpec.scala | 52 ++++++------- .../scala/utils/ArmeriaServerSupport.scala | 2 +- .../test/scala/utils/TestRoutesSupport.scala | 59 -------------- .../src/test/scala/utils/UserRoutes.scala | 76 +++++++++++++++++++ 6 files changed, 109 insertions(+), 91 deletions(-) delete mode 100644 instrumentation/kamon-armeria/src/test/scala/utils/TestRoutesSupport.scala create mode 100644 instrumentation/kamon-armeria/src/test/scala/utils/UserRoutes.scala diff --git a/instrumentation/kamon-armeria/src/test/resources/application.conf b/instrumentation/kamon-armeria/src/test/resources/application.conf index e69b419cc..2a8579eb9 100644 --- a/instrumentation/kamon-armeria/src/test/resources/application.conf +++ b/instrumentation/kamon-armeria/src/test/resources/application.conf @@ -1,3 +1,4 @@ kamon.instrumentation.armeria.server.tracing.operations.mappings = { - "/dummy-resources/*/other-resources/*" = "dummy-resources/{}/other-resources/{}" + "/users/*" = "/users/{}", + "/users/*/accounts/*" = "/users/{}/accounts/{}" } \ No newline at end of file diff --git a/instrumentation/kamon-armeria/src/test/scala/kamon/instrumentation/armeria/client/ArmeriaHttpClientTracingSpec.scala b/instrumentation/kamon-armeria/src/test/scala/kamon/instrumentation/armeria/client/ArmeriaHttpClientTracingSpec.scala index 654d45e83..ddb537bb9 100644 --- a/instrumentation/kamon-armeria/src/test/scala/kamon/instrumentation/armeria/client/ArmeriaHttpClientTracingSpec.scala +++ b/instrumentation/kamon-armeria/src/test/scala/kamon/instrumentation/armeria/client/ArmeriaHttpClientTracingSpec.scala @@ -32,7 +32,7 @@ class ArmeriaHttpClientTracingSpec extends WordSpec "The Armeria http client tracing instrumentation" should { "propagate the current context and generate a span around an async request" in { - val path = "/dummy" + val path = "/users" val url = s"http://$interface:$httpPort" val okSpan = Kamon.spanBuilder("ok-async-operation-span").start() @@ -70,7 +70,7 @@ class ArmeriaHttpClientTracingSpec extends WordSpec } "propagate context tags" in { - val path = "/dummy" + val path = "/users" val url = s"http://$interface:$httpPort" val okSpan = Kamon.spanBuilder("ok-span-with-extra-tags").start() @@ -109,7 +109,7 @@ class ArmeriaHttpClientTracingSpec extends WordSpec } "mark span as failed when server response with 5xx on async execution" in { - val path = "/dummy-error" + val path = "/users/error" val url = s"http://$interface:$httpPort" val okSpan = Kamon.spanBuilder("ok-async-operation-span").start() @@ -146,7 +146,7 @@ class ArmeriaHttpClientTracingSpec extends WordSpec } "add timing marks to the generated span" in { - val path = "/dummy" + val path = "/users" val url = s"http://$interface:$httpPort" val okSpan = Kamon.spanBuilder("ok-async-operation-span").start() diff --git a/instrumentation/kamon-armeria/src/test/scala/kamon/instrumentation/armeria/server/ArmeriaHttpServerTracingSpec.scala b/instrumentation/kamon-armeria/src/test/scala/kamon/instrumentation/armeria/server/ArmeriaHttpServerTracingSpec.scala index 3d267bcea..6cfb10340 100644 --- a/instrumentation/kamon-armeria/src/test/scala/kamon/instrumentation/armeria/server/ArmeriaHttpServerTracingSpec.scala +++ b/instrumentation/kamon-armeria/src/test/scala/kamon/instrumentation/armeria/server/ArmeriaHttpServerTracingSpec.scala @@ -23,7 +23,7 @@ import org.scalatest.OptionValues.convertOptionToValuable import org.scalatest.concurrent.Eventually import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach, Matchers, WordSpec} import utils.ArmeriaServerSupport.startArmeriaServer -import utils.TestEndpoints._ +import utils.Endpoints._ import scala.concurrent.duration._ @@ -50,10 +50,10 @@ class ArmeriaHttpServerTracingSpec extends WordSpec s"The Armeria $protocol server" should { "create a server Span when receiving requests" in { - val target = s"$protocol://$interface:$port/$dummyPath" - val expected = "/dummy" + val target = s"$protocol://$interface:$port/$usersEndpoint" + val expected = "/users" - val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, dummyPath)) + val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, usersEndpoint)) webClient.execute(request) eventually(timeout(3 seconds)) { @@ -68,11 +68,11 @@ class ArmeriaHttpServerTracingSpec extends WordSpec "set operation name with unhandled" when { "request path doesn't exists" in { - val target = s"$protocol://$interface:$port/$dummyNotFoundPath" + val target = s"$protocol://$interface:$port/$pathNotFoundEndpoint" val expected = "unhandled" - val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, dummyNotFoundPath)) - webClient.execute(request) + val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, pathNotFoundEndpoint)) + webClient.execute(request) eventually(timeout(3 seconds)) { val span = testSpanReporter().nextSpan().value @@ -86,13 +86,13 @@ class ArmeriaHttpServerTracingSpec extends WordSpec } } - "set operation name with path + http method" when { - "resource doesn't exist" in { - val target = s"$protocol://$interface:$port/$dummyResourceNotFoundPath" - val expected = "/dummy-resource-not-found" + "set operation name with path + http method" when { + "resource doesn't exist" in { + val target = s"$protocol://$interface:$port/$usersEndpoint/not-found" + val expected = "/users/{}" - val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, dummyResourceNotFoundPath)) - webClient.execute(request) + val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, s"$usersEndpoint/not-found")) + webClient.execute(request) eventually(timeout(3 seconds)) { val span = testSpanReporter().nextSpan().value @@ -107,9 +107,9 @@ class ArmeriaHttpServerTracingSpec extends WordSpec } "not include path variables names" in { - val expected = "dummy-resources/{}/other-resources/{}" + val expected = "/users/{}/accounts/{}" - val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, dummyMultipleResourcesPath)) + val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, userAccountEndpoint)) webClient.execute(request) eventually(timeout(3 seconds)) { @@ -119,9 +119,9 @@ class ArmeriaHttpServerTracingSpec extends WordSpec } "not fail when request url contains special regexp chars" in { - val expected = "dummy-resources/{}/other-resources/{}" + val expected = "/users/{}/accounts/{}" - val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, s"$dummyMultipleResourcesPath**")) + val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, s"$userAccountEndpoint**")) val response = webClient.execute(request).aggregate().get() eventually(timeout(3 seconds)) { @@ -132,10 +132,10 @@ class ArmeriaHttpServerTracingSpec extends WordSpec } "mark spans as failed when request fails" in { - val target = s"$protocol://$interface:$port/$dummyErrorPath" - val expected = s"/$dummyErrorPath" + val target = s"$protocol://$interface:$port/$usersEndpoint/error" + val expected = "/users/{}" - val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, dummyErrorPath)) + val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, s"$usersEndpoint/error")) webClient.execute(request) eventually(timeout(3 seconds)) { @@ -151,10 +151,10 @@ class ArmeriaHttpServerTracingSpec extends WordSpec "return a redirect status code" when { "a request to /docs is redirected to /docs/" in { - val target = s"$protocol://$interface:$port/$docs" - val expected = s"/$docs" + val target = s"$protocol://$interface:$port/$docsEndpoint" + val expected = s"/docs" - val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, docs)) + val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, docsEndpoint)) webClient.execute(request) eventually(timeout(3 seconds)) { @@ -170,10 +170,10 @@ class ArmeriaHttpServerTracingSpec extends WordSpec "return a ok status code " when { "a request to /docs/ is done" in { - val target = s"$protocol://$interface:$port/$docs/" - val expected = s"/$docs" + val target = s"$protocol://$interface:$port/$docsEndpoint/" + val expected = s"/docs" - val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, s"$docs/")) + val request = HttpRequest.of(RequestHeaders.of(HttpMethod.GET, s"$docsEndpoint/")) webClient.execute(request) eventually(timeout(3 seconds)) { diff --git a/instrumentation/kamon-armeria/src/test/scala/utils/ArmeriaServerSupport.scala b/instrumentation/kamon-armeria/src/test/scala/utils/ArmeriaServerSupport.scala index 7dbe3289d..c809893d4 100644 --- a/instrumentation/kamon-armeria/src/test/scala/utils/ArmeriaServerSupport.scala +++ b/instrumentation/kamon-armeria/src/test/scala/utils/ArmeriaServerSupport.scala @@ -29,7 +29,7 @@ object ArmeriaServerSupport { .builder() .service("/health-check", HealthCheckService.of()) .serviceUnder("/docs", new DocService()) - .annotatedService().build(TestRoutesSupport()) + .annotatedService().build(UserRoutes()) .http(InetSocketAddress.createUnresolved("localhost", port)) httpsPort.foreach { diff --git a/instrumentation/kamon-armeria/src/test/scala/utils/TestRoutesSupport.scala b/instrumentation/kamon-armeria/src/test/scala/utils/TestRoutesSupport.scala deleted file mode 100644 index 508be5020..000000000 --- a/instrumentation/kamon-armeria/src/test/scala/utils/TestRoutesSupport.scala +++ /dev/null @@ -1,59 +0,0 @@ -/* ========================================================================================= - * Copyright © 2013-2020 the kamon project - * - * Licensed under the Apache License, Version 2.0 (the "License") you may not use this file - * except in compliance with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the - * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, - * either express or implied. See the License for the specific language governing permissions - * and limitations under the License. - * ========================================================================================= - */ - -package utils - -import com.linecorp.armeria.common.{HttpRequest, HttpResponse, HttpStatus, ResponseHeaders} -import com.linecorp.armeria.server.annotation.{Get, Param} - -final class TestRoutesSupport { - @Get("/dummy") - def getDummy(req: HttpRequest): HttpResponse = { - val responseHeaders = ResponseHeaders.builder(HttpStatus.OK).add(req.headers()).build() - HttpResponse.of(responseHeaders) - } - - @Get("/dummy-resources/{resource}/other-resources/{other}") - def getResource(@Param("resource") resource: String, @Param("other") other: String): HttpResponse = { - println(s"Received a request to retrieve resource $resource and $other") - HttpResponse.of(HttpStatus.OK) - } - - @Get("/dummy-error") - def getDummyError(req: HttpRequest): HttpResponse = { - val responseHeaders = ResponseHeaders.builder(HttpStatus.INTERNAL_SERVER_ERROR).add(req.headers()).build() - HttpResponse.of(responseHeaders) - } - - @Get("/dummy-resource-not-found") - def getDummyResourceNotFound(req: HttpRequest): HttpResponse = { - val responseHeaders = ResponseHeaders.builder(HttpStatus.NOT_FOUND).add(req.headers()).build() - HttpResponse.of(responseHeaders) - } - -} - -object TestRoutesSupport { - def apply(): TestRoutesSupport = new TestRoutesSupport() -} - -object TestEndpoints { - val dummyPath = "dummy" - val dummyErrorPath = "dummy-error" - val dummyMultipleResourcesPath = "dummy-resources/a/other-resources/b" - val dummyNotFoundPath = "dummy-not-found" - val dummyResourceNotFoundPath = "dummy-resource-not-found" - val docs = "docs" -} diff --git a/instrumentation/kamon-armeria/src/test/scala/utils/UserRoutes.scala b/instrumentation/kamon-armeria/src/test/scala/utils/UserRoutes.scala new file mode 100644 index 000000000..8191eb1df --- /dev/null +++ b/instrumentation/kamon-armeria/src/test/scala/utils/UserRoutes.scala @@ -0,0 +1,76 @@ +/* ========================================================================================= + * Copyright © 2013-2020 the kamon project + * + * Licensed under the Apache License, Version 2.0 (the "License") you may not use this file + * except in compliance with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software distributed under the + * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, + * either express or implied. See the License for the specific language governing permissions + * and limitations under the License. + * ========================================================================================= + */ + +package utils + +import com.linecorp.armeria.common.{HttpRequest, HttpResponse, HttpStatus, ResponseHeaders} +import com.linecorp.armeria.server.annotation.{Blocking, Get, Param} +import kamon.Kamon +import org.slf4j.LoggerFactory + +final class UserRoutes { + val log = LoggerFactory.getLogger(this.getClass) + + @Get("/users") + def getUsers(req: HttpRequest): HttpResponse = { + log.info(s"${Kamon.currentContext().hashCode()} - Looking for users ...") + val responseHeaders = ResponseHeaders.builder(HttpStatus.OK).add(req.headers()).build() + HttpResponse.of(responseHeaders) + } + + @Get("/users/{userId}/accounts/{accountId}") + def getUserAccount(@Param("userId") userId: String, @Param("accountId") accountId: String): HttpResponse = { + log.info(s"${Kamon.currentContext().hashCode()} - Looking for user $userId account $accountId ...") + HttpResponse.of(HttpStatus.OK) + } + + @Get("/users/{userId}") + def getUser(@Param("userId") userId: String, req: HttpRequest): HttpResponse = { + log.info(s"${Kamon.currentContext().hashCode()} - Looking for user $userId ...") + userId match { + case "error" => + val responseHeaders = ResponseHeaders.builder(HttpStatus.INTERNAL_SERVER_ERROR).add(req.headers()).build() + HttpResponse.of(responseHeaders) + + case "not-found" => + val responseHeaders = ResponseHeaders.builder(HttpStatus.NOT_FOUND).add(req.headers()).build() + HttpResponse.of(responseHeaders) + + case _ => + val responseHeaders = ResponseHeaders.builder(HttpStatus.OK).add(req.headers()).build() + HttpResponse.of(responseHeaders) + } + } + + @Blocking + @Get("/users-blocking") + def getUsersBlocking(req: HttpRequest): HttpResponse = { + log.info(s"${Kamon.currentContext().hashCode()} - Looking for users under a blocking pool ...") + val responseHeaders = ResponseHeaders.builder(HttpStatus.OK).add(req.headers()).build() + HttpResponse.of(responseHeaders) + } +} + +object UserRoutes { + def apply(): UserRoutes = new UserRoutes() +} + +object Endpoints { + val usersEndpoint = "users" + val usersBlockingEndpoint = "users-blocking" + val userAccountEndpoint = "users/ABC123/accounts/BCA213" + val pathNotFoundEndpoint = "not/found" + val docsEndpoint = "docs" +}