Skip to content

Commit a6cc5d8

Browse files
committed
Truncate trace_id for specific LightStep requirements
1 parent c42e99d commit a6cc5d8

File tree

2 files changed

+41
-4
lines changed

2 files changed

+41
-4
lines changed

lightstep/tracer.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,47 @@ def __init__(self, enable_binary_format, recorder, scope_manager):
8282
self.register_propagator(Format.BINARY, BinaryPropagator())
8383
self.register_propagator(LightStepFormat.LIGHTSTEP_BINARY, LightStepBinaryPropagator())
8484

85+
def start_active_span(
86+
self,
87+
operation_name,
88+
child_of=None,
89+
references=None,
90+
tags=None,
91+
start_time=None,
92+
ignore_active_span=False,
93+
finish_on_close=True
94+
):
95+
96+
scope = super(_LightstepTracer, self).start_active_span(
97+
operation_name,
98+
child_of=child_of,
99+
references=references,
100+
tags=tags,
101+
start_time=start_time,
102+
ignore_active_span=ignore_active_span,
103+
finish_on_close=finish_on_close
104+
)
105+
106+
class ScopePatch(scope.__class__):
107+
108+
def __exit__(self, exc_type, exc_val, exc_tb):
109+
110+
self.span.context.trace_id = int(
111+
format(self.span.context.trace_id, "032x")[:16], 16
112+
)
113+
114+
result = super(self.__class__, self).__exit__(
115+
exc_type, exc_val, exc_tb
116+
)
117+
118+
return result
119+
120+
# This monkey patching is done because LightStep requires for the
121+
# trace_id to be 64b long.
122+
scope.__class__ = ScopePatch
123+
124+
return scope
125+
85126
def flush(self):
86127
"""Force a flush of buffered Span data to the LightStep collector."""
87128
self.recorder.flush()

tests/trace-context/trace_context_test_service.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ def hello():
4747
timeout=5.0,
4848
)
4949

50-
scope.span.context.trace_id = int(
51-
format(scope.span.context.trace_id, "032x")[:16], 16
52-
)
53-
5450
return 'Hello, {}!'.format(hello_to)
5551

5652

0 commit comments

Comments
 (0)