From 1a59ff32c3de25e6836be0d165a1e8017071f7be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Tue, 17 Sep 2019 12:32:40 +0200 Subject: [PATCH 1/3] span: add is_recording_events Add missing is_recording_events() function to span in API and SDK. The important point of this commit is to implement the check in functions like add_event, set_attribute and so son. Currently is_recording_events always returns true. --- .../src/opentelemetry/trace/__init__.py | 7 +++++++ .../src/opentelemetry/sdk/trace/__init__.py | 13 +++++++++++++ 2 files changed, 20 insertions(+) diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index 2be60151af1..b27e9de4b0e 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -191,6 +191,13 @@ def update_name(self, name: str) -> None: on the implementation. """ + def is_recording_events(self) -> bool: + """Returns the flag whether this span will be recorded. + + Returns true if this Span is active and recording information like + events with the AddEvent operation and attributes using SetAttributes. + """ + class TraceOptions(int): """A bitmask that represents options specific to the trace. diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index 23f368955be..0d4ae83185b 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -284,6 +284,8 @@ def get_context(self): def set_attribute(self, key: str, value: types.AttributeValue) -> None: with self._lock: + if not self.is_recording_events(): + return has_ended = self.end_time is not None if not has_ended: if self.attributes is Span.empty_attributes: @@ -302,6 +304,8 @@ def add_event( def add_lazy_event(self, event: trace_api.Event) -> None: with self._lock: + if not self.is_recording_events(): + return has_ended = self.end_time is not None if not has_ended: if self.events is Span.empty_events: @@ -322,6 +326,8 @@ def add_link( def add_lazy_link(self, link: "trace_api.Link") -> None: with self._lock: + if not self.is_recording_events(): + return has_ended = self.end_time is not None if not has_ended: if self.links is Span.empty_links: @@ -333,6 +339,8 @@ def add_lazy_link(self, link: "trace_api.Link") -> None: def start(self): with self._lock: + if not self.is_recording_events(): + return has_started = self.start_time is not None if not has_started: self.start_time = util.time_ns() @@ -343,6 +351,8 @@ def start(self): def end(self): with self._lock: + if not self.is_recording_events(): + return if self.start_time is None: raise RuntimeError("Calling end() on a not started span.") has_ended = self.end_time is not None @@ -362,6 +372,9 @@ def update_name(self, name: str) -> None: return self.name = name + def is_recording_events(self) -> bool: + return True + def generate_span_id(): """Get a new random span ID. From d78b8c17a68ce072d7bd643bf87fcd29b721e0aa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Tue, 17 Sep 2019 07:55:49 -0500 Subject: [PATCH 2/3] Update opentelemetry-api/src/opentelemetry/trace/__init__.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-Authored-By: Christian Neumüller --- opentelemetry-api/src/opentelemetry/trace/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index b27e9de4b0e..0c641bb7f8e 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -192,7 +192,7 @@ def update_name(self, name: str) -> None: """ def is_recording_events(self) -> bool: - """Returns the flag whether this span will be recorded. + """Returns whether this span will be recorded. Returns true if this Span is active and recording information like events with the AddEvent operation and attributes using SetAttributes. From 4d57a944ee654e7cf17242756f1a94db2caa3b2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauricio=20V=C3=A1squez?= Date: Tue, 17 Sep 2019 14:58:47 +0200 Subject: [PATCH 3/3] handle PR review --- opentelemetry-api/src/opentelemetry/trace/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index 0c641bb7f8e..22489333b91 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -195,7 +195,7 @@ def is_recording_events(self) -> bool: """Returns whether this span will be recorded. Returns true if this Span is active and recording information like - events with the AddEvent operation and attributes using SetAttributes. + events with the add_event operation and attributes using set_attribute. """