Skip to content

Commit c54f957

Browse files
committed
Add more test cases
1 parent 04eaa42 commit c54f957

File tree

2 files changed

+124
-38
lines changed

2 files changed

+124
-38
lines changed

lightstep/b3_propagator.py

+19-13
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
from basictracer.propagator import Propagator
66
from basictracer.context import SpanContext
7-
from opentracing import span
87
from opentracing import SpanContext as OTSpanContext
8+
from opentracing import SpanContextCorruptedException
99

1010
_LOG = getLogger(__name__)
1111
_SINGLE_HEADER = "b3"
@@ -37,7 +37,7 @@ def inject(self, span_context, carrier):
3737

3838
flags = baggage.pop(_FLAGS, None)
3939
if flags is not None:
40-
carrier[self._FLAGS] = flags
40+
carrier[_FLAGS] = flags
4141

4242
sampled = baggage.pop(_SAMPLED, None)
4343
if sampled is not None:
@@ -56,9 +56,7 @@ def inject(self, span_context, carrier):
5656
)
5757
carrier[_SAMPLED] = int(sampled)
5858

59-
if (sampled is None and flags is None) and (
60-
traceid is None or spanid is None
61-
):
59+
if sampled is flags is (traceid and spanid) is None:
6260
warn(
6361
"If not propagating only the sampling state, traceid and "
6462
"spanid must be defined"
@@ -92,30 +90,36 @@ def extract(self, carrier):
9290
baggage = {}
9391

9492
if _SINGLE_HEADER in carrier.keys():
95-
fields = carrier[_SINGLE_HEADER].split("-", 4)
93+
fields = carrier.pop(_SINGLE_HEADER).split("-", 4)
94+
baggage.update(carrier)
9695
len_fields = len(fields)
97-
if len(fields) == 1:
96+
if len_fields == 1:
9897
sampled = fields[0]
99-
elif len(fields) == 2:
98+
elif len_fields == 2:
10099
traceid, spanid = fields
101-
elif len(fields) == 3:
100+
elif len_fields == 3:
102101
traceid, spanid, sampled = fields
103102
else:
104103
traceid, spanid, sampled, parent_spanid = fields
105-
baggage[_PARENTSPANID] = parent_spanid
104+
baggage[_PARENTSPANID] = int(parent_spanid, 16)
106105
if sampled == "d":
107106
baggage[_FLAGS] = 1
108107
else:
109-
baggage[_SAMPLED] = sampled
108+
baggage[_SAMPLED] = int(sampled, 16)
110109
else:
111110
traceid = carrier.pop(_TRACEID, None)
112111
spanid = carrier.pop(_SPANID, None)
113112
parentspanid = carrier.pop(_PARENTSPANID, None)
114113
sampled = carrier.pop(_SAMPLED, None)
115114
flags = carrier.pop(_FLAGS, None)
116115

116+
if sampled is flags is (traceid and spanid) is None:
117+
118+
raise SpanContextCorruptedException()
119+
117120
if parentspanid is not None:
118-
baggage[_PARENTSPANID] = parentspanid
121+
baggage[_PARENTSPANID] = int(parentspanid, 16)
122+
119123
if flags == 1:
120124
baggage[_FLAGS] = flags
121125
if sampled is not None:
@@ -124,8 +128,10 @@ def extract(self, carrier):
124128
"the received value of x-b3-sampled"
125129
)
126130
elif sampled is not None:
127-
baggage[_SAMPLED] = sampled
131+
baggage[_SAMPLED] = int(sampled, 16)
132+
128133
baggage.update(carrier)
134+
129135
if baggage == OTSpanContext.EMPTY_BAGGAGE:
130136
baggage = None
131137
return SpanContext(

tests/b3_propagator_test.py

+105-25
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,125 @@
1-
import unittest
1+
from unittest import TestCase
2+
3+
from pytest import raises
4+
from opentracing import SpanContextCorruptedException
25

3-
import lightstep
46
from lightstep.propagation import B3
7+
from lightstep import Tracer
58

69

7-
class B3PropagatorTest(unittest.TestCase):
10+
class B3PropagatorTest(TestCase):
811
def setUp(self):
9-
self._tracer = lightstep.Tracer(
10-
periodic_flush_seconds=0,
11-
collector_host='localhost')
12+
self._tracer = Tracer(
13+
periodic_flush_seconds=0,
14+
collector_host="localhost"
15+
)
1216

1317
def tracer(self):
1418
return self._tracer
1519

1620
def tearDown(self):
1721
self._tracer.flush()
1822

19-
def testInjectExtract(self):
23+
def test_inject(self):
2024
carrier = {}
21-
span = self.tracer().start_span('Sending request')
22-
23-
span.set_baggage_item('checked', 'baggage')
25+
span = self.tracer().start_span("test_inject")
26+
span.set_baggage_item("checked", "baggage")
27+
self.tracer().inject(span.context, B3, carrier)
28+
self.assertEqual(
29+
carrier, {
30+
"x-b3-traceid": format(span.context.trace_id, "032x"),
31+
"x-b3-spanid": format(span.context.span_id, "016x"),
32+
"checked": "baggage"
33+
}
34+
)
2435

36+
carrier = {}
37+
span = self.tracer().start_span("test_inject")
38+
span.set_baggage_item("x-b3-flags", 1)
39+
span.set_baggage_item("x-b3-sampled", 0)
2540
self.tracer().inject(span.context, B3, carrier)
41+
self.assertEqual(
42+
carrier, {
43+
"x-b3-traceid": format(span.context.trace_id, "032x"),
44+
"x-b3-spanid": format(span.context.span_id, "016x"),
45+
"x-b3-flags": 1,
46+
}
47+
)
48+
49+
def test_extract_multiple_headers(self):
50+
51+
result = self.tracer().extract(
52+
B3, {
53+
"x-b3-traceid": format(12, "032x"),
54+
"x-b3-spanid": format(345, "016x"),
55+
"checked": "baggage"
56+
}
57+
)
58+
59+
self.assertEqual(12, result.trace_id)
60+
self.assertEqual(345, result.span_id)
61+
self.assertEqual({"checked": "baggage"}, result.baggage)
62+
63+
result = self.tracer().extract(
64+
B3, {
65+
"x-b3-traceid": format(12, "032x"),
66+
"x-b3-spanid": format(345, "016x"),
67+
"x-b3-flags": 1,
68+
"x-b3-sampled": 0
69+
}
70+
)
71+
72+
self.assertEqual(12, result.trace_id)
73+
self.assertEqual(345, result.span_id)
74+
self.assertEqual({"x-b3-flags": 1}, result.baggage)
2675

27-
result = self.tracer().extract(B3, carrier)
28-
self.assertEqual(span.context.span_id, result.span_id)
29-
self.assertEqual(span.context.trace_id, result.trace_id)
30-
self.assertEqual(span.context.baggage, result.baggage)
31-
self.assertEqual(span.context.sampled, result.sampled)
76+
def test_extract_single_header(self):
77+
result = self.tracer().extract(
78+
B3, {
79+
"b3": "a12-b34-1-c56",
80+
"checked": "baggage"
81+
}
82+
)
83+
self.assertEqual(2578, result.trace_id)
84+
self.assertEqual(2868, result.span_id)
85+
self.assertDictEqual(
86+
{
87+
"x-b3-sampled": 1,
88+
"x-b3-parentspanid": 3158,
89+
"checked": "baggage"
90+
}, result.baggage
91+
)
3292

33-
def single_header_extraction(self):
34-
# FIXME complete this test case
35-
input_ = 1
93+
result = self.tracer().extract(
94+
B3, {
95+
"b3": "a12-b34-d-c56",
96+
"checked": "baggage"
97+
}
98+
)
99+
self.assertEqual(2578, result.trace_id)
100+
self.assertEqual(2868, result.span_id)
101+
self.assertDictEqual(
102+
{
103+
"x-b3-flags": 1,
104+
"x-b3-parentspanid": 3158,
105+
"checked": "baggage"
106+
}, result.baggage
107+
)
36108

37-
result = self.tracer().extract(B3, bytearray(input, 'utf-8'))
38-
self.assertEqual(6397081719746291766, result.span_id)
39-
self.assertEqual(506100417967962170, result.trace_id)
40-
self.assertEqual(True, result.sampled)
41-
self.assertDictEqual(result.baggage, {"checked" : "baggage"})
109+
def test_invalid_traceid_spanid(self):
42110

111+
with raises(SpanContextCorruptedException):
112+
self.tracer().extract(
113+
B3, {
114+
"x-b3-spanid": format(345, "016x"),
115+
"checked": "baggage"
116+
}
117+
)
43118

44-
if __name__ == '__main__':
45-
unittest.main()
119+
with raises(SpanContextCorruptedException):
120+
self.tracer().extract(
121+
B3, {
122+
"x-b3-traceid": format(345, "032x"),
123+
"checked": "baggage"
124+
}
125+
)

0 commit comments

Comments
 (0)