Skip to content

Commit

Permalink
Truncate trace_id for specific LightStep requirements
Browse files Browse the repository at this point in the history
  • Loading branch information
ocelotl committed Nov 30, 2019
1 parent c42e99d commit a6cc5d8
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 4 deletions.
41 changes: 41 additions & 0 deletions lightstep/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,47 @@ def __init__(self, enable_binary_format, recorder, scope_manager):
self.register_propagator(Format.BINARY, BinaryPropagator())
self.register_propagator(LightStepFormat.LIGHTSTEP_BINARY, LightStepBinaryPropagator())

def start_active_span(
self,
operation_name,
child_of=None,
references=None,
tags=None,
start_time=None,
ignore_active_span=False,
finish_on_close=True
):

scope = super(_LightstepTracer, self).start_active_span(
operation_name,
child_of=child_of,
references=references,
tags=tags,
start_time=start_time,
ignore_active_span=ignore_active_span,
finish_on_close=finish_on_close
)

class ScopePatch(scope.__class__):

def __exit__(self, exc_type, exc_val, exc_tb):

self.span.context.trace_id = int(
format(self.span.context.trace_id, "032x")[:16], 16

This comment has been minimized.

Copy link
@lookfwd

lookfwd Dec 9, 2019

@ocelotl I find this a bit unusual. If I follow this and this,

>>> guid_rng = random.Random()
>>> tid = guid_rng.getrandbits(64) - 1
>>> tid
15011346066240981164
>>> int(format(tid, "032x")[:16], 16)
0

This comment has been minimized.

Copy link
@ocelotl

ocelotl Dec 9, 2019

Author Contributor

Thanks for pointing that out, will open a PR with the fix. 👍

)

result = super(self.__class__, self).__exit__(
exc_type, exc_val, exc_tb
)

return result

# This monkey patching is done because LightStep requires for the
# trace_id to be 64b long.
scope.__class__ = ScopePatch

return scope

def flush(self):
"""Force a flush of buffered Span data to the LightStep collector."""
self.recorder.flush()
Expand Down
4 changes: 0 additions & 4 deletions tests/trace-context/trace_context_test_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ def hello():
timeout=5.0,
)

scope.span.context.trace_id = int(
format(scope.span.context.trace_id, "032x")[:16], 16
)

return 'Hello, {}!'.format(hello_to)


Expand Down

0 comments on commit a6cc5d8

Please sign in to comment.