Skip to content

Commit

Permalink
update test routes
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasamoroso committed Sep 29, 2021
1 parent f4b3ca3 commit 53a2921
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 91 deletions.
Original file line number Diff line number Diff line change
@@ -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/{}"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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._

Expand All @@ -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)) {
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand All @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/* =========================================================================================
* Copyright © 2013-2020 the kamon project <http://kamon.io/>
*
* 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"
}

0 comments on commit 53a2921

Please sign in to comment.