From a7f170b7d6db6ddcb99ed697a20fc9700aa55e29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Neum=C3=BCller?= Date: Thu, 19 Sep 2019 16:49:12 +0200 Subject: [PATCH 1/4] Make use_span more flexible (closes #147). --- .../src/opentelemetry/trace/__init__.py | 17 +++++++++------ .../src/opentelemetry/sdk/trace/__init__.py | 21 ++++++++++++------- 2 files changed, 24 insertions(+), 14 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index b79cdeb4df..e7552d256f 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -415,7 +415,8 @@ def start_span( is equivalent to:: span = tracer.create_span(name) - with tracer.use_span(span): + span.start() + with tracer.use_span(span, end_on_exit=True): do_work() Args: @@ -472,17 +473,21 @@ def create_span( return INVALID_SPAN @contextmanager # type: ignore - def use_span(self, span: "Span") -> typing.Iterator[None]: + def use_span( + self, span: "Span", end_on_exit=False + ) -> typing.Iterator[None]: """Context manager for controlling a span's lifetime. - Start the given span and set it as the current span in this tracer's - context. + Set the given span as the current span in this tracer's context. - On exiting the context manager stop the span and set its parent as the - current span. + On exiting the context manager set the span that was previously active + as the current span (this is usually but not necessarily the parent of + the given span). If ``end_on_exit`` is ``True``, then the span is also + ended when exiting the context manager. Args: span: The span to start and make current. + end_on_exit: Whether to end the span automatically when leaving the context manager. """ # pylint: disable=unused-argument,no-self-use yield diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index a694476e1f..7d42595a22 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -423,7 +423,10 @@ def start_span( kind: trace_api.SpanKind = trace_api.SpanKind.INTERNAL, ) -> typing.Iterator["Span"]: """See `opentelemetry.trace.Tracer.start_span`.""" - with self.use_span(self.create_span(name, parent, kind)) as span: + + span = self.create_span(name, parent, kind) + span.start() + with self.use_span(span, end_on_exit=True) as span: yield span def create_span( @@ -460,16 +463,18 @@ def create_span( ) @contextmanager - def use_span(self, span: "Span") -> typing.Iterator["Span"]: + def use_span(self, span: Span, end_on_exit=False) -> typing.Iterator[Span]: """See `opentelemetry.trace.Tracer.use_span`.""" - span.start() - span_snapshot = self._current_span_slot.get() - self._current_span_slot.set(span) try: - yield span + span_snapshot = self._current_span_slot.get() + self._current_span_slot.set(span) + try: + yield span + finally: + self._current_span_slot.set(span_snapshot) finally: - self._current_span_slot.set(span_snapshot) - span.end() + if end_on_exit: + span.end() def add_span_processor(self, span_processor: SpanProcessor) -> None: """Registers a new :class:`SpanProcessor` for this `Tracer`. From 991c333bbf7b19429ce2a04efff3fbb34665f6f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Neum=C3=BCller?= Date: Thu, 19 Sep 2019 16:58:46 +0200 Subject: [PATCH 2/4] Fix types. --- opentelemetry-api/src/opentelemetry/trace/__init__.py | 2 +- opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index e7552d256f..5e860dac59 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -474,7 +474,7 @@ def create_span( @contextmanager # type: ignore def use_span( - self, span: "Span", end_on_exit=False + self, span: "Span", end_on_exit: bool = False ) -> typing.Iterator[None]: """Context manager for controlling a span's lifetime. diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 7d42595a22..99429e1910 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -463,7 +463,9 @@ def create_span( ) @contextmanager - def use_span(self, span: Span, end_on_exit=False) -> typing.Iterator[Span]: + def use_span( + self, span: Span, end_on_exit: bool = False + ) -> typing.Iterator[Span]: """See `opentelemetry.trace.Tracer.use_span`.""" try: span_snapshot = self._current_span_slot.get() From 2b2298706047e8c5b3042c0647d9163c6e0f20a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Neum=C3=BCller?= Date: Mon, 23 Sep 2019 17:09:34 +0200 Subject: [PATCH 3/4] Remove usesless `as span`. Co-Authored-By: Reiley Yang --- opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 99429e1910..eef40c27ff 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -426,7 +426,7 @@ def start_span( span = self.create_span(name, parent, kind) span.start() - with self.use_span(span, end_on_exit=True) as span: + with self.use_span(span, end_on_exit=True): yield span def create_span( From 88123668022dfcf59f1cbd7f361d022eb2f4ab89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Neum=C3=BCller?= Date: Tue, 24 Sep 2019 11:25:31 +0200 Subject: [PATCH 4/4] Fix overly long line in docstring. Co-Authored-By: Chris Kleinknecht --- opentelemetry-api/src/opentelemetry/trace/__init__.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index 5e860dac59..33c62e9d3b 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -487,7 +487,8 @@ def use_span( Args: span: The span to start and make current. - end_on_exit: Whether to end the span automatically when leaving the context manager. + end_on_exit: Whether to end the span automatically when leaving the + context manager. """ # pylint: disable=unused-argument,no-self-use yield