Skip to content

Commit

Permalink
don't use __del__ and implement Tracer.shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
mauriciovasquezbernal committed Nov 13, 2019
1 parent 25fa67e commit 5375741
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,26 +321,24 @@ class Tracer(trace_api.Tracer):
Args:
name: The name of the tracer.
shutdown_on_exit: Register an atexit hook to shutdown the tracer when
the application exits.
"""

def __init__(
self,
name: str = "",
sampler: sampling.Sampler = trace_api.sampling.ALWAYS_ON,
shutdown_on_exit: bool = True,
) -> None:
slot_name = "current_span"
if name:
slot_name = "{}.current_span".format(name)
self._current_span_slot = Context.register_slot(slot_name)
self._active_span_processor = MultiSpanProcessor()
self.sampler = sampler
self._atexit_hanlder = atexit.register(
self._active_span_processor.shutdown
)

def __del__(self):
atexit.unregister(self._atexit_hanlder)
self._active_span_processor.shutdown()
if shutdown_on_exit:
self._atexit_handler = atexit.register(self.shutdown)

def get_current_span(self):
"""See `opentelemetry.trace.Tracer.get_current_span`."""
Expand Down Expand Up @@ -462,5 +460,11 @@ def add_span_processor(self, span_processor: SpanProcessor) -> None:
# thread safe
self._active_span_processor.add_span_processor(span_processor)

def shutdown(self):
"""Shutdown the span processors added to the tracer."""
self._active_span_processor.shutdown()
if hasattr(self, "_atexit_handler"):
atexit.unregister(self._atexit_handler)


tracer = Tracer()

0 comments on commit 5375741

Please sign in to comment.