Skip to content

Commit

Permalink
sdk: shut down span processors automatically
Browse files Browse the repository at this point in the history
The BatchExportSpanProcessor is an asynchronous span processor that uses a
worker thread to call the different exporters.  Before this commit applications
had to shut down the span processor explicitely to guarantee that all the spans
were summited to the exporters, this was not very intuitive for the users.

This commit removes that limitation by implementing the tracer's __del__
method and an atexit hook.  According to __del__'s documentation [1] it is
possible that sometimes it's not called, for that reason the atexit hook is
also used to guarantee that the processor is shut down in all the cases.

[1] https://docs.python.org/3/reference/datamodel.html#object.__del__
  • Loading branch information
mauriciovasquezbernal committed Nov 8, 2019
1 parent 9fd4ccd commit 25fa67e
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 12 deletions.
1 change: 0 additions & 1 deletion examples/trace/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,3 @@
tracer.add_span_processor(span_processor)

response = requests.get(url="http://127.0.0.1:5000/")
span_processor.shutdown()
1 change: 0 additions & 1 deletion examples/trace/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,3 @@ def hello():

if __name__ == "__main__":
app.run(debug=True)
span_processor.shutdown()
1 change: 0 additions & 1 deletion ext/opentelemetry-ext-azure-monitor/examples/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,3 @@
tracer.add_span_processor(span_processor)

response = requests.get(url="http://127.0.0.1:5000/")
span_processor.shutdown()
1 change: 0 additions & 1 deletion ext/opentelemetry-ext-azure-monitor/examples/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,3 @@ def hello():

if __name__ == "__main__":
app.run(debug=True)
span_processor.shutdown()
4 changes: 0 additions & 4 deletions ext/opentelemetry-ext-jaeger/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,6 @@ gRPC is still not supported by this implementation.
with tracer.start_as_current_span('foo'):
print('Hello world!')
# shutdown the span processor
# TODO: this has to be improved so user doesn't need to call it manually
span_processor.shutdown()
The `examples <./examples>`_ folder contains more elaborated examples.

References
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,3 @@
time.sleep(0.2)

time.sleep(0.1)

# shutdown the span processor
# TODO: this has to be improved so user doesn't need to call it manually
span_processor.shutdown()
8 changes: 8 additions & 0 deletions opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.


import atexit
import logging
import random
import threading
Expand Down Expand Up @@ -333,6 +334,13 @@ def __init__(
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()

def get_current_span(self):
"""See `opentelemetry.trace.Tracer.get_current_span`."""
Expand Down

0 comments on commit 25fa67e

Please sign in to comment.