-
Notifications
You must be signed in to change notification settings - Fork 260
Expand file tree
/
Copy pathresource_manager.py
More file actions
405 lines (342 loc) · 15.2 KB
/
resource_manager.py
File metadata and controls
405 lines (342 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
"""Tracer implementation for Langfuse OpenTelemetry integration.
This module provides the LangfuseTracer class, a thread-safe singleton that manages OpenTelemetry
tracing infrastructure for Langfuse. It handles tracer initialization, span processors,
API clients, and coordinates background tasks for efficient data processing and media handling.
Key features:
- Thread-safe OpenTelemetry tracer with Langfuse-specific span processors and sampling
- Configurable batch processing of spans and scores with intelligent flushing behavior
- Asynchronous background media upload processing with dedicated worker threads
- Concurrent score ingestion with batching and retry mechanisms
- Automatic project ID discovery and caching
- Graceful shutdown handling with proper resource cleanup
- Fault tolerance with detailed error logging and recovery mechanisms
"""
import atexit
import os
import threading
from queue import Full, Queue
from typing import Any, Dict, Optional, cast
import httpx
from opentelemetry import trace as otel_trace_api
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.sampling import Decision, TraceIdRatioBased
from langfuse._client.attributes import LangfuseOtelSpanAttributes
from langfuse._client.constants import LANGFUSE_TRACER_NAME
from langfuse._client.environment_variables import (
LANGFUSE_MEDIA_UPLOAD_ENABLED,
LANGFUSE_MEDIA_UPLOAD_THREAD_COUNT,
LANGFUSE_RELEASE,
LANGFUSE_TRACING_ENVIRONMENT,
)
from langfuse._client.span_processor import LangfuseSpanProcessor
from langfuse._task_manager.media_manager import MediaManager
from langfuse._task_manager.media_upload_consumer import MediaUploadConsumer
from langfuse._task_manager.score_ingestion_consumer import ScoreIngestionConsumer
from langfuse._utils.environment import get_common_release_envs
from langfuse._utils.prompt_cache import PromptCache
from langfuse._utils.request import LangfuseClient
from langfuse.api.client import AsyncFernLangfuse, FernLangfuse
from langfuse.logger import langfuse_logger
from langfuse.types import MaskFunction
from ..version import __version__ as langfuse_version
class LangfuseResourceManager:
"""Thread-safe singleton that provides access to the OpenTelemetry tracer and processors.
This class implements a thread-safe singleton pattern keyed by the public API key,
ensuring that only one tracer instance exists per API key combination. It manages
the lifecycle of the OpenTelemetry tracer provider, span processors, and resource
attributes, as well as background threads for media uploads and score ingestion.
The tracer is responsible for:
1. Setting up the OpenTelemetry tracer with appropriate sampling and configuration
2. Managing the span processor for exporting spans to the Langfuse API
3. Creating and managing Langfuse API clients (both synchronous and asynchronous)
4. Handling background media upload processing via dedicated worker threads
5. Processing and batching score ingestion events with configurable flush settings
6. Retrieving and caching project information for URL generation and media handling
7. Coordinating graceful shutdown of all background processes with proper resource cleanup
This implementation follows best practices for resource management in long-running
applications, including thread-safe singleton pattern, bounded queues to prevent memory
exhaustion, proper resource cleanup on shutdown, and fault-tolerant error handling with
detailed logging.
Thread safety is ensured through the use of locks, thread-safe queues, and atomic operations,
making this implementation suitable for multi-threaded and asyncio applications.
"""
_instances: Dict[str, "LangfuseResourceManager"] = {}
_lock = threading.RLock()
def __new__(
cls,
*,
public_key: str,
secret_key: str,
host: str,
environment: Optional[str] = None,
release: Optional[str] = None,
timeout: Optional[int] = None,
flush_at: Optional[int] = None,
flush_interval: Optional[float] = None,
httpx_client: Optional[httpx.Client] = None,
media_upload_thread_count: Optional[int] = None,
sample_rate: Optional[float] = None,
mask: Optional[MaskFunction] = None,
) -> "LangfuseResourceManager":
if public_key in cls._instances:
return cls._instances[public_key]
with cls._lock:
if public_key not in cls._instances:
instance = super(LangfuseResourceManager, cls).__new__(cls)
instance._otel_tracer = None
instance._initialize_instance(
public_key=public_key,
secret_key=secret_key,
host=host,
timeout=timeout,
environment=environment,
release=release,
flush_at=flush_at,
flush_interval=flush_interval,
httpx_client=httpx_client,
media_upload_thread_count=media_upload_thread_count,
sample_rate=sample_rate,
mask=mask,
)
cls._instances[public_key] = instance
return cls._instances[public_key]
def _initialize_instance(
self,
*,
public_key: str,
secret_key: str,
host: str,
environment: Optional[str] = None,
release: Optional[str] = None,
timeout: Optional[int] = None,
flush_at: Optional[int] = None,
flush_interval: Optional[float] = None,
media_upload_thread_count: Optional[int] = None,
httpx_client: Optional[httpx.Client] = None,
sample_rate: Optional[float] = None,
mask: Optional[MaskFunction] = None,
):
self.public_key = public_key
self.mask = mask
# OTEL Tracer
tracer_provider = _init_tracer_provider(
environment=environment, release=release, sample_rate=sample_rate
)
langfuse_processor = LangfuseSpanProcessor(
public_key=self.public_key,
secret_key=secret_key,
host=host,
timeout=timeout,
flush_at=flush_at,
flush_interval=flush_interval,
)
tracer_provider.add_span_processor(langfuse_processor)
tracer_provider = cast(TracerProvider, otel_trace_api.get_tracer_provider())
self._otel_tracer = tracer_provider.get_tracer(
LANGFUSE_TRACER_NAME,
langfuse_version,
attributes={"public_key": self.public_key},
)
# API Clients
## API clients must be singletons because the underlying HTTPX clients
## use connection pools with limited capacity. Creating multiple instances
## could exhaust the OS's maximum number of available TCP sockets (file descriptors),
## leading to connection errors.
self.httpx_client = httpx_client or httpx.Client(timeout=timeout)
self.api = FernLangfuse(
base_url=host,
username=self.public_key,
password=secret_key,
x_langfuse_sdk_name="python",
x_langfuse_sdk_version=langfuse_version,
x_langfuse_public_key=self.public_key,
httpx_client=self.httpx_client,
timeout=timeout,
)
self.async_api = AsyncFernLangfuse(
base_url=host,
username=self.public_key,
password=secret_key,
x_langfuse_sdk_name="python",
x_langfuse_sdk_version=langfuse_version,
x_langfuse_public_key=self.public_key,
timeout=timeout,
)
score_ingestion_client = LangfuseClient(
public_key=self.public_key,
secret_key=secret_key,
base_url=host,
version=langfuse_version,
timeout=timeout or 20,
session=self.httpx_client,
)
# Media
self._media_upload_enabled = os.environ.get(
LANGFUSE_MEDIA_UPLOAD_ENABLED, "True"
).lower() not in ("false", "0")
self._media_upload_queue: Queue[Any] = Queue(100_000)
self._media_manager = MediaManager(
api_client=self.api,
media_upload_queue=self._media_upload_queue,
max_retries=3,
)
self._media_upload_consumers = []
media_upload_thread_count = media_upload_thread_count or max(
int(os.getenv(LANGFUSE_MEDIA_UPLOAD_THREAD_COUNT, 1)), 1
)
if self._media_upload_enabled:
for i in range(media_upload_thread_count):
media_upload_consumer = MediaUploadConsumer(
identifier=i,
media_manager=self._media_manager,
)
media_upload_consumer.start()
self._media_upload_consumers.append(media_upload_consumer)
# Prompt cache
self.prompt_cache = PromptCache()
# Score ingestion
self._score_ingestion_queue: Queue[Any] = Queue(100_000)
self._ingestion_consumers = []
ingestion_consumer = ScoreIngestionConsumer(
ingestion_queue=self._score_ingestion_queue,
identifier=0,
client=score_ingestion_client,
flush_at=flush_at,
flush_interval=flush_interval,
max_retries=3,
public_key=self.public_key,
)
ingestion_consumer.start()
self._ingestion_consumers.append(ingestion_consumer)
# Register shutdown handler
atexit.register(self.shutdown)
langfuse_logger.info(
f"Startup: Langfuse tracer successfully initialized | "
f"public_key={self.public_key} | "
f"host={host} | "
f"environment={environment or 'default'} | "
f"sample_rate={sample_rate if sample_rate is not None else 1.0} | "
f"media_threads={media_upload_thread_count or 1}"
)
@classmethod
def reset(cls):
cls._instances.clear()
def add_score_task(self, event: dict):
try:
# Sample scores with the same sampler that is used for tracing
tracer_provider = cast(TracerProvider, otel_trace_api.get_tracer_provider())
should_sample = (
(
tracer_provider.sampler.should_sample(
parent_context=None,
trace_id=int(event["body"].trace_id, 16),
name="score",
).decision
== Decision.RECORD_AND_SAMPLE
if hasattr(event["body"], "trace_id")
else True
)
if event["body"].trace_id
is not None # do not sample out session / dataset run scores
else True
)
if should_sample:
langfuse_logger.debug(
f"Score: Enqueuing event type={event['type']} for trace_id={event['body'].trace_id} name={event['body'].name} value={event['body'].value}"
)
self._score_ingestion_queue.put(event, block=False)
except Full:
langfuse_logger.warning(
"System overload: Score ingestion queue has reached capacity (100,000 items). Score will be dropped. Consider increasing flush frequency or decreasing event volume."
)
return
except Exception as e:
langfuse_logger.error(
f"Unexpected error: Failed to process score event. The score will be dropped. Error details: {e}"
)
return
@property
def tracer(self):
return self._otel_tracer
@staticmethod
def get_current_span():
return otel_trace_api.get_current_span()
def _stop_and_join_consumer_threads(self):
"""End the consumer threads once the queue is empty.
Blocks execution until finished
"""
langfuse_logger.debug(
f"Shutdown: Waiting for {len(self._media_upload_consumers)} media upload thread(s) to complete processing"
)
for media_upload_consumer in self._media_upload_consumers:
media_upload_consumer.pause()
for media_upload_consumer in self._media_upload_consumers:
try:
media_upload_consumer.join()
except RuntimeError:
# consumer thread has not started
pass
langfuse_logger.debug(
f"Shutdown: Media upload thread #{media_upload_consumer._identifier} successfully terminated"
)
langfuse_logger.debug(
f"Shutdown: Waiting for {len(self._ingestion_consumers)} score ingestion thread(s) to complete processing"
)
for score_ingestion_consumer in self._ingestion_consumers:
score_ingestion_consumer.pause()
for score_ingestion_consumer in self._ingestion_consumers:
try:
score_ingestion_consumer.join()
except RuntimeError:
# consumer thread has not started
pass
langfuse_logger.debug(
f"Shutdown: Score ingestion thread #{score_ingestion_consumer._identifier} successfully terminated"
)
def flush(self):
tracer_provider = cast(TracerProvider, otel_trace_api.get_tracer_provider())
if isinstance(tracer_provider, otel_trace_api.ProxyTracerProvider):
return
tracer_provider.force_flush()
langfuse_logger.debug("Successfully flushed OTEL tracer provider")
self._score_ingestion_queue.join()
langfuse_logger.debug("Successfully flushed score ingestion queue")
self._media_upload_queue.join()
langfuse_logger.debug("Successfully flushed media upload queue")
def shutdown(self):
# Unregister the atexit handler first
atexit.unregister(self.shutdown)
tracer_provider = cast(TracerProvider, otel_trace_api.get_tracer_provider())
if isinstance(tracer_provider, otel_trace_api.ProxyTracerProvider):
return
tracer_provider.force_flush()
self._stop_and_join_consumer_threads()
def _init_tracer_provider(
*,
environment: Optional[str] = None,
release: Optional[str] = None,
sample_rate: Optional[float] = None,
) -> TracerProvider:
environment = environment or os.environ.get(LANGFUSE_TRACING_ENVIRONMENT)
release = release or os.environ.get(LANGFUSE_RELEASE) or get_common_release_envs()
resource_attributes = {
LangfuseOtelSpanAttributes.ENVIRONMENT: environment,
LangfuseOtelSpanAttributes.RELEASE: release,
}
resource = Resource.create(
{k: v for k, v in resource_attributes.items() if v is not None}
)
provider = None
default_provider = cast(TracerProvider, otel_trace_api.get_tracer_provider())
if isinstance(default_provider, otel_trace_api.ProxyTracerProvider):
provider = TracerProvider(
resource=resource,
sampler=TraceIdRatioBased(sample_rate)
if sample_rate is not None and sample_rate < 1
else None,
)
otel_trace_api.set_tracer_provider(provider)
else:
provider = default_provider
return provider