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

Commit 8de87b0

Browse files
reyangSergeyKanzhelev
authored andcommitted
add tracestate to spancontext (#259)
1 parent beac0c2 commit 8de87b0

5 files changed

Lines changed: 50 additions & 14 deletions

File tree

opencensus/trace/span.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@ def __init__(
145145
self._child_spans = []
146146
self.context_tracer = context_tracer
147147
self.span_kind = span_kind
148+
for callback in Span._on_create_callbacks:
149+
callback(self)
150+
151+
_on_create_callbacks = []
152+
153+
@staticmethod
154+
def on_create(callback):
155+
Span._on_create_callbacks.append(callback)
148156

149157
@property
150158
def children(self):

opencensus/trace/span_context.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ def __init__(
5858
trace_id=None,
5959
span_id=None,
6060
trace_options=None,
61+
tracestate=None,
6162
from_header=False):
6263
if trace_id is None:
6364
trace_id = generate_trace_id()
@@ -69,21 +70,22 @@ def __init__(
6970
self.trace_id = self._check_trace_id(trace_id)
7071
self.span_id = self._check_span_id(span_id)
7172
self.trace_options = trace_options
73+
self.tracestate = tracestate
7274

73-
def __str__(self):
74-
"""Returns a string form of the SpanContext. This is the format of
75-
the Trace Context Header and should be forwarded to downstream
76-
requests as the X-Cloud-Trace-Context header.
75+
def __repr__(self):
76+
"""Returns a string form of the SpanContext.
7777
7878
:rtype: str
7979
:returns: String form of the SpanContext.
8080
"""
81-
enabled = self.trace_options.enabled
82-
header = '{}/{};o={}'.format(
81+
fmt = '{}(trace_id={}, span_id={}, trace_options={}, tracestate={})'
82+
return fmt.format(
83+
type(self).__name__,
8384
self.trace_id,
8485
self.span_id,
85-
int(enabled))
86-
return header
86+
self.trace_options,
87+
self.tracestate,
88+
)
8789

8890
def _check_span_id(self, span_id):
8991
"""Check the format of the span_id to ensure it is 16-character hex

opencensus/trace/trace_options.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ def check_trace_options(self, trace_options_byte):
4444

4545
return trace_options_byte
4646

47+
def __repr__(self):
48+
fmt = '{}(enabled={})'
49+
return fmt.format(
50+
type(self).__name__,
51+
self.get_enabled,
52+
)
53+
4754
@property
4855
def get_enabled(self):
4956
"""Get the last bit from the trace options which is the enabled field.

tests/unit/trace/test_span.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,21 @@ def test_finish(self):
212212
span.finish()
213213
self.assertIsNotNone(span.end_time)
214214

215+
def test_on_create(self):
216+
from opencensus.trace.span import Span
217+
self.on_create_called = False
218+
span = self._make_one('span1')
219+
self.assertFalse(self.on_create_called)
220+
try:
221+
@Span.on_create
222+
def callback(span):
223+
self.on_create_called = True
224+
span = self._make_one('span2')
225+
finally:
226+
Span._on_create_callbacks = []
227+
self.assertTrue(self.on_create_called)
228+
del self.on_create_called
229+
215230
def test___iter__(self):
216231
root_span_name = 'root_span_name'
217232
child1_span_name = 'child1_span_name'

tests/unit/trace/test_span_context.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import unittest
1616
from opencensus.trace import span_context as span_context_module
1717
from opencensus.trace.trace_options import TraceOptions
18+
from opencensus.trace.tracestate import Tracestate
1819

1920

2021
class TestSpanContext(unittest.TestCase):
@@ -38,17 +39,20 @@ def test_constructor(self):
3839
self.assertEqual(span_context.trace_id, self.trace_id)
3940
self.assertEqual(span_context.span_id, self.span_id)
4041

41-
def test__str__(self):
42+
def test__repr__(self):
4243
span_context = self._make_one(
4344
trace_id=self.trace_id,
4445
span_id=self.span_id,
45-
trace_options=TraceOptions('1'))
46+
trace_options=TraceOptions('1'),
47+
tracestate=Tracestate())
4648

47-
header_expected = '6e0c63257de34c92bf9efcd03927272e' \
48-
'/6e0c63257de34c92;o=1'
49-
header = span_context.__str__()
49+
expected_repr = 'SpanContext(' \
50+
'trace_id=6e0c63257de34c92bf9efcd03927272e, ' \
51+
'span_id=6e0c63257de34c92, ' \
52+
'trace_options=TraceOptions(enabled=True), ' \
53+
'tracestate=Tracestate())'
5054

51-
self.assertEqual(header_expected, header)
55+
self.assertEqual(expected_repr, span_context.__repr__())
5256

5357
def test_check_span_id_none(self):
5458
span_context = self._make_one(

0 commit comments

Comments
 (0)