Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sdk: span parents are now always spancontext #548

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def validate_spans(self):
self.assertIsNot(root_span, None)
self.assertIsNot(pymongo_span, None)
self.assertIsNotNone(pymongo_span.parent)
self.assertEqual(pymongo_span.parent.name, root_span.name)
self.assertEqual(pymongo_span.parent, root_span.get_context())
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved
self.assertIs(pymongo_span.kind, trace_api.SpanKind.CLIENT)
self.assertEqual(
pymongo_span.attributes["db.instance"], MONGODB_DB_NAME
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ def _translate_to_jaeger(spans: Span):
status = span.status

parent_id = 0
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved
if isinstance(span.parent, trace_api.Span):
parent_id = span.parent.get_context().span_id
elif isinstance(span.parent, trace_api.SpanContext):
if span.parent is not None:
parent_id = span.parent.span_id

tags = _extract_tags(span.attributes)
Expand Down
11 changes: 8 additions & 3 deletions ext/opentelemetry-ext-opentracing-shim/tests/test_shim.py
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ def test_parent_child_implicit(self):

self.assertEqual(parent_trace_id, child_trace_id)
self.assertEqual(
child.span.unwrap().parent, parent.span.unwrap()
child.span.unwrap().parent,
parent.span.unwrap().get_context(),
)

# Verify parent span becomes the active span again.
Expand Down Expand Up @@ -320,7 +321,9 @@ def test_parent_child_explicit_span(self):
child_trace_id = child.span.unwrap().get_context().trace_id

self.assertEqual(child_trace_id, parent_trace_id)
self.assertEqual(child.span.unwrap().parent, parent.unwrap())
self.assertEqual(
child.span.unwrap().parent, parent.unwrap().get_context()
)

with self.shim.start_span("ParentSpan") as parent:
child = self.shim.start_span("ChildSpan", child_of=parent)
Expand All @@ -329,7 +332,9 @@ def test_parent_child_explicit_span(self):
child_trace_id = child.unwrap().get_context().trace_id

self.assertEqual(child_trace_id, parent_trace_id)
self.assertEqual(child.unwrap().parent, parent.unwrap())
self.assertEqual(
child.unwrap().parent, parent.unwrap().get_context()
)

child.finish()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,7 @@ def translate_to_collector(spans: Sequence[Span]):
)

parent_id = 0
if isinstance(span.parent, trace_api.Span):
parent_id = span.parent.get_context().span_id
elif isinstance(span.parent, trace_api.SpanContext):
if span.parent is not None:
parent_id = span.parent.span_id

collector_span.parent_span_id = parent_id.to_bytes(8, "big")
Expand Down Expand Up @@ -157,18 +155,7 @@ def translate_to_collector(spans: Sequence[Span]):
collector_span_link.type = (
trace_pb2.Span.Link.Type.TYPE_UNSPECIFIED
)

if isinstance(span.parent, trace_api.Span):
if (
link.context.span_id
== span.parent.get_context().span_id
and link.context.trace_id
== span.parent.get_context().trace_id
):
collector_span_link.type = (
trace_pb2.Span.Link.Type.PARENT_LINKED_SPAN
)
elif isinstance(span.parent, trace_api.SpanContext):
if span.parent is not None:
if (
link.context.span_id == span.parent.span_id
and link.context.trace_id == span.parent.trace_id
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,10 @@ def test_translate_to_collector(self):
kind=trace_api.SpanKind.SERVER,
)
span_3 = trace.Span(
name="test3", context=other_context, links=(link_2,), parent=span_2
name="test3",
context=other_context,
links=(link_2,),
parent=span_2.get_context(),
)
otel_spans = [span_1, span_2, span_3]
otel_spans[0].start(start_time=start_times[0])
Expand Down
10 changes: 5 additions & 5 deletions opentelemetry-api/src/opentelemetry/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
logger = getLogger(__name__)

# TODO: quarantine
ParentSpan = typing.Optional[typing.Union["Span", "SpanContext"]]
Parent = typing.Optional[typing.Union["SpanContext", "Span"]]
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved


