mirror of
https://github.com/open-telemetry/opentelemetry-python-contrib.git
synced 2025-08-01 09:13:23 +08:00
Parent is now always passed in via Context, intead of Span or SpanContext (#1146)
Co-authored-by: Diego Hurtado <ocelotl@users.noreply.github.com>
This commit is contained in:
@ -378,7 +378,7 @@ class ScopeShim(Scope):
|
||||
"""
|
||||
|
||||
otel_span = span_cm.__enter__()
|
||||
span_context = SpanContextShim(otel_span.get_context())
|
||||
span_context = SpanContextShim(otel_span.get_span_context())
|
||||
span = SpanShim(manager.tracer, span_context, otel_span)
|
||||
return cls(manager, span, span_cm)
|
||||
|
||||
@ -474,13 +474,13 @@ class ScopeManagerShim(ScopeManager):
|
||||
"""
|
||||
|
||||
span = get_current_span()
|
||||
if span.get_context() == INVALID_SPAN_CONTEXT:
|
||||
if span.get_span_context() == INVALID_SPAN_CONTEXT:
|
||||
return None
|
||||
|
||||
try:
|
||||
return get_value("scope_shim")
|
||||
except KeyError:
|
||||
span_context = SpanContextShim(span.get_context())
|
||||
span_context = SpanContextShim(span.get_span_context())
|
||||
wrapped_span = SpanShim(self._tracer, span_context, span)
|
||||
return ScopeShim(self, span=wrapped_span)
|
||||
|
||||
@ -630,6 +630,10 @@ class TracerShim(Tracer):
|
||||
# Use the specified parent or the active span if possible. Otherwise,
|
||||
# use a `None` parent, which triggers the creation of a new trace.
|
||||
parent = child_of.unwrap() if child_of else None
|
||||
if isinstance(parent, OtelSpanContext):
|
||||
parent = DefaultSpan(parent)
|
||||
|
||||
parent_span_context = set_span_in_context(parent)
|
||||
|
||||
links = []
|
||||
if references:
|
||||
@ -645,13 +649,13 @@ class TracerShim(Tracer):
|
||||
|
||||
span = self._otel_tracer.start_span(
|
||||
operation_name,
|
||||
parent,
|
||||
context=parent_span_context,
|
||||
links=links,
|
||||
attributes=tags,
|
||||
start_time=start_time_ns,
|
||||
)
|
||||
|
||||
context = SpanContextShim(span.get_context())
|
||||
context = SpanContextShim(span.get_span_context())
|
||||
return SpanShim(self, context, span)
|
||||
|
||||
def inject(self, span_context, format: object, carrier: object):
|
||||
@ -714,7 +718,7 @@ class TracerShim(Tracer):
|
||||
ctx = propagator.extract(get_as_list, carrier)
|
||||
span = get_current_span(ctx)
|
||||
if span is not None:
|
||||
otel_context = span.get_context()
|
||||
otel_context = span.get_span_context()
|
||||
else:
|
||||
otel_context = INVALID_SPAN_CONTEXT
|
||||
|
||||
|
@ -284,13 +284,17 @@ class TestShim(TestCase):
|
||||
)
|
||||
|
||||
# Verify parent-child relationship.
|
||||
parent_trace_id = parent.span.unwrap().get_context().trace_id
|
||||
child_trace_id = child.span.unwrap().get_context().trace_id
|
||||
parent_trace_id = (
|
||||
parent.span.unwrap().get_span_context().trace_id
|
||||
)
|
||||
child_trace_id = (
|
||||
child.span.unwrap().get_span_context().trace_id
|
||||
)
|
||||
|
||||
self.assertEqual(parent_trace_id, child_trace_id)
|
||||
self.assertEqual(
|
||||
child.span.unwrap().parent,
|
||||
parent.span.unwrap().get_context(),
|
||||
parent.span.unwrap().get_span_context(),
|
||||
)
|
||||
|
||||
# Verify parent span becomes the active span again.
|
||||
@ -314,23 +318,26 @@ class TestShim(TestCase):
|
||||
with self.shim.start_active_span(
|
||||
"ChildSpan", child_of=parent
|
||||
) as child:
|
||||
parent_trace_id = parent.unwrap().get_context().trace_id
|
||||
child_trace_id = child.span.unwrap().get_context().trace_id
|
||||
parent_trace_id = parent.unwrap().get_span_context().trace_id
|
||||
child_trace_id = (
|
||||
child.span.unwrap().get_span_context().trace_id
|
||||
)
|
||||
|
||||
self.assertEqual(child_trace_id, parent_trace_id)
|
||||
self.assertEqual(
|
||||
child.span.unwrap().parent, parent.unwrap().get_context()
|
||||
child.span.unwrap().parent,
|
||||
parent.unwrap().get_span_context(),
|
||||
)
|
||||
|
||||
with self.shim.start_span("ParentSpan") as parent:
|
||||
child = self.shim.start_span("ChildSpan", child_of=parent)
|
||||
|
||||
parent_trace_id = parent.unwrap().get_context().trace_id
|
||||
child_trace_id = child.unwrap().get_context().trace_id
|
||||
parent_trace_id = parent.unwrap().get_span_context().trace_id
|
||||
child_trace_id = child.unwrap().get_span_context().trace_id
|
||||
|
||||
self.assertEqual(child_trace_id, parent_trace_id)
|
||||
self.assertEqual(
|
||||
child.unwrap().parent, parent.unwrap().get_context()
|
||||
child.unwrap().parent, parent.unwrap().get_span_context()
|
||||
)
|
||||
|
||||
child.finish()
|
||||
@ -344,8 +351,10 @@ class TestShim(TestCase):
|
||||
with self.shim.start_active_span(
|
||||
"ChildSpan", child_of=parent.context
|
||||
) as child:
|
||||
parent_trace_id = parent.unwrap().get_context().trace_id
|
||||
child_trace_id = child.span.unwrap().get_context().trace_id
|
||||
parent_trace_id = parent.unwrap().get_span_context().trace_id
|
||||
child_trace_id = (
|
||||
child.span.unwrap().get_span_context().trace_id
|
||||
)
|
||||
|
||||
self.assertEqual(child_trace_id, parent_trace_id)
|
||||
self.assertEqual(
|
||||
@ -356,8 +365,8 @@ class TestShim(TestCase):
|
||||
with self.shim.start_span(
|
||||
"SpanWithContextParent", child_of=parent.context
|
||||
) as child:
|
||||
parent_trace_id = parent.unwrap().get_context().trace_id
|
||||
child_trace_id = child.unwrap().get_context().trace_id
|
||||
parent_trace_id = parent.unwrap().get_span_context().trace_id
|
||||
child_trace_id = child.unwrap().get_span_context().trace_id
|
||||
|
||||
self.assertEqual(child_trace_id, parent_trace_id)
|
||||
self.assertEqual(
|
||||
|
Reference in New Issue
Block a user