diff --git a/examples/trace/client.py b/examples/trace/client.py index 662cea8d969..d25fa60c9b7 100755 --- a/examples/trace/client.py +++ b/examples/trace/client.py @@ -39,4 +39,3 @@ tracer.add_span_processor(span_processor) response = requests.get(url="http://127.0.0.1:5000/") -span_processor.shutdown() diff --git a/examples/trace/server.py b/examples/trace/server.py index 3632540e213..0ce5a99c2c7 100755 --- a/examples/trace/server.py +++ b/examples/trace/server.py @@ -52,4 +52,3 @@ def hello(): if __name__ == "__main__": app.run(debug=True) - span_processor.shutdown() diff --git a/ext/opentelemetry-ext-azure-monitor/examples/client.py b/ext/opentelemetry-ext-azure-monitor/examples/client.py index ff954788e66..044050322f8 100644 --- a/ext/opentelemetry-ext-azure-monitor/examples/client.py +++ b/ext/opentelemetry-ext-azure-monitor/examples/client.py @@ -27,4 +27,3 @@ tracer.add_span_processor(span_processor) response = requests.get(url="http://127.0.0.1:5000/") -span_processor.shutdown() diff --git a/ext/opentelemetry-ext-azure-monitor/examples/server.py b/ext/opentelemetry-ext-azure-monitor/examples/server.py index 9374c986a4a..9b4e9ab8324 100644 --- a/ext/opentelemetry-ext-azure-monitor/examples/server.py +++ b/ext/opentelemetry-ext-azure-monitor/examples/server.py @@ -41,4 +41,3 @@ def hello(): if __name__ == "__main__": app.run(debug=True) - span_processor.shutdown() diff --git a/ext/opentelemetry-ext-jaeger/README.rst b/ext/opentelemetry-ext-jaeger/README.rst index 04ccdfac4ac..b4493f6174e 100644 --- a/ext/opentelemetry-ext-jaeger/README.rst +++ b/ext/opentelemetry-ext-jaeger/README.rst @@ -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 diff --git a/ext/opentelemetry-ext-jaeger/examples/jaeger_exporter_example.py b/ext/opentelemetry-ext-jaeger/examples/jaeger_exporter_example.py index 5cf57bfccad..d557f3fc258 100644 --- a/ext/opentelemetry-ext-jaeger/examples/jaeger_exporter_example.py +++ b/ext/opentelemetry-ext-jaeger/examples/jaeger_exporter_example.py @@ -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() diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py index d06d34d93bc..3c71e74f186 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/__init__.py @@ -13,6 +13,7 @@ # limitations under the License. +import atexit import logging import random import threading @@ -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`."""