diff options
author | Ivan Topolnjak <ivantopo@gmail.com> | 2018-09-03 15:37:14 +0200 |
---|---|---|
committer | Ivan Topolnjak <ivantopo@gmail.com> | 2018-09-03 15:37:14 +0200 |
commit | c487c51a54e67944c80cf2aecc63ac8158bf99a6 (patch) | |
tree | d88669c3445fcdee659b62b0a8352f5ad50c1e9a /kamon-core-tests/src/test/scala | |
parent | d30ff29cdb5f94be34163d851d71716a316bdf10 (diff) | |
download | Kamon-c487c51a54e67944c80cf2aecc63ac8158bf99a6.tar.gz Kamon-c487c51a54e67944c80cf2aecc63ac8158bf99a6.tar.bz2 Kamon-c487c51a54e67944c80cf2aecc63ac8158bf99a6.zip |
wip on the HttpServer instrumentation
Diffstat (limited to 'kamon-core-tests/src/test/scala')
-rw-r--r-- | kamon-core-tests/src/test/scala/kamon/context/HttpPropagationSpec.scala | 30 | ||||
-rw-r--r-- | kamon-core-tests/src/test/scala/kamon/instrumentation/HttpServerInstrumentationSpec.scala | 88 |
2 files changed, 103 insertions, 15 deletions
diff --git a/kamon-core-tests/src/test/scala/kamon/context/HttpPropagationSpec.scala b/kamon-core-tests/src/test/scala/kamon/context/HttpPropagationSpec.scala index 08d0b691..44165b98 100644 --- a/kamon-core-tests/src/test/scala/kamon/context/HttpPropagationSpec.scala +++ b/kamon-core-tests/src/test/scala/kamon/context/HttpPropagationSpec.scala @@ -12,7 +12,7 @@ class HttpPropagationSpec extends WordSpec with Matchers with OptionValues { "The HTTP Context Propagation" when { "reading from incoming requests" should { "return an empty context if there are no tags nor keys" in { - val context = httpPropagation.read(headerReaderFromMap(Map.empty)) + val context = httpPropagation.readContext(headerReaderFromMap(Map.empty)) context.tags shouldBe empty context.entries shouldBe empty } @@ -22,7 +22,7 @@ class HttpPropagationSpec extends WordSpec with Matchers with OptionValues { "x-content-tags" -> "hello=world;correlation=1234", "x-mapped-tag" -> "value" ) - val context = httpPropagation.read(headerReaderFromMap(headers)) + val context = httpPropagation.readContext(headerReaderFromMap(headers)) context.tags should contain only( "hello" -> "world", "correlation" -> "1234", @@ -32,7 +32,7 @@ class HttpPropagationSpec extends WordSpec with Matchers with OptionValues { "handle errors when reading HTTP headers" in { val headers = Map("fail" -> "") - val context = httpPropagation.read(headerReaderFromMap(headers)) + val context = httpPropagation.readContext(headerReaderFromMap(headers)) context.tags shouldBe empty context.entries shouldBe empty } @@ -44,7 +44,7 @@ class HttpPropagationSpec extends WordSpec with Matchers with OptionValues { "integer-header" -> "123" ) - val context = httpPropagation.read(headerReaderFromMap(headers)) + val context = httpPropagation.readContext(headerReaderFromMap(headers)) context.get(HttpPropagationSpec.StringKey) shouldBe "hey" context.get(HttpPropagationSpec.IntegerKey) shouldBe 123 context.get(HttpPropagationSpec.OptionalKey) shouldBe empty @@ -66,7 +66,7 @@ class HttpPropagationSpec extends WordSpec with Matchers with OptionValues { def propagationWritingTests(direction: Direction.Write) = { "not write anything if the context is empty" in { val headers = mutable.Map.empty[String, String] - httpPropagation.write(Context.Empty, headerWriterFromMap(headers), direction) + httpPropagation.writeContext(Context.Empty, headerWriterFromMap(headers), direction) headers shouldBe empty } @@ -77,7 +77,7 @@ class HttpPropagationSpec extends WordSpec with Matchers with OptionValues { "mappedTag" -> "value" )) - httpPropagation.write(context, headerWriterFromMap(headers), direction) + httpPropagation.writeContext(context, headerWriterFromMap(headers), direction) headers should contain only( "x-content-tags" -> "hello=world;", "x-mapped-tag" -> "value" @@ -91,7 +91,7 @@ class HttpPropagationSpec extends WordSpec with Matchers with OptionValues { HttpPropagationSpec.IntegerKey, 42, ) - httpPropagation.write(context, headerWriterFromMap(headers), direction) + httpPropagation.writeContext(context, headerWriterFromMap(headers), direction) headers should contain only( "string-header" -> "out-we-go" ) @@ -123,7 +123,7 @@ class HttpPropagationSpec extends WordSpec with Matchers with OptionValues { def headerReaderFromMap(map: Map[String, String]): HttpPropagation.HeaderReader = new HttpPropagation.HeaderReader { - override def read(header: String): Option[String] = { + override def readHeader(header: String): Option[String] = { if(map.get("fail").nonEmpty) sys.error("failing on purpose") @@ -132,7 +132,7 @@ class HttpPropagationSpec extends WordSpec with Matchers with OptionValues { } def headerWriterFromMap(map: mutable.Map[String, String]): HttpPropagation.HeaderWriter = new HttpPropagation.HeaderWriter { - override def write(header: String, value: String): Unit = map.put(header, value) + override def writeHeader(header: String, value: String): Unit = map.put(header, value) } } @@ -146,20 +146,20 @@ object HttpPropagationSpec { class StringEntryCodec extends HttpPropagation.EntryReader with HttpPropagation.EntryWriter { private val HeaderName = "string-header" - override def read(reader: HttpPropagation.HeaderReader, context: Context): Context = { - reader.read(HeaderName) + override def readEntry(reader: HttpPropagation.HeaderReader, context: Context): Context = { + reader.readHeader(HeaderName) .map(v => context.withKey(StringKey, v)) .getOrElse(context) } - override def write(context: Context, writer: HttpPropagation.HeaderWriter, direction: Direction.Write): Unit = { - Option(context.get(StringKey)).foreach(v => writer.write(HeaderName, v)) + override def writeEntry(context: Context, writer: HttpPropagation.HeaderWriter, direction: Direction.Write): Unit = { + Option(context.get(StringKey)).foreach(v => writer.writeHeader(HeaderName, v)) } } class IntegerEntryCodec extends HttpPropagation.EntryReader { - override def read(reader: HttpPropagation.HeaderReader, context: Context): Context = { - reader.read("integer-header") + override def readEntry(reader: HttpPropagation.HeaderReader, context: Context): Context = { + reader.readHeader("integer-header") .map(v => context.withKey(IntegerKey, v.toInt)) .getOrElse(context) diff --git a/kamon-core-tests/src/test/scala/kamon/instrumentation/HttpServerInstrumentationSpec.scala b/kamon-core-tests/src/test/scala/kamon/instrumentation/HttpServerInstrumentationSpec.scala new file mode 100644 index 00000000..a3662127 --- /dev/null +++ b/kamon-core-tests/src/test/scala/kamon/instrumentation/HttpServerInstrumentationSpec.scala @@ -0,0 +1,88 @@ +package kamon.instrumentation + +import kamon.context.Context +import kamon.testkit.SpanInspection +import org.scalatest.{Matchers, OptionValues, WordSpec} + +import scala.collection.mutable + +class HttpServerInstrumentationSpec extends WordSpec with Matchers with SpanInspection with OptionValues { + + "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( + "context-tags" -> "tag=value;none=0011223344556677;", + "custom-trace-id" -> "0011223344556677" + ))) + + handler.context.tags should contain only( + "tag" -> "value", + "none" -> "0011223344556677" + ) + } + + "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( + "context-tags" -> "tag=value;none=0011223344556677;", + "custom-trace-id" -> "0011223344556677" + ))) + + handler.context.tags should contain only( + "tag" -> "value", + "none" -> "0011223344556677" + ) + + val span = inspect(handler.span) + span.context().traceID.string shouldNot be("0011223344556677") + span.tag("http.method").value shouldBe "GET" + 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")) + + } + } + + "when 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( + "context-tags" -> "tag=value;none=0011223344556677;", + "custom-trace-id" -> "0011223344556677" + ))) + + handler.context shouldBe Context.Empty + } + + "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( + "context-tags" -> "tag=value;none=0011223344556677;", + "custom-trace-id" -> "0011223344556677" + ))) + + handler.span.isEmpty() shouldBe true + } + + "not record any HTTP server metrics" is (pending) + } + } + + def fakeRequest(requestUrl: String, requestPath: String, requestMethod: String, headers: Map[String, String]): HttpRequest = + new HttpRequest { + override def url: String = requestUrl + override def path: String = requestPath + override def method: String = requestMethod + override def readHeader(header: String): Option[String] = headers.get(header) + } + + def fakeResponse(responseStatusCode: Int, headers: mutable.Map[String, String]): HttpResponse.Writable[HttpResponse] = + new HttpResponse.Writable[HttpResponse] { + override def statusCode: Int = responseStatusCode + override def writeHeader(header: String, value: String): Unit = headers.put(header, value) + override def build(): HttpResponse = this + } +} |