Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 8cf88d2

Browse files
aberresreyang
authored andcommitted
Allow to pass x-b3-sampled as string (#595)
While x-b3-sampled should be '0' or '1' some implementations pass a string like 'True' or 'true'.
1 parent 817249a commit 8cf88d2

File tree

2 files changed

+39
-18
lines changed

2 files changed

+39
-18
lines changed

opencensus/trace/propagation/b3_format.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ def from_headers(self, headers):
6262
sampled = headers.get(_SAMPLED_KEY)
6363

6464
if sampled is not None:
65-
if len(sampled) != 1:
66-
return SpanContext(from_header=False)
67-
68-
sampled = sampled in ('1', 'd')
65+
# The specification encodes an enabled tracing decision as "1".
66+
# In the wild pre-standard implementations might still send "true".
67+
# "d" is set in the single header case when debugging is enabled.
68+
sampled = sampled.lower() in ('1', 'd', 'true')
6969
else:
7070
# If there's no incoming sampling decision, it was deferred to us.
7171
# Even though we set it to False here, we might still sample

tests/unit/trace/propagation/test_b3_format.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,44 @@ def test_from_headers_no_headers(self):
3030
def test_from_headers_keys_exist(self):
3131
test_trace_id = '6e0c63257de34c92bf9efcd03927272e'
3232
test_span_id = '00f067aa0ba902b7'
33-
test_sampled = '1'
3433

35-
headers = {
36-
b3_format._TRACE_ID_KEY: test_trace_id,
37-
b3_format._SPAN_ID_KEY: test_span_id,
38-
b3_format._SAMPLED_KEY: test_sampled,
39-
}
34+
for test_sampled in ['1', 'True', 'true', 'd']:
35+
headers = {
36+
b3_format._TRACE_ID_KEY: test_trace_id,
37+
b3_format._SPAN_ID_KEY: test_span_id,
38+
b3_format._SAMPLED_KEY: test_sampled,
39+
}
4040

41-
propagator = b3_format.B3FormatPropagator()
42-
span_context = propagator.from_headers(headers)
41+
propagator = b3_format.B3FormatPropagator()
42+
span_context = propagator.from_headers(headers)
4343

44-
self.assertEqual(span_context.trace_id, test_trace_id)
45-
self.assertEqual(span_context.span_id, test_span_id)
46-
self.assertEqual(
47-
span_context.trace_options.enabled,
48-
bool(test_sampled)
49-
)
44+
self.assertEqual(span_context.trace_id, test_trace_id)
45+
self.assertEqual(span_context.span_id, test_span_id)
46+
self.assertEqual(
47+
span_context.trace_options.enabled,
48+
True
49+
)
50+
51+
def test_from_headers_keys_exist_disabled_sampling(self):
52+
test_trace_id = '6e0c63257de34c92bf9efcd03927272e'
53+
test_span_id = '00f067aa0ba902b7'
54+
55+
for test_sampled in ['0', 'False', 'false', None]:
56+
headers = {
57+
b3_format._TRACE_ID_KEY: test_trace_id,
58+
b3_format._SPAN_ID_KEY: test_span_id,
59+
b3_format._SAMPLED_KEY: test_sampled,
60+
}
61+
62+
propagator = b3_format.B3FormatPropagator()
63+
span_context = propagator.from_headers(headers)
64+
65+
self.assertEqual(span_context.trace_id, test_trace_id)
66+
self.assertEqual(span_context.span_id, test_span_id)
67+
self.assertEqual(
68+
span_context.trace_options.enabled,
69+
False
70+
)
5071

5172
def test_from_headers_keys_not_exist(self):
5273
propagator = b3_format.B3FormatPropagator()

0 commit comments

Comments
 (0)