Skip to content

Commit b6e97fc

Browse files
mauriciovasquezbernalreyang
authored andcommitted
span: add is_recording_events (#141)
1 parent be7577d commit b6e97fc

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

opentelemetry-api/src/opentelemetry/trace/__init__.py

+7
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ def update_name(self, name: str) -> None:
191191
on the implementation.
192192
"""
193193

194+
def is_recording_events(self) -> bool:
195+
"""Returns whether this span will be recorded.
196+
197+
Returns true if this Span is active and recording information like
198+
events with the add_event operation and attributes using set_attribute.
199+
"""
200+
194201

195202
class TraceOptions(int):
196203
"""A bitmask that represents options specific to the trace.

opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py

+13
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,8 @@ def get_context(self):
284284

285285
def set_attribute(self, key: str, value: types.AttributeValue) -> None:
286286
with self._lock:
287+
if not self.is_recording_events():
288+
return
287289
has_ended = self.end_time is not None
288290
if not has_ended:
289291
if self.attributes is Span.empty_attributes:
@@ -302,6 +304,8 @@ def add_event(
302304

303305
def add_lazy_event(self, event: trace_api.Event) -> None:
304306
with self._lock:
307+
if not self.is_recording_events():
308+
return
305309
has_ended = self.end_time is not None
306310
if not has_ended:
307311
if self.events is Span.empty_events:
@@ -322,6 +326,8 @@ def add_link(
322326

323327
def add_lazy_link(self, link: "trace_api.Link") -> None:
324328
with self._lock:
329+
if not self.is_recording_events():
330+
return
325331
has_ended = self.end_time is not None
326332
if not has_ended:
327333
if self.links is Span.empty_links:
@@ -333,6 +339,8 @@ def add_lazy_link(self, link: "trace_api.Link") -> None:
333339

334340
def start(self):
335341
with self._lock:
342+
if not self.is_recording_events():
343+
return
336344
has_started = self.start_time is not None
337345
if not has_started:
338346
self.start_time = util.time_ns()
@@ -343,6 +351,8 @@ def start(self):
343351

344352
def end(self):
345353
with self._lock:
354+
if not self.is_recording_events():
355+
return
346356
if self.start_time is None:
347357
raise RuntimeError("Calling end() on a not started span.")
348358
has_ended = self.end_time is not None
@@ -362,6 +372,9 @@ def update_name(self, name: str) -> None:
362372
return
363373
self.name = name
364374

375+
def is_recording_events(self) -> bool:
376+
return True
377+
365378

366379
def generate_span_id():
367380
"""Get a new random span ID.

0 commit comments

Comments
 (0)