|
| 1 | +package kamon.trace |
| 2 | + |
| 3 | +import kamon.context.{Context, HttpPropagation} |
| 4 | +import kamon.trace.Trace.SamplingDecision |
| 5 | +import org.scalatest.{Matchers, OptionValues, WordSpecLike} |
| 6 | + |
| 7 | +import scala.collection.mutable |
| 8 | + |
| 9 | +class W3CTraceContextSpanPropagationSpec extends WordSpecLike with Matchers with OptionValues { |
| 10 | + val traceContextPropagation = SpanPropagation.W3CTraceContext() |
| 11 | + |
| 12 | + "The TraceContext Span propagation for HTTP" should { |
| 13 | + "write the Span data into headers" in { |
| 14 | + val headersMap = mutable.Map.empty[String, String] |
| 15 | + traceContextPropagation.write(testContext(), headerWriterFromMap(headersMap)) |
| 16 | + |
| 17 | + headersMap.get("traceparent").value shouldBe "00-00000000000000000000000001020304-0000000004030201-01" |
| 18 | + headersMap.get("tracestate").value shouldBe "" |
| 19 | + } |
| 20 | + |
| 21 | + "not inject anything if there is no Span in the Context" in { |
| 22 | + val headersMap = mutable.Map.empty[String, String] |
| 23 | + traceContextPropagation.write(Context.Empty, headerWriterFromMap(headersMap)) |
| 24 | + headersMap.values shouldBe empty |
| 25 | + } |
| 26 | + |
| 27 | + "extract a RemoteSpan from incoming headers when all fields are set" in { |
| 28 | + val headersMap = Map( |
| 29 | + "traceparent" -> "00-00000000000000000000000001020304-0000000004030201-01", |
| 30 | + "tracestate" -> "2222" |
| 31 | + ) |
| 32 | + |
| 33 | + val spanContext = traceContextPropagation.read(headerReaderFromMap(headersMap), Context.Empty).get(Span.Key) |
| 34 | + spanContext.id.string shouldBe "00000000000000000000000001020304" |
| 35 | + spanContext.parentId.string shouldBe "0000000004030201" |
| 36 | + spanContext.trace.id.string shouldBe "00000000000000000000000001020304" |
| 37 | + spanContext.trace.samplingDecision shouldBe SamplingDecision.Sample |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + def headerWriterFromMap(map: mutable.Map[String, String]): HttpPropagation.HeaderWriter = new HttpPropagation.HeaderWriter { |
| 42 | + override def write(header: String, value: String): Unit = map.put(header, value) |
| 43 | + } |
| 44 | + |
| 45 | + def headerReaderFromMap(map: Map[String, String]): HttpPropagation.HeaderReader = new HttpPropagation.HeaderReader { |
| 46 | + override def read(header: String): Option[String] = { |
| 47 | + if(map.contains("fail")) |
| 48 | + sys.error("failing on purpose") |
| 49 | + |
| 50 | + map.get(header) |
| 51 | + } |
| 52 | + |
| 53 | + override def readAll(): Map[String, String] = map |
| 54 | + } |
| 55 | + |
| 56 | + def testContext(): Context = |
| 57 | + Context.of(Span.Key, Span.Remote( |
| 58 | + id = Identifier("4321", Array[Byte](4, 3, 2, 1)), |
| 59 | + parentId = Identifier("2222", Array[Byte](2, 2, 2, 2)), |
| 60 | + trace = Trace( |
| 61 | + id = Identifier("1234", Array[Byte](1, 2, 3, 4)), |
| 62 | + samplingDecision = SamplingDecision.Sample |
| 63 | + ) |
| 64 | + )) |
| 65 | + |
| 66 | + def testContextWithoutParent(): Context = |
| 67 | + Context.of(Span.Key, Span.Remote( |
| 68 | + id = Identifier("4321", Array[Byte](4, 3, 2, 1)), |
| 69 | + parentId = Identifier.Empty, |
| 70 | + trace = Trace( |
| 71 | + id = Identifier("1234", Array[Byte](1, 2, 3, 4)), |
| 72 | + samplingDecision = SamplingDecision.Sample |
| 73 | + ) |
| 74 | + )) |
| 75 | +} |
0 commit comments