diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index 2be60151af1..22489333b91 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 whether this span will be recorded. + + Returns true if this Span is active and recording information like + events with the add_event operation and attributes using set_attribute. + """ + 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.