Skip to content

Commit

Permalink
Parent is now always passed in via Context, intead of Span or SpanCon…
Browse files Browse the repository at this point in the history
…text (#1146)

Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
  • Loading branch information
alrex and ocelotl authored Oct 8, 2020
1 parent 4a66652 commit 0e852ea
Show file tree
Hide file tree
Showing 41 changed files with 261 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -189,12 +189,12 @@ def _translate_to_datadog(self, spans):

def _get_trace_ids(span):
"""Extract tracer ids from span"""
ctx = span.get_context()
ctx = span.get_span_context()
trace_id = ctx.trace_id
span_id = ctx.span_id

if isinstance(span.parent, trace_api.Span):
parent_id = span.parent.get_context().span_id
parent_id = span.parent.get_span_context().span_id
elif isinstance(span.parent, trace_api.SpanContext):
parent_id = span.parent.span_id
else:
Expand Down Expand Up @@ -255,13 +255,13 @@ def _get_exc_info(span):


def _get_origin(span):
ctx = span.get_context()
ctx = span.get_span_context()
origin = ctx.trace_state.get(DD_ORIGIN)
return origin


def _get_sampling_rate(span):
ctx = span.get_context()
ctx = span.get_span_context()
return (
span.sampler.rate
if ctx.trace_flags.sampled
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def inject(
context: typing.Optional[Context] = None,
) -> None:
span = get_current_span(context)
span_context = span.get_context()
span_context = span.get_span_context()
if span_context == trace.INVALID_SPAN_CONTEXT:
return
sampled = (trace.TraceFlags.SAMPLED & span.context.trace_flags) != 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def __init__(
self.worker_thread.start()

def on_start(self, span: Span) -> None:
ctx = span.get_context()
ctx = span.get_span_context()
trace_id = ctx.trace_id

with self.traces_lock:
Expand All @@ -102,7 +102,7 @@ def on_end(self, span: Span) -> None:
logger.warning("Already shutdown, dropping span.")
return

ctx = span.get_context()
ctx = span.get_span_context()
trace_id = ctx.trace_id

with self.traces_lock:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ def test_translate_to_datadog(self):
span_context = trace_api.SpanContext(
trace_id, span_id, is_remote=False
)
parent_context = trace_api.SpanContext(
parent_span_context = trace_api.SpanContext(
trace_id, parent_id, is_remote=False
)
other_context = trace_api.SpanContext(
Expand All @@ -191,14 +191,14 @@ def test_translate_to_datadog(self):
trace._Span(
name=span_names[0],
context=span_context,
parent=parent_context,
parent=parent_span_context,
kind=trace_api.SpanKind.CLIENT,
instrumentation_info=instrumentation_info,
resource=Resource({}),
),
trace._Span(
name=span_names[1],
context=parent_context,
context=parent_span_context,
parent=None,
instrumentation_info=instrumentation_info,
resource=resource_without_service,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_malformed_headers(self):
malformed_parent_id_key: self.serialized_parent_id,
},
)
).get_context()
).get_span_context()

self.assertNotEqual(context.trace_id, int(self.serialized_trace_id))
self.assertNotEqual(context.span_id, int(self.serialized_parent_id))
Expand All @@ -64,7 +64,7 @@ def test_missing_trace_id(self):
}

ctx = FORMAT.extract(get_as_list, carrier)
span_context = get_current_span(ctx).get_context()
span_context = get_current_span(ctx).get_span_context()
self.assertEqual(span_context.trace_id, trace_api.INVALID_TRACE_ID)

def test_missing_parent_id(self):
Expand All @@ -74,12 +74,12 @@ def test_missing_parent_id(self):
}

ctx = FORMAT.extract(get_as_list, carrier)
span_context = get_current_span(ctx).get_context()
span_context = get_current_span(ctx).get_span_context()
self.assertEqual(span_context.span_id, trace_api.INVALID_SPAN_ID)

def test_context_propagation(self):
"""Test the propagation of Datadog headers."""
parent_context = get_current_span(
parent_span_context = get_current_span(
FORMAT.extract(
get_as_list,
{
Expand All @@ -89,31 +89,31 @@ def test_context_propagation(self):
FORMAT.ORIGIN_KEY: self.serialized_origin,
},
)
).get_context()
).get_span_context()

