1+ import time
12from basictracer import BasicTracer
23import instana .recorder as r
34import opentracing
45import instana .options as o
56import instana .sensor as s
67
8+ from basictracer .context import SpanContext
9+ from basictracer .span import BasicSpan
10+ from instana .util import generate_id
11+
712
813class InstanaTracer (BasicTracer ):
914 sensor = None
@@ -13,6 +18,47 @@ def __init__(self, options=o.Options()):
1318 super (InstanaTracer , self ).__init__ (
1419 r .InstanaRecorder (self .sensor ), r .InstanaSampler ())
1520
21+ def start_span (
22+ self ,
23+ operation_name = None ,
24+ child_of = None ,
25+ references = None ,
26+ tags = None ,
27+ start_time = None ):
28+ "Taken from BasicTracer so we can override generate_id calls to ours"
29+
30+ start_time = time .time () if start_time is None else start_time
31+
32+ # See if we have a parent_ctx in `references`
33+ parent_ctx = None
34+ if child_of is not None :
35+ parent_ctx = (
36+ child_of if isinstance (child_of , opentracing .SpanContext )
37+ else child_of .context )
38+ elif references is not None and len (references ) > 0 :
39+ # TODO only the first reference is currently used
40+ parent_ctx = references [0 ].referenced_context
41+
42+ # Assemble the child ctx
43+ ctx = SpanContext (span_id = generate_id ())
44+ if parent_ctx is not None :
45+ if parent_ctx ._baggage is not None :
46+ ctx ._baggage = parent_ctx ._baggage .copy ()
47+ ctx .trace_id = parent_ctx .trace_id
48+ ctx .sampled = parent_ctx .sampled
49+ else :
50+ ctx .trace_id = generate_id ()
51+ ctx .sampled = self .sampler .sampled (ctx .trace_id )
52+
53+ # Tie it all together
54+ return BasicSpan (
55+ self ,
56+ operation_name = operation_name ,
57+ context = ctx ,
58+ parent_id = (None if parent_ctx is None else parent_ctx .span_id ),
59+ tags = tags ,
60+ start_time = start_time )
61+
1662
1763def init (options ):
1864 opentracing .tracer = InstanaTracer (options )
0 commit comments