From ac43c0476c239a9cf1c20e838b0fd212b20161e1 Mon Sep 17 00:00:00 2001 From: Ivan Topolnjak Date: Tue, 4 Sep 2018 18:32:36 +0200 Subject: basic testing for the HTTP server instrumentation --- .../HttpServerInstrumentationSpec.scala | 65 +++++++++++++++++++--- 1 file changed, 57 insertions(+), 8 deletions(-) (limited to 'kamon-core-tests/src/test/scala') diff --git a/kamon-core-tests/src/test/scala/kamon/instrumentation/HttpServerInstrumentationSpec.scala b/kamon-core-tests/src/test/scala/kamon/instrumentation/HttpServerInstrumentationSpec.scala index a3662127..e31382a5 100644 --- a/kamon-core-tests/src/test/scala/kamon/instrumentation/HttpServerInstrumentationSpec.scala +++ b/kamon-core-tests/src/test/scala/kamon/instrumentation/HttpServerInstrumentationSpec.scala @@ -11,8 +11,7 @@ class HttpServerInstrumentationSpec extends WordSpec with Matchers with SpanInsp "the HTTP server instrumentation" when { "configured for context propagation" should { "read context entries and tags from the incoming request" in { - val httpServer = HttpServer.from("default", port = 8080, "http.server") - val handler = httpServer.handle(fakeRequest("http://localhost:8080/", "/", "GET", Map( + val handler = httpServer().receive(fakeRequest("http://localhost:8080/", "/", "GET", Map( "context-tags" -> "tag=value;none=0011223344556677;", "custom-trace-id" -> "0011223344556677" ))) @@ -24,8 +23,7 @@ class HttpServerInstrumentationSpec extends WordSpec with Matchers with SpanInsp } "use the configured HTTP propagation channel" in { - val httpServer = HttpServer.from("default", port = 8080, "http.server") - val handler = httpServer.handle(fakeRequest("http://localhost:8080/", "/", "GET", Map( + val handler = httpServer().receive(fakeRequest("http://localhost:8080/", "/", "GET", Map( "context-tags" -> "tag=value;none=0011223344556677;", "custom-trace-id" -> "0011223344556677" ))) @@ -41,15 +39,63 @@ class HttpServerInstrumentationSpec extends WordSpec with Matchers with SpanInsp span.tag("http.url").value shouldBe "http://localhost:8080/" val responseHeaders = mutable.Map.empty[String, String] - handler.startResponse(fakeResponse(200, responseHeaders), handler.context.withTag("hello", "world")) + handler.send(fakeResponse(200, responseHeaders), handler.context.withTag("hello", "world")) } } - "when all capabilities are disabled" should { + "configured for distributed tracing" should { + "create a span representing the current HTTP operation" in { + val handler = httpServer().receive(fakeRequest("http://localhost:8080/", "/", "GET", Map.empty)) + handler.send(fakeResponse(200, mutable.Map.empty), handler.context) + + val span = inspect(handler.span) + span.tag("http.method").value shouldBe "GET" + span.tag("http.url").value shouldBe "http://localhost:8080/" + span.tag("http.status_code").value shouldBe "200" + } + + "adopt a traceID when explicitly provided" in { + val handler = httpServer().receive(fakeRequest("http://localhost:8080/", "/", "GET", Map( + "context-tags" -> "tag=value;none=0011223344556677;", + "x-correlation-id" -> "0011223344556677" + ))) + + handler.span.context().traceID.string shouldBe "0011223344556677" + } + + "record span metrics when enabled" in { + val handler = httpServer().receive(fakeRequest("http://localhost:8080/", "/", "GET", Map.empty)) + handler.send(fakeResponse(200, mutable.Map.empty), handler.context) + + val span = inspect(handler.span) + span.hasMetricsEnabled() shouldBe true + } + + "not record span metrics when disabled" in { + val handler = HttpServer.from("no-span-metrics", port = 8081, "http.server") + .receive(fakeRequest("http://localhost:8080/", "/", "GET", Map.empty)) + handler.send(fakeResponse(200, mutable.Map.empty), handler.context) + + val span = inspect(handler.span) + span.hasMetricsEnabled() shouldBe false + } + + "receive tags from context when available" in { + val handler = httpServer().receive(fakeRequest("http://localhost:8080/", "/", "GET", Map( + "context-tags" -> "tag=value;none=0011223344556677;peer=superservice;", + "custom-trace-id" -> "0011223344556677" + ))) + + val span = inspect(handler.span) + span.tag("peer").value shouldBe "superservice" + } + } + + "all capabilities are disabled" should { "not read any context from the incoming requests" in { val httpServer = HttpServer.from("noop", port = 8081, "http.server") - val handler = httpServer.handle(fakeRequest("http://localhost:8080/", "/", "GET", Map( + val handler = httpServer.receive(fakeRequest("http://localhost:8080/", "/", "GET", Map( "context-tags" -> "tag=value;none=0011223344556677;", "custom-trace-id" -> "0011223344556677" ))) @@ -59,7 +105,7 @@ class HttpServerInstrumentationSpec extends WordSpec with Matchers with SpanInsp "not create any span to represent the server request" in { val httpServer = HttpServer.from("noop", port = 8081, "http.server") - val handler = httpServer.handle(fakeRequest("http://localhost:8080/", "/", "GET", Map( + val handler = httpServer.receive(fakeRequest("http://localhost:8080/", "/", "GET", Map( "context-tags" -> "tag=value;none=0011223344556677;", "custom-trace-id" -> "0011223344556677" ))) @@ -71,6 +117,9 @@ class HttpServerInstrumentationSpec extends WordSpec with Matchers with SpanInsp } } + def httpServer(): HttpServer = HttpServer.from("default", port = 8081, "http.server") + def noopHttpServer(): HttpServer = HttpServer.from("noop", port = 8081, "http.server") + def fakeRequest(requestUrl: String, requestPath: String, requestMethod: String, headers: Map[String, String]): HttpRequest = new HttpRequest { override def url: String = requestUrl -- cgit v1.2.3