self.assertEqual(
parent_context.trace_id, int(self.serialized_trace_id)
parent_span_context.trace_id, int(self.serialized_trace_id)
)
self.assertEqual(
parent_context.span_id, int(self.serialized_parent_id)
parent_span_context.span_id, int(self.serialized_parent_id)
)
self.assertEqual(parent_context.trace_flags, constants.AUTO_KEEP)
self.assertEqual(parent_span_context.trace_flags, constants.AUTO_KEEP)
self.assertEqual(
parent_context.trace_state.get(constants.DD_ORIGIN),
parent_span_context.trace_state.get(constants.DD_ORIGIN),
self.serialized_origin,
)
self.assertTrue(parent_context.is_remote)
self.assertTrue(parent_span_context.is_remote)

child = trace._Span(
"child",
trace_api.SpanContext(
parent_context.trace_id,
parent_span_context.trace_id,
trace_api.RandomIdsGenerator().generate_span_id(),
is_remote=False,
trace_flags=parent_context.trace_flags,
trace_state=parent_context.trace_state,
trace_flags=parent_span_context.trace_flags,
trace_state=parent_span_context.trace_state,
),
parent=parent_context,
parent=parent_span_context,
)

child_carrier = {}
Expand All @@ -136,7 +136,7 @@ def test_context_propagation(self):

def test_sampling_priority_auto_reject(self):
"""Test sampling priority rejected."""
parent_context = get_current_span(
parent_span_context = get_current_span(
FORMAT.extract(
get_as_list,
{
Expand All @@ -145,20 +145,22 @@ def test_sampling_priority_auto_reject(self):
FORMAT.SAMPLING_PRIORITY_KEY: str(constants.AUTO_REJECT),
},
)
).get_context()
).get_span_context()

self.assertEqual(parent_context.trace_flags, constants.AUTO_REJECT)
self.assertEqual(
parent_span_context.trace_flags, constants.AUTO_REJECT
)

child = trace._Span(
"child",
trace_api.SpanContext(
parent_context.trace_id,
parent_span_context.trace_id,
trace_api.RandomIdsGenerator().generate_span_id(),
is_remote=False,
trace_flags=parent_context.trace_flags,
trace_state=parent_context.trace_state,
trace_flags=parent_span_context.trace_flags,
trace_state=parent_span_context.trace_state,
),
parent=parent_context,
parent=parent_span_context,
)