class LinkBase(abc.ABC):
Expand Down Expand Up @@ -512,7 +512,7 @@ def get_current_span(self) -> "Span":
def start_span(
self,
name: str,
parent: ParentSpan = CURRENT_SPAN,
parent: Parent = CURRENT_SPAN,
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved
kind: SpanKind = SpanKind.INTERNAL,
attributes: typing.Optional[types.Attributes] = None,
links: typing.Sequence[Link] = (),
Expand Down Expand Up @@ -564,7 +564,7 @@ def start_span(
def start_as_current_span(
self,
name: str,
parent: ParentSpan = CURRENT_SPAN,
parent: Parent = CURRENT_SPAN,
kind: SpanKind = SpanKind.INTERNAL,
attributes: typing.Optional[types.Attributes] = None,
links: typing.Sequence[Link] = (),
Expand Down Expand Up @@ -644,7 +644,7 @@ def get_current_span(self) -> "Span":
def start_span(
self,
name: str,
parent: ParentSpan = Tracer.CURRENT_SPAN,
parent: Parent = Tracer.CURRENT_SPAN,
kind: SpanKind = SpanKind.INTERNAL,
attributes: typing.Optional[types.Attributes] = None,
links: typing.Sequence[Link] = (),
Expand All @@ -658,7 +658,7 @@ def start_span(
def start_as_current_span(
self,
name: str,
parent: ParentSpan = Tracer.CURRENT_SPAN,
parent: Parent = Tracer.CURRENT_SPAN,
kind: SpanKind = SpanKind.INTERNAL,
attributes: typing.Optional[types.Attributes] = None,
links: typing.Sequence[Link] = (),
Expand Down
12 changes: 7 additions & 5 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ def __init__(
self,
name: str,
context: trace_api.SpanContext,
parent: trace_api.ParentSpan = None,
parent: Optional[trace_api.SpanContext] = None,
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved
sampler: Optional[sampling.Sampler] = None,
trace_config: None = None, # TODO
resource: None = None,
Expand Down Expand Up @@ -503,7 +503,7 @@ def get_current_span(self):
def start_as_current_span(
self,
name: str,
parent: trace_api.ParentSpan = trace_api.Tracer.CURRENT_SPAN,
parent: trace_api.Parent = trace_api.Tracer.CURRENT_SPAN,
codeboten marked this conversation as resolved.
Show resolved Hide resolved
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
attributes: Optional[types.Attributes] = None,
links: Sequence[trace_api.Link] = (),
Expand All @@ -514,7 +514,7 @@ def start_as_current_span(
def start_span( # pylint: disable=too-many-locals
self,
name: str,
parent: trace_api.ParentSpan = trace_api.Tracer.CURRENT_SPAN,
parent: trace_api.Parent = trace_api.Tracer.CURRENT_SPAN,
kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL,
attributes: Optional[types.Attributes] = None,
links: Sequence[trace_api.Link] = (),
Expand All @@ -531,7 +531,9 @@ def start_span( # pylint: disable=too-many-locals
if parent_context is not None and not isinstance(
parent_context, trace_api.SpanContext
):
raise TypeError
raise TypeError(
"expected Span, SpanContext or None object for trace parent."
codeboten marked this conversation as resolved.
Show resolved Hide resolved
toumorokoshi marked this conversation as resolved.
Show resolved Hide resolved
)

if parent_context is None or not parent_context.is_valid():
parent = parent_context = None
Expand Down Expand Up @@ -577,7 +579,7 @@ def start_span( # pylint: disable=too-many-locals
span = Span(
name=name,
context=context,
parent=parent,
parent=parent_context,
sampler=self.source.sampler,
resource=self.source.resource,
attributes=span_attributes,
Expand Down
2 changes: 1 addition & 1 deletion opentelemetry-sdk/tests/context/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,4 @@ def test_with_asyncio(self):
for span in span_list:
if span is expected_parent:
continue
self.assertEqual(span.parent, expected_parent)
self.assertEqual(span.parent, expected_parent.get_context())
4 changes: 2 additions & 2 deletions opentelemetry-sdk/tests/trace/test_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def test_start_span_implicit(self):
with tracer.start_span(
"child", kind=trace_api.SpanKind.CLIENT
) as child:
self.assertIs(child.parent, root)
self.assertIs(child.parent, root.get_context())
self.assertEqual(child.kind, trace_api.SpanKind.CLIENT)

self.assertIsNotNone(child.start_time)
Expand Down Expand Up @@ -322,7 +322,7 @@ def test_start_as_current_span_implicit(self):

with tracer.start_as_current_span("child") as child:
self.assertIs(tracer.get_current_span(), child)
self.assertIs(child.parent, root)
self.assertIs(child.parent, root.get_context())

# After exiting the child's scope the parent should become the
# current span again.
Expand Down