exporters.py 987 B

12345678910111213141516171819202122232425262728293031
  1. import threading
  2. from opentelemetry.sdk.trace import ReadableSpan
  3. from opentelemetry.sdk.trace.export import BatchSpanProcessor
  4. class LazyBatchSpanProcessor(BatchSpanProcessor):
  5. def __init__(self, *args, **kwargs):
  6. super().__init__(*args, **kwargs)
  7. self.done = True
  8. with self.condition:
  9. self.condition.notify_all()
  10. self.worker_thread.join()
  11. self.done = False
  12. self.worker_thread = None
  13. def on_end(self, span: ReadableSpan) -> None:
  14. if self.worker_thread is None:
  15. self.worker_thread = threading.Thread(
  16. name=self.__class__.__name__, target=self.worker, daemon=True
  17. )
  18. self.worker_thread.start()
  19. super().on_end(span)
  20. def shutdown(self) -> None:
  21. self.done = True
  22. with self.condition:
  23. self.condition.notify_all()
  24. if self.worker_thread:
  25. self.worker_thread.join()
  26. self.span_exporter.shutdown()