|
| 1 | +package com.expedia.www.haystack.client.propagation; |
| 2 | + |
| 3 | +import com.expedia.www.haystack.client.SpanContext; |
| 4 | +import java.util.UUID; |
| 5 | +import org.junit.Assert; |
| 6 | +import org.junit.Test; |
| 7 | + |
| 8 | +public class TextMapPropagatorTest { |
| 9 | + @Test |
| 10 | + public void propagatorExtractsSpanContextIdentitiesAsExpected() { |
| 11 | + final KeyConvention keyConvention = new DefaultKeyConvention(); |
| 12 | + final TextMapPropagator propagator = new TextMapPropagator.Builder().withKeyConvention(keyConvention).build(); |
| 13 | + final MapBackedTextMap carrier = new MapBackedTextMap(); |
| 14 | + |
| 15 | + carrier.put(keyConvention.traceIdKey(), "8557731e-cce9-45c2-9485-1fd86f5116ca"); |
| 16 | + carrier.put(keyConvention.spanIdKey(), "30898bb0-f836-43fb-ad69-44969f15e52d"); |
| 17 | + carrier.put(keyConvention.parentIdKey(), "3a0bc1c1-504f-4f5d-907b-9b4522453bcf"); |
| 18 | + |
| 19 | + final SpanContext spanContext = propagator.extract(carrier); |
| 20 | + |
| 21 | + Assert.assertEquals("trace-id was not extracted correctly", |
| 22 | + "8557731e-cce9-45c2-9485-1fd86f5116ca", spanContext.getTraceId().toString()); |
| 23 | + Assert.assertEquals("span-id was not extracted correctly", |
| 24 | + "30898bb0-f836-43fb-ad69-44969f15e52d", spanContext.getSpanId().toString()); |
| 25 | + Assert.assertEquals("parent-id was not extracted correctly", |
| 26 | + "3a0bc1c1-504f-4f5d-907b-9b4522453bcf", spanContext.getParentId().toString()); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + public void propagatorExtractsSpanContextIdentitiesAsExpectedRegardlessOfCase() { |
| 31 | + final KeyConvention keyConvention = new DefaultKeyConvention(); |
| 32 | + final TextMapPropagator propagator = new TextMapPropagator.Builder().withKeyConvention(keyConvention).build(); |
| 33 | + final MapBackedTextMap carrier = new MapBackedTextMap(); |
| 34 | + |
| 35 | + carrier.put(keyConvention.traceIdKey().toLowerCase(), "8557731e-cce9-45c2-9485-1fd86f5116ca"); |
| 36 | + carrier.put(keyConvention.spanIdKey().toLowerCase(), "30898bb0-f836-43fb-ad69-44969f15e52d"); |
| 37 | + carrier.put(keyConvention.parentIdKey().toLowerCase(), "3a0bc1c1-504f-4f5d-907b-9b4522453bcf"); |
| 38 | + carrier.put(keyConvention.baggagePrefix().toLowerCase() + "item-1", "foo"); |
| 39 | + |
| 40 | + final SpanContext spanContext = propagator.extract(carrier); |
| 41 | + |
| 42 | + Assert.assertEquals("trace-id was not extracted correctly", |
| 43 | + "8557731e-cce9-45c2-9485-1fd86f5116ca", spanContext.getTraceId().toString()); |
| 44 | + Assert.assertEquals("span-id was not extracted correctly", |
| 45 | + "30898bb0-f836-43fb-ad69-44969f15e52d", spanContext.getSpanId().toString()); |
| 46 | + Assert.assertEquals("parent-id was not extracted correctly", |
| 47 | + "3a0bc1c1-504f-4f5d-907b-9b4522453bcf", spanContext.getParentId().toString()); |
| 48 | + Assert.assertEquals("baggage item was not extracted correctly", |
| 49 | + "foo", spanContext.getBaggage().get("item-1")); |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + public void propagatorInjectsSpanContextIdentitiesAsExpected() { |
| 54 | + final KeyConvention keyConvention = new DefaultKeyConvention(); |
| 55 | + final TextMapPropagator propagator = new TextMapPropagator.Builder().withKeyConvention(keyConvention).build(); |
| 56 | + final MapBackedTextMap carrier = new MapBackedTextMap(); |
| 57 | + |
| 58 | + final SpanContext spanContext = new SpanContext(UUID.fromString("8557731e-cce9-45c2-9485-1fd86f5116ca"), |
| 59 | + UUID.fromString("30898bb0-f836-43fb-ad69-44969f15e52d"), |
| 60 | + UUID.fromString("3a0bc1c1-504f-4f5d-907b-9b4522453bcf")); |
| 61 | + |
| 62 | + propagator.inject(spanContext, carrier); |
| 63 | + |
| 64 | + Assert.assertEquals("trace-id was not injected correctly", |
| 65 | + "8557731e-cce9-45c2-9485-1fd86f5116ca", carrier.get(keyConvention.traceIdKey())); |
| 66 | + Assert.assertEquals("span-id was not injected correctly", |
| 67 | + "30898bb0-f836-43fb-ad69-44969f15e52d", carrier.get(keyConvention.spanIdKey())); |
| 68 | + Assert.assertEquals("parent-id was not injected correctly", |
| 69 | + "3a0bc1c1-504f-4f5d-907b-9b4522453bcf", carrier.get(keyConvention.parentIdKey())); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void builderUsesTheProvidedCodexForKeyAndValue() { |
| 74 | + final KeyConvention keyConvention = new DefaultKeyConvention(); |
| 75 | + final TextMapCodex codex = new OneAppendingCodex(); |
| 76 | + final TextMapPropagator propagator = new TextMapPropagator.Builder() |
| 77 | + .withKeyConvention(keyConvention) |
| 78 | + .withCodex(codex) |
| 79 | + .build(); |
| 80 | + final MapBackedTextMap carrier = new MapBackedTextMap(); |
| 81 | + |
| 82 | + final SpanContext spanContext = new SpanContext(UUID.fromString("8557731e-cce9-45c2-9485-1fd86f5116ca"), |
| 83 | + UUID.fromString("30898bb0-f836-43fb-ad69-44969f15e52d"), |
| 84 | + UUID.fromString("3a0bc1c1-504f-4f5d-907b-9b4522453bcf")); |
| 85 | + |
| 86 | + propagator.inject(spanContext, carrier); |
| 87 | + |
| 88 | + Assert.assertEquals("trace-id was not injected correctly", |
| 89 | + "1-8557731e-cce9-45c2-9485-1fd86f5116ca", |
| 90 | + carrier.get("1-" + keyConvention.traceIdKey())); |
| 91 | + Assert.assertEquals("span-id was not injected correctly", |
| 92 | + "1-30898bb0-f836-43fb-ad69-44969f15e52d", |
| 93 | + carrier.get("1-" + keyConvention.spanIdKey())); |
| 94 | + Assert.assertEquals("parent-id was not injected correctly", |
| 95 | + "1-3a0bc1c1-504f-4f5d-907b-9b4522453bcf", |
| 96 | + carrier.get("1-" + keyConvention.parentIdKey())); |
| 97 | + } |
| 98 | + |
| 99 | + private class OneAppendingCodex extends TextMapCodex { |
| 100 | + @Override |
| 101 | + public String encode(String value) { |
| 102 | + return "1-" + value; |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public String decode(String value) { |
| 107 | + return value.substring(2); |
| 108 | + } |
| 109 | + } |
| 110 | +} |
0 commit comments