Skip to content

Commit 60947d5

Browse files
authored
feat(client): allow setting separate tracerprovider (#1254)
1 parent c59da31 commit 60947d5

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

langfuse/_client/client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class Langfuse:
106106
mask (Optional[MaskFunction]): Function to mask sensitive data in traces before sending to the API.
107107
blocked_instrumentation_scopes (Optional[List[str]]): List of instrumentation scope names to block from being exported to Langfuse. Spans from these scopes will be filtered out before being sent to the API. Useful for filtering out spans from specific libraries or frameworks. For exported spans, you can see the instrumentation scope name in the span metadata in Langfuse (`metadata.scope.name`)
108108
additional_headers (Optional[Dict[str, str]]): Additional headers to include in all API requests and OTLPSpanExporter requests. These headers will be merged with default headers. Note: If httpx_client is provided, additional_headers must be set directly on your custom httpx_client as well.
109+
tracer_provider(Optional[TracerProvider]): OpenTelemetry TracerProvider to use for Langfuse. This can be useful to set to have disconnected tracing between Langfuse and other OpenTelemetry-span emitting libraries. Note: To track active spans, the context is still shared between TracerProviders. This may lead to broken trace trees.
109110
110111
Example:
111112
```python
@@ -165,6 +166,7 @@ def __init__(
165166
mask: Optional[MaskFunction] = None,
166167
blocked_instrumentation_scopes: Optional[List[str]] = None,
167168
additional_headers: Optional[Dict[str, str]] = None,
169+
tracer_provider: Optional[otel_trace_api.TracerProvider] = None,
168170
):
169171
self._host = host or os.environ.get(LANGFUSE_HOST, "https://cloud.langfuse.com")
170172
self._environment = environment or os.environ.get(LANGFUSE_TRACING_ENVIRONMENT)
@@ -228,6 +230,7 @@ def __init__(
228230
tracing_enabled=self._tracing_enabled,
229231
blocked_instrumentation_scopes=blocked_instrumentation_scopes,
230232
additional_headers=additional_headers,
233+
tracer_provider=tracer_provider,
231234
)
232235
self._mask = self._resources.mask
233236

langfuse/_client/resource_manager.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ def __new__(
9595
tracing_enabled: Optional[bool] = None,
9696
blocked_instrumentation_scopes: Optional[List[str]] = None,
9797
additional_headers: Optional[Dict[str, str]] = None,
98+
tracer_provider: Optional[TracerProvider] = None,
9899
) -> "LangfuseResourceManager":
99100
if public_key in cls._instances:
100101
return cls._instances[public_key]
@@ -121,6 +122,7 @@ def __new__(
121122
else True,
122123
blocked_instrumentation_scopes=blocked_instrumentation_scopes,
123124
additional_headers=additional_headers,
125+
tracer_provider=tracer_provider,
124126
)
125127

126128
cls._instances[public_key] = instance
@@ -145,6 +147,7 @@ def _initialize_instance(
145147
tracing_enabled: bool = True,
146148
blocked_instrumentation_scopes: Optional[List[str]] = None,
147149
additional_headers: Optional[Dict[str, str]] = None,
150+
tracer_provider: Optional[TracerProvider] = None,
148151
):
149152
self.public_key = public_key
150153
self.secret_key = secret_key
@@ -154,7 +157,7 @@ def _initialize_instance(
154157

155158
# OTEL Tracer
156159
if tracing_enabled:
157-
tracer_provider = _init_tracer_provider(
160+
tracer_provider = tracer_provider or _init_tracer_provider(
158161
environment=environment, release=release, sample_rate=sample_rate
159162
)
160163

@@ -170,7 +173,6 @@ def _initialize_instance(
170173
)
171174
tracer_provider.add_span_processor(langfuse_processor)
172175

173-
tracer_provider = cast(TracerProvider, otel_trace_api.get_tracer_provider())
174176
self._otel_tracer = tracer_provider.get_tracer(
175177
LANGFUSE_TRACER_NAME,
176178
langfuse_version,

0 commit comments

Comments
 (0)