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

Commit a86e5c8

Browse files
mhinderysongy23
authored andcommitted
Span id encoding (#719)
1 parent c198151 commit a86e5c8

File tree

6 files changed

+72
-14
lines changed

6 files changed

+72
-14
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unreleased
44

5+
- Fix cloud format propagator to use decimal span_id encoding instead of hex
6+
([#719](https://github.com/census-instrumentation/opencensus-python/pull/719))
7+
8+
59
## 0.6.0
610
Released 2019-05-31
711

opencensus/trace/propagation/google_cloud_format.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from opencensus.trace.trace_options import TraceOptions
2020

2121
_TRACE_CONTEXT_HEADER_NAME = 'X-Cloud-Trace-Context'
22-
_TRACE_CONTEXT_HEADER_FORMAT = r'([0-9a-f]{32})(\/([0-9a-f]{16}))?(;o=(\d+))?'
22+
_TRACE_CONTEXT_HEADER_FORMAT = r'([0-9a-f]{32})(\/([\d]{0,20}))?(;o=(\d+))?'
2323
_TRACE_CONTEXT_HEADER_RE = re.compile(_TRACE_CONTEXT_HEADER_FORMAT)
2424
_TRACE_ID_DELIMETER = '/'
2525
_SPAN_ID_DELIMETER = ';'
@@ -62,6 +62,9 @@ def from_header(self, header):
6262
if trace_options is None:
6363
trace_options = 1
6464

65+
if span_id:
66+
span_id = '{:016x}'.format(int(span_id))
67+
6568
span_context = SpanContext(
6669
trace_id=trace_id,
6770
span_id=span_id,
@@ -88,7 +91,6 @@ def from_headers(self, headers):
8891
header = headers.get(_TRACE_CONTEXT_HEADER_NAME)
8992
if header is None:
9093
return SpanContext()
91-
header = str(header.encode('utf-8'))
9294
return self.from_header(header)
9395

9496
def to_header(self, span_context):
@@ -107,7 +109,7 @@ def to_header(self, span_context):
107109

108110
header = '{}/{};o={}'.format(
109111
trace_id,
110-
span_id,
112+
int(span_id, 16),
111113
int(trace_options))
112114
return header
113115

tests/system/trace/basic_trace/basic_trace_system_test.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def test_tracer(self):
3333
span_id = '6e0c63257de34c92'
3434
trace_option = 1
3535

36-
trace_header = '{}/{};o={}'.format(trace_id, span_id, trace_option)
36+
trace_header = '{}/{};o={}'.format(
37+
trace_id, int(span_id, 16), trace_option)
3738

3839
sampler = samplers.AlwaysOnSampler()
3940
exporter = file_exporter.FileExporter()

tests/system/trace/django/django_system_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def generate_header():
4343
span_id = uuid.uuid4().hex[:16]
4444
trace_option = 1
4545

46-
header = '{}/{};o={}'.format(trace_id, span_id, trace_option)
46+
header = '{}/{};o={}'.format(trace_id, int(span_id, 16), trace_option)
4747

4848
return trace_id, span_id, header
4949

@@ -74,7 +74,7 @@ def setUp(self):
7474

7575
self.headers_trace = {
7676
'x-cloud-trace-context':
77-
'{}/{};o={}'.format(self.trace_id, self.span_id, 1)
77+
'{}/{};o={}'.format(self.trace_id, int(self.span_id, 16), 1)
7878
}
7979

8080
# Wait the application to start

tests/system/trace/flask/flask_system_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def generate_header():
4343
span_id = uuid.uuid4().hex[:16]
4444
trace_option = 1
4545

46-
header = '{}/{};o={}'.format(trace_id, span_id, trace_option)
46+
header = '{}/{};o={}'.format(trace_id, int(span_id, 16), trace_option)
4747

4848
return trace_id, span_id, header
4949

@@ -73,7 +73,7 @@ def setUp(self):
7373

7474
self.headers_trace = {
7575
'X-Cloud-Trace-Context':
76-
'{}/{};o={}'.format(self.trace_id, self.span_id, 1)
76+
'{}/{};o={}'.format(self.trace_id, int(self.span_id, 16), 1)
7777
}
7878

7979
# Wait the application to start

tests/unit/trace/propagation/test_google_cloud_format.py

Lines changed: 57 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_header_type_error(self):
5353

5454
def test_header_match(self):
5555
# Trace option is not enabled.
56-
header = '6e0c63257de34c92bf9efcd03927272e/00f067aa0ba902b7;o=0'
56+
header = '6e0c63257de34c92bf9efcd03927272e/67667974448284343;o=0'
5757
expected_trace_id = '6e0c63257de34c92bf9efcd03927272e'
5858
expected_span_id = '00f067aa0ba902b7'
5959

@@ -65,7 +65,7 @@ def test_header_match(self):
6565
self.assertFalse(span_context.trace_options.enabled)
6666

6767
# Trace option is enabled.
68-
header = '6e0c63257de34c92bf9efcd03927272e/00f067aa0ba902b7;o=1'
68+
header = '6e0c63257de34c92bf9efcd03927272e/67667974448284343;o=1'
6969
expected_trace_id = '6e0c63257de34c92bf9efcd03927272e'
7070
expected_span_id = '00f067aa0ba902b7'
7171

@@ -76,8 +76,58 @@ def test_header_match(self):
7676
self.assertEqual(span_context.span_id, expected_span_id)
7777
self.assertTrue(span_context.trace_options.enabled)
7878

79+
def test_header_match_no_span_id(self):
80+
# Trace option is not enabled.
81+
header = '6e0c63257de34c92bf9efcd03927272e;o=0'
82+
expected_trace_id = '6e0c63257de34c92bf9efcd03927272e'
83+
expected_span_id = None
84+
85+
propagator = google_cloud_format.GoogleCloudFormatPropagator()
86+
span_context = propagator.from_header(header)
87+
88+
self.assertEqual(span_context.trace_id, expected_trace_id)
89+
self.assertEqual(span_context.span_id, expected_span_id)
90+
self.assertFalse(span_context.trace_options.enabled)
91+
92+
# Trace option is enabled.
93+
header = '6e0c63257de34c92bf9efcd03927272e;o=1'
94+
expected_trace_id = '6e0c63257de34c92bf9efcd03927272e'
95+
expected_span_id = None
96+
97+
propagator = google_cloud_format.GoogleCloudFormatPropagator()
98+
span_context = propagator.from_header(header)
99+
100+
self.assertEqual(span_context.trace_id, expected_trace_id)
101+
self.assertEqual(span_context.span_id, expected_span_id)
102+
self.assertTrue(span_context.trace_options.enabled)
103+
104+
def test_header_match_empty_span_id(self):
105+
# Trace option is not enabled.
106+
header = '6e0c63257de34c92bf9efcd03927272e/;o=0'
107+
expected_trace_id = '6e0c63257de34c92bf9efcd03927272e'
108+
expected_span_id = None
109+
110+
propagator = google_cloud_format.GoogleCloudFormatPropagator()
111+
span_context = propagator.from_header(header)
112+
113+
self.assertEqual(span_context.trace_id, expected_trace_id)
114+
self.assertEqual(span_context.span_id, expected_span_id)
115+
self.assertFalse(span_context.trace_options.enabled)
116+
117+
# Trace option is enabled.
118+
header = '6e0c63257de34c92bf9efcd03927272e/;o=1'
119+
expected_trace_id = '6e0c63257de34c92bf9efcd03927272e'
120+
expected_span_id = None
121+
122+
propagator = google_cloud_format.GoogleCloudFormatPropagator()
123+
span_context = propagator.from_header(header)
124+
125+
self.assertEqual(span_context.trace_id, expected_trace_id)
126+
self.assertEqual(span_context.span_id, expected_span_id)
127+
self.assertTrue(span_context.trace_options.enabled)
128+
79129
def test_header_match_no_option(self):
80-
header = '6e0c63257de34c92bf9efcd03927272e/00f067aa0ba902b7'
130+
header = '6e0c63257de34c92bf9efcd03927272e/67667974448284343'
81131
expected_trace_id = '6e0c63257de34c92bf9efcd03927272e'
82132
expected_span_id = '00f067aa0ba902b7'
83133

@@ -101,7 +151,7 @@ def test_headers_match(self):
101151
# Trace option is enabled.
102152
headers = {
103153
'X-Cloud-Trace-Context':
104-
'6e0c63257de34c92bf9efcd03927272e/00f067aa0ba902b7;o=1',
154+
'6e0c63257de34c92bf9efcd03927272e/67667974448284343;o=1',
105155
}
106156
expected_trace_id = '6e0c63257de34c92bf9efcd03927272e'
107157
expected_span_id = '00f067aa0ba902b7'
@@ -128,7 +178,7 @@ def test_to_header(self):
128178

129179
header = propagator.to_header(span_context)
130180
expected_header = '{}/{};o={}'.format(
131-
trace_id, span_id, 1)
181+
trace_id, int(span_id, 16), 1)
132182

133183
self.assertEqual(header, expected_header)
134184

@@ -147,7 +197,8 @@ def test_to_headers(self):
147197

148198
headers = propagator.to_headers(span_context)
149199
expected_headers = {
150-
'X-Cloud-Trace-Context': '{}/{};o={}'.format(trace_id, span_id, 1),
200+
'X-Cloud-Trace-Context': '{}/{};o={}'.format(
201+
trace_id, int(span_id, 16), 1),
151202
}
152203

153204
self.assertEqual(headers, expected_headers)

0 commit comments

Comments
 (0)