|
1 | 1 | import unittest
|
2 | 2 | from opentracing import SpanContextCorruptedException
|
3 | 3 | from haystack.text_propagator import TextPropagator
|
| 4 | +from haystack.propagator import PropagatorOpts |
| 5 | +from haystack.constants import ( |
| 6 | + TRACE_ID, |
| 7 | + SPAN_ID, |
| 8 | + PARENT_SPAN_ID, |
| 9 | + BAGGAGE_PREFIX, |
| 10 | +) |
4 | 11 |
|
5 | 12 |
|
6 | 13 | class TextPropagatorTest(unittest.TestCase):
|
7 | 14 |
|
8 | 15 | def setUp(self):
|
9 | 16 | self.propagator = TextPropagator()
|
10 | 17 |
|
11 |
| - def test_message_id_in_carrier_should_be_extracted_as_span_id(self): |
| 18 | + def test_span_id_without_trace_id_throws_exception(self): |
| 19 | + carrier = {"Span-ID": "123456", "Foo-ID": "1234567"} |
| 20 | + |
| 21 | + self.assertRaises(SpanContextCorruptedException, |
| 22 | + self.propagator.extract, carrier) |
| 23 | + |
| 24 | + def test_trace_id_without_span_id_throws_exception(self): |
| 25 | + carrier = {"Trace-ID": "123456", "Foo-ID": "1234567"} |
| 26 | + |
| 27 | + self.assertRaises(SpanContextCorruptedException, |
| 28 | + self.propagator.extract, carrier) |
| 29 | + |
| 30 | + def test_no_context_in_carrier_returns_none(self): |
| 31 | + span_id = "1234" |
| 32 | + carrier = {"Not-A-Message-ID": span_id, "Not-A-Trace-ID": "123456"} |
| 33 | + |
| 34 | + ctx = self.propagator.extract(carrier) |
| 35 | + |
| 36 | + self.assertIsNone(ctx) |
| 37 | + |
| 38 | + def test_lowercase_headers_are_still_extracted(self): |
| 39 | + pass |
| 40 | + |
| 41 | + def test_default_propagator_options_injected_and_context_is_extracted(self): |
12 | 42 | span_id = "1234"
|
13 | 43 | parent_id = "4321"
|
14 | 44 | trace_id = "1212"
|
15 |
| - carrier = {"Message-ID": span_id, "Parent-Message-ID": parent_id, |
16 |
| - "Trace-ID": trace_id} |
| 45 | + baggage = {"Item1": "Value1", "Item2": "Value2"} |
| 46 | + carrier = {TRACE_ID: trace_id, SPAN_ID: span_id, PARENT_SPAN_ID: parent_id} |
| 47 | + for key, value in baggage.items(): |
| 48 | + carrier[BAGGAGE_PREFIX + key] = value |
17 | 49 |
|
18 | 50 | ctx = self.propagator.extract(carrier)
|
19 | 51 |
|
20 | 52 | self.assertEqual(ctx.span_id, span_id)
|
21 | 53 | self.assertEqual(ctx.parent_id, parent_id)
|
22 | 54 | self.assertEqual(ctx.trace_id, trace_id)
|
| 55 | + self.assertDictEqual(ctx.baggage, baggage) |
23 | 56 |
|
24 |
| - def test_corrupted_context_throws_exception(self): |
| 57 | + def test_custom_propagator_options_are_injected_and_context_is_extracted(self): |
| 58 | + propagator = TextPropagator(PropagatorOpts("X-Trace-ID", "X-Span-ID", |
| 59 | + "X-Parent-ID", "X-baggage-")) |
25 | 60 | span_id = "1234"
|
26 |
| - carrier = {"Message-ID": span_id, "Span-ID": "123456", |
27 |
| - "Trace-ID": "1234567"} # two span id's are invalid |
28 |
| - |
29 |
| - self.assertRaises(SpanContextCorruptedException, self.propagator.extract, carrier) |
30 |
| - |
31 |
| - def test_no_context_in_carrier_returns_none(self): |
32 |
| - span_id = "1234" |
33 |
| - carrier = {"Not-A-Message-ID": span_id, "Not-A-Trace-ID": "123456"} |
| 61 | + parent_id = "4321" |
| 62 | + trace_id = "1212" |
| 63 | + carrier = {"X-Span-ID": span_id, "X-Parent-ID": parent_id, |
| 64 | + "X-Trace-ID": trace_id} |
| 65 | + baggage = {"Item1": "Value1", "Item2": "Value2"} |
| 66 | + for key, value in baggage.items(): |
| 67 | + carrier["X-baggage-" + key] = value |
34 | 68 |
|
35 |
| - ctx = self.propagator.extract(carrier) |
| 69 | + ctx = propagator.extract(carrier) |
36 | 70 |
|
37 |
| - self.assertIsNone(ctx) |
| 71 | + self.assertEqual(ctx.span_id, span_id) |
| 72 | + self.assertEqual(ctx.parent_id, parent_id) |
| 73 | + self.assertEqual(ctx.trace_id, trace_id) |
38 | 74 |
|
39 | 75 |
|
40 | 76 | if __name__ == "__main__":
|
|
0 commit comments