Skip to content

Commit d8595b1

Browse files
committed
Override start_span so Instana IDs are used via generate_id
1 parent b19e6d5 commit d8595b1

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

instana/tracer.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1+
import time
12
from basictracer import BasicTracer
23
import instana.recorder as r
34
import opentracing
45
import instana.options as o
56
import 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

813
class 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

1763
def init(options):
1864
opentracing.tracer = InstanaTracer(options)

0 commit comments

Comments
 (0)