mirror of
https://github.com/open-webui/open-webui
synced 2025-04-18 21:38:28 +00:00
32 lines
987 B
Python
32 lines
987 B
Python
import threading
|
|
|
|
from opentelemetry.sdk.trace import ReadableSpan
|
|
from opentelemetry.sdk.trace.export import BatchSpanProcessor
|
|
|
|
|
|
class LazyBatchSpanProcessor(BatchSpanProcessor):
|
|
def __init__(self, *args, **kwargs):
|
|
super().__init__(*args, **kwargs)
|
|
self.done = True
|
|
with self.condition:
|
|
self.condition.notify_all()
|
|
self.worker_thread.join()
|
|
self.done = False
|
|
self.worker_thread = None
|
|
|
|
def on_end(self, span: ReadableSpan) -> None:
|
|
if self.worker_thread is None:
|
|
self.worker_thread = threading.Thread(
|
|
name=self.__class__.__name__, target=self.worker, daemon=True
|
|
)
|
|
self.worker_thread.start()
|
|
super().on_end(span)
|
|
|
|
def shutdown(self) -> None:
|
|
self.done = True
|
|
with self.condition:
|
|
self.condition.notify_all()
|
|
if self.worker_thread:
|
|
self.worker_thread.join()
|
|
self.span_exporter.shutdown()
|