child_carrier = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
foo.set_attribute("my_atribbute", True)
foo.add_event("event in foo", {"name": "foo1"})
with tracer.start_as_current_span(
"bar", links=[trace.Link(foo.get_context())]
"bar", links=[trace.Link(foo.get_span_context())]
) as bar:
time.sleep(0.2)
bar.set_attribute("speed", 100.0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ def _translate_to_jaeger(spans: Span):
jaeger_spans = []

for span in spans:
ctx = span.get_context()
ctx = span.get_span_context()
trace_id = ctx.trace_id
span_id = ctx.span_id

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def test_translate_to_jaeger(self):
span_context = trace_api.SpanContext(
trace_id, span_id, is_remote=False
)
parent_context = trace_api.SpanContext(
parent_span_context = trace_api.SpanContext(
trace_id, parent_id, is_remote=False
)
other_context = trace_api.SpanContext(
Expand Down Expand Up @@ -190,13 +190,13 @@ def test_translate_to_jaeger(self):
trace._Span(
name=span_names[0],
context=span_context,
parent=parent_context,
parent=parent_span_context,
events=(event,),
links=(link,),
kind=trace_api.SpanKind.CLIENT,
),
trace._Span(
name=span_names[1], context=parent_context, parent=None
name=span_names[1], context=parent_span_context, parent=None
),
trace._Span(
name=span_names[2], context=other_context, parent=None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def test_translate_to_collector(self):
trace_flags=TraceFlags(TraceFlags.SAMPLED),
trace_state=trace_api.TraceState({"testKey": "testValue"}),
)
parent_context = trace_api.SpanContext(
parent_span_context = trace_api.SpanContext(
trace_id, parent_id, is_remote=False
)
other_context = trace_api.SpanContext(
Expand All @@ -118,27 +118,27 @@ def test_translate_to_collector(self):
context=other_context, attributes=link_attributes
)
link_2 = trace_api.Link(
context=parent_context, attributes=link_attributes
context=parent_span_context, attributes=link_attributes
)
span_1 = trace._Span(
name="test1",
context=span_context,
parent=parent_context,
parent=parent_span_context,
events=(event,),
links=(link_1,),
kind=trace_api.SpanKind.CLIENT,
)
span_2 = trace._Span(
name="test2",
context=parent_context,
context=parent_span_context,
parent=None,
kind=trace_api.SpanKind.SERVER,
)
span_3 = trace._Span(
name="test3",
context=other_context,
links=(link_2,),
parent=span_2.get_context(),
parent=span_2.get_span_context(),
)
otel_spans = [span_1, span_2, span_3]
otel_spans[0].start(start_time=start_times[0])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def _translate_to_zipkin(self, spans: Sequence[Span]):

zipkin_spans = []
for span in spans:
context = span.get_context()
context = span.get_span_context()
trace_id = context.trace_id
span_id = context.span_id

Expand Down Expand Up @@ -205,7 +205,7 @@ def _translate_to_zipkin(self, spans: Sequence[Span]):

if isinstance(span.parent, Span):
zipkin_span["parentId"] = format(
span.parent.get_context().span_id, "016x"
span.parent.get_span_context().span_id, "016x"
)
elif isinstance(span.parent, SpanContext):
zipkin_span["parentId"] = format(span.parent.span_id, "016x")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ def test_export(self):
is_remote=False,
trace_flags=TraceFlags(TraceFlags.SAMPLED),
)
parent_context = trace_api.SpanContext(
parent_span_context = trace_api.SpanContext(
trace_id, parent_id, is_remote=False
)
other_context = trace_api.SpanContext(
Expand Down Expand Up @@ -157,12 +157,12 @@ def test_export(self):
trace._Span(
name=span_names[0],
context=span_context,
parent=parent_context,
parent=parent_span_context,
events=(event,),
links=(link,),
),
trace._Span(
name=span_names[1], context=parent_context, parent=None
name=span_names[1], context=parent_span_context, parent=None
),
trace._Span(
name=span_names[2], context=other_context, parent=None
Expand Down Expand Up @@ -328,12 +328,14 @@ def test_zero_padding(self):
is_remote=False,
trace_flags=TraceFlags(TraceFlags.SAMPLED),
)
parent_context = trace_api.SpanContext(
parent_span_context = trace_api.SpanContext(
trace_id, parent_id, is_remote=False
)

otel_span = trace._Span(
name=span_names[0], context=span_context, parent=parent_context,
name=span_names[0],
context=span_context,
parent=parent_span_context,
)

otel_span.start(start_time=start_time)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,13 @@ def _trace_prerun(self, *args, **kwargs):
return

request = task.request
tracectx = propagators.extract(carrier_extractor, request) or {}
parent = get_current_span(tracectx)
tracectx = propagators.extract(carrier_extractor, request) or None

logger.debug("prerun signal start task_id=%s", task_id)

operation_name = "{0}/{1}".format(_TASK_RUN, task.name)
span = self._tracer.start_span(
operation_name, parent=parent, kind=trace.SpanKind.CONSUMER
operation_name, context=tracectx, kind=trace.SpanKind.CONSUMER
)

activation = self._tracer.use_span(span, end_on_exit=True)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def test_render_inline_template_with_root(self):
# pylint:disable=unbalanced-tuple-unpacking
render, template, root = spans[:3]

self.assertIs(render.parent, root.get_context())
self.assertIs(template.parent, root.get_context())
self.assertIs(render.parent, root.get_span_context())
self.assertIs(template.parent, root.get_span_context())
self.assertIsNone(root.parent)

def test_render_inline_template(self):
Expand Down Expand Up @@ -88,8 +88,8 @@ def test_generate_inline_template_with_root(self):
# pylint:disable=unbalanced-tuple-unpacking
template, generate, root = spans

self.assertIs(generate.parent, root.get_context())
self.assertIs(template.parent, root.get_context())
self.assertIs(generate.parent, root.get_span_context())
self.assertIs(template.parent, root.get_span_context())
self.assertIsNone(root.parent)

def test_generate_inline_template(self):
Expand Down Expand Up @@ -131,11 +131,11 @@ def test_file_template_with_root(self):
# pylint:disable=unbalanced-tuple-unpacking
compile2, load2, compile1, load1, render, root = spans

self.assertIs(compile2.parent, load2.get_context())
self.assertIs(load2.parent, root.get_context())
self.assertIs(compile1.parent, load1.get_context())
self.assertIs(load1.parent, render.get_context())
self.assertIs(render.parent, root.get_context())
self.assertIs(compile2.parent, load2.get_span_context())
self.assertIs(load2.parent, root.get_span_context())
self.assertIs(compile1.parent, load1.get_span_context())
self.assertIs(load1.parent, render.get_span_context())
self.assertIs(render.parent, root.get_span_context())
self.assertIsNone(root.parent)

def test_file_template(self):
Expand Down
Loading

0 comments on commit 0e852ea

Please sign in to comment.