Skip to content

Commit fc86ef8

Browse files
authored
feat(span-processor): simplify app root state and expand LLM scopes (#1662)
1 parent 246bef7 commit fc86ef8

6 files changed

Lines changed: 129 additions & 67 deletions

File tree

‎langfuse/_client/span_filter.py‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,37 @@
1212
{
1313
LANGFUSE_TRACER_NAME,
1414
"agent_framework",
15+
"autogen-core",
1516
"ai",
1617
"haystack",
1718
"langsmith",
1819
"litellm",
1920
"openinference",
21+
"opentelemetry.instrumentation.agno",
22+
"opentelemetry.instrumentation.alephalpha",
2023
"opentelemetry.instrumentation.anthropic",
24+
"opentelemetry.instrumentation.bedrock",
25+
"opentelemetry.instrumentation.cohere",
26+
"opentelemetry.instrumentation.crewai",
27+
"opentelemetry.instrumentation.google_generativeai",
28+
"opentelemetry.instrumentation.groq",
29+
"opentelemetry.instrumentation.haystack",
30+
"opentelemetry.instrumentation.langchain",
31+
"opentelemetry.instrumentation.llamaindex",
32+
"opentelemetry.instrumentation.mistralai",
33+
"opentelemetry.instrumentation.ollama",
34+
"opentelemetry.instrumentation.openai",
35+
"opentelemetry.instrumentation.openai_agents",
36+
"opentelemetry.instrumentation.openai_v2",
37+
"opentelemetry.instrumentation.replicate",
38+
"opentelemetry.instrumentation.sagemaker",
39+
"opentelemetry.instrumentation.together",
40+
"opentelemetry.instrumentation.transformers",
41+
"opentelemetry.instrumentation.vertexai",
42+
"opentelemetry.instrumentation.voyageai",
43+
"opentelemetry.instrumentation.watsonx",
44+
"opentelemetry.instrumentation.writer",
45+
"pydantic-ai",
2146
"strands-agents",
2247
"vllm",
2348
}
@@ -28,7 +53,7 @@
2853
- exact match (``scope == prefix``)
2954
- direct descendant scopes (``scope.startswith(prefix + ".")``)
3055
31-
Please create a Github issue in https://github.com/langfuse/langfuse if you'd like to expand this default allow list.
56+
Please create a GitHub issue in https://github.com/langfuse/langfuse if you'd like to expand this default allow list.
3257
"""
3358

3459

‎langfuse/_client/span_processor.py‎

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import base64
1515
import os
1616
import threading
17-
from dataclasses import dataclass
1817
from typing import Callable, Dict, List, Optional, cast
1918

2019
from opentelemetry import context as context_api
@@ -40,18 +39,6 @@
4039
from langfuse.logger import langfuse_logger
4140

4241

43-
@dataclass
44-
class _AppRootSpanState:
45-
expected_exported_at_start: bool
46-
ended: bool = False
47-
48-
49-
@dataclass
50-
class _AppRootTraceState:
51-
active_count: int
52-
spans: Dict[str, _AppRootSpanState]
53-
54-
5542
class LangfuseSpanProcessor(BatchSpanProcessor):
5643
"""OpenTelemetry span processor that exports spans to the Langfuse API.
5744
@@ -92,7 +79,7 @@ def __init__(
9279
self._should_export_span = should_export_span or is_default_export_span
9380

9481
self._app_root_lock = threading.Lock()
95-
self._app_root_traces: Dict[str, _AppRootTraceState] = {}
82+
self._span_export_expectation_by_id: Dict[str, bool] = {}
9683

9784
env_flush_at = os.environ.get(LANGFUSE_FLUSH_AT, None)
9885
flush_at = flush_at or int(env_flush_at) if env_flush_at is not None else None
@@ -220,56 +207,28 @@ def _mark_app_root_candidate(self, *, span: Span, parent_context: Context) -> No
220207
propagated_trace_id = _get_langfuse_trace_id_from_baggage(parent_context)
221208

222209
with self._app_root_lock:
223-
trace_state = self._app_root_traces.get(trace_id)
224-
225-
if trace_state is None:
226-
trace_state = _AppRootTraceState(active_count=0, spans={})
227-
self._app_root_traces[trace_id] = trace_state
228-
229-
parent_state = (
230-
trace_state.spans.get(parent_span_id)
231-
if parent_span_id is not None
232-
else None
233-
)
234210
parent_expected_exported = (
235-
parent_state.expected_exported_at_start is True
236-
if parent_state is not None
237-
else False
211+
parent_span_id is not None
212+
and self._span_export_expectation_by_id.get(parent_span_id) is True
238213
)
239214
suppressed_by_parent_claim = propagated_trace_id == trace_id
240215

216+
self._span_export_expectation_by_id[span_id] = expected_exported
217+
241218
mark_app_root = (
242219
expected_exported
243220
and not parent_expected_exported
244221
and not suppressed_by_parent_claim
245222
)
246223

247-
trace_state.spans[span_id] = _AppRootSpanState(
248-
expected_exported_at_start=expected_exported,
249-
)
250-
trace_state.active_count += 1
251-
252224
if mark_app_root:
253225
span.set_attribute(LangfuseOtelSpanAttributes.IS_APP_ROOT, True)
254226

255227
def _cleanup_app_root_state(self, span: ReadableSpan) -> None:
256-
trace_id = format_trace_id(span.context.trace_id)
257228
span_id = format_span_id(span.context.span_id)
258229

259230
with self._app_root_lock:
260-
trace_state = self._app_root_traces.get(trace_id)
261-
262-
if trace_state is None:
263-
return
264-
265-
span_state = trace_state.spans.get(span_id)
266-
267-
if span_state is not None and not span_state.ended:
268-
span_state.ended = True
269-
trace_state.active_count -= 1
270-
271-
if trace_state.active_count <= 0:
272-
self._app_root_traces.pop(trace_id, None)
231+
self._span_export_expectation_by_id.pop(span_id, None)
273232

274233
def _is_expected_exported_at_start(self, span: Span) -> bool:
275234
readable_span = cast(ReadableSpan, span)

‎tests/conftest.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def mock_init(self: Any, **kwargs: Any) -> None:
110110
kwargs.get("should_export_span") or is_default_export_span
111111
)
112112
self._app_root_lock = threading.Lock()
113-
self._app_root_traces = {}
113+
self._span_export_expectation_by_id = {}
114114
BatchSpanProcessor.__init__(
115115
self,
116116
span_exporter=memory_exporter,

‎tests/unit/test_app_root_detection.py‎

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ def test_filtered_parent_marks_exported_children_as_app_roots(memory_exporter):
6565
assert "filtered-parent" not in spans
6666
assert spans["child-a"].attributes[LangfuseOtelSpanAttributes.IS_APP_ROOT] is True
6767
assert spans["child-b"].attributes[LangfuseOtelSpanAttributes.IS_APP_ROOT] is True
68-
assert processor._app_root_traces == {}
68+
assert processor._span_export_expectation_by_id == {}
6969

7070

7171
def test_exported_parent_suppresses_exported_child_app_root(memory_exporter):
@@ -82,7 +82,7 @@ def test_exported_parent_suppresses_exported_child_app_root(memory_exporter):
8282

8383
assert spans["parent"].attributes[LangfuseOtelSpanAttributes.IS_APP_ROOT] is True
8484
assert LangfuseOtelSpanAttributes.IS_APP_ROOT not in spans["child"].attributes
85-
assert processor._app_root_traces == {}
85+
assert processor._span_export_expectation_by_id == {}
8686

8787

8888
def test_grandparent_baggage_claim_suppresses_child_through_filtered_parent(
@@ -115,7 +115,7 @@ def test_grandparent_baggage_claim_suppresses_child_through_filtered_parent(
115115
spans["grandparent"].attributes[LangfuseOtelSpanAttributes.IS_APP_ROOT] is True
116116
)
117117
assert LangfuseOtelSpanAttributes.IS_APP_ROOT not in spans["child"].attributes
118-
assert processor._app_root_traces == {}
118+
assert processor._span_export_expectation_by_id == {}
119119

120120

121121
def test_same_trace_baggage_claim_suppresses_local_app_root(memory_exporter):
@@ -139,7 +139,7 @@ def test_same_trace_baggage_claim_suppresses_local_app_root(memory_exporter):
139139
LangfuseOtelSpanAttributes.IS_APP_ROOT
140140
not in spans["downstream-root"].attributes
141141
)
142-
assert processor._app_root_traces == {}
142+
assert processor._span_export_expectation_by_id == {}
143143

144144

145145
def test_different_trace_baggage_claim_does_not_suppress_local_app_root(
@@ -165,7 +165,7 @@ def test_different_trace_baggage_claim_does_not_suppress_local_app_root(
165165
spans["downstream-root"].attributes[LangfuseOtelSpanAttributes.IS_APP_ROOT]
166166
is True
167167
)
168-
assert processor._app_root_traces == {}
168+
assert processor._span_export_expectation_by_id == {}
169169

170170

171171
def test_local_baggage_claim_suppresses_child_even_when_parent_is_filtered(
@@ -200,7 +200,7 @@ def should_export_span(span: ReadableSpan) -> bool:
200200

201201
assert "parent" not in spans
202202
assert LangfuseOtelSpanAttributes.IS_APP_ROOT not in spans["child"].attributes
203-
assert processor._app_root_traces == {}
203+
assert processor._span_export_expectation_by_id == {}
204204

205205

206206
def test_start_time_false_positive_can_leave_exported_child_without_app_root(
@@ -228,7 +228,7 @@ def should_export_span(span: ReadableSpan) -> bool:
228228

229229
assert "parent" not in spans
230230
assert LangfuseOtelSpanAttributes.IS_APP_ROOT not in spans["child"].attributes
231-
assert processor._app_root_traces == {}
231+
assert processor._span_export_expectation_by_id == {}
232232

233233

234234
def test_active_langfuse_scope_sets_baggage_after_root_start(
@@ -272,7 +272,7 @@ def test_blocked_instrumentation_scope_parent_marks_child_as_app_root(
272272

273273
assert "blocked-parent" not in spans
274274
assert spans["child"].attributes[LangfuseOtelSpanAttributes.IS_APP_ROOT] is True
275-
assert processor._app_root_traces == {}
275+
assert processor._span_export_expectation_by_id == {}
276276

277277

278278
def test_foreign_project_langfuse_parent_marks_child_as_app_root(memory_exporter):
@@ -294,7 +294,7 @@ def test_foreign_project_langfuse_parent_marks_child_as_app_root(memory_exporter
294294

295295
assert "foreign-parent" not in spans
296296
assert spans["child"].attributes[LangfuseOtelSpanAttributes.IS_APP_ROOT] is True
297-
assert processor._app_root_traces == {}
297+
assert processor._span_export_expectation_by_id == {}
298298

299299

300300
def test_should_export_span_raising_does_not_mark_app_root(memory_exporter):
@@ -315,7 +315,7 @@ def should_export_span(span: ReadableSpan) -> bool:
315315
spans = _get_spans_by_name(memory_exporter)
316316

317317
assert "root" not in spans
318-
assert processor._app_root_traces == {}
318+
assert processor._span_export_expectation_by_id == {}
319319

320320

321321
def test_mark_app_root_candidate_exception_is_swallowed(memory_exporter, monkeypatch):
@@ -378,10 +378,12 @@ def worker(worker_id: int) -> None:
378378
LangfuseOtelSpanAttributes.IS_APP_ROOT not in span.attributes
379379
for span in child_spans
380380
)
381-
assert processor._app_root_traces == {}
381+
assert processor._span_export_expectation_by_id == {}
382382

383383

384-
def test_multiple_interleaved_traces_track_state_independently(memory_exporter):
384+
def test_multiple_interleaved_traces_track_active_span_state_independently(
385+
memory_exporter,
386+
):
385387
tracer_provider, processor = _create_processor(memory_exporter)
386388
langfuse_tracer = _langfuse_tracer(tracer_provider)
387389

@@ -390,15 +392,17 @@ def test_multiple_interleaved_traces_track_state_independently(memory_exporter):
390392
trace_b_root = langfuse_tracer.start_span("trace-b-root")
391393
trace_b_ctx = trace_api.set_span_in_context(trace_b_root)
392394

393-
assert len(processor._app_root_traces) == 2
395+
assert len(processor._span_export_expectation_by_id) == 2
394396

395397
trace_a_child = langfuse_tracer.start_span("trace-a-child", context=trace_a_ctx)
396398
trace_b_child = langfuse_tracer.start_span("trace-b-child", context=trace_b_ctx)
397399

400+
assert len(processor._span_export_expectation_by_id) == 4
401+
398402
trace_b_child.end()
399403
trace_b_root.end()
400404

401-
assert len(processor._app_root_traces) == 1
405+
assert len(processor._span_export_expectation_by_id) == 2
402406

403407
trace_a_child.end()
404408
trace_a_root.end()
@@ -419,7 +423,28 @@ def test_multiple_interleaved_traces_track_state_independently(memory_exporter):
419423
assert (
420424
LangfuseOtelSpanAttributes.IS_APP_ROOT not in spans["trace-b-child"].attributes
421425
)
422-
assert processor._app_root_traces == {}
426+
assert processor._span_export_expectation_by_id == {}
427+
428+
429+
def test_child_started_after_parent_end_is_marked_as_app_root(memory_exporter):
430+
tracer_provider, processor = _create_processor(memory_exporter)
431+
langfuse_tracer = _langfuse_tracer(tracer_provider)
432+
433+
parent = langfuse_tracer.start_span("parent")
434+
parent_ctx = trace_api.set_span_in_context(parent)
435+
436+
parent.end()
437+
438+
child = langfuse_tracer.start_span("child", context=parent_ctx)
439+
child.end()
440+
441+
processor.force_flush()
442+
443+
spans = _get_spans_by_name(memory_exporter)
444+
445+
assert spans["parent"].attributes[LangfuseOtelSpanAttributes.IS_APP_ROOT] is True
446+
assert spans["child"].attributes[LangfuseOtelSpanAttributes.IS_APP_ROOT] is True
447+
assert processor._span_export_expectation_by_id == {}
423448

424449

425450
def test_set_langfuse_trace_id_in_baggage_sets_value():

‎tests/unit/test_otel.py‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ def mock_init(self, **kwargs):
103103
kwargs.get("should_export_span") or is_default_export_span
104104
)
105105
self._app_root_lock = threading.Lock()
106-
self._app_root_traces = {}
106+
self._span_export_expectation_by_id = {}
107107
BatchSpanProcessor.__init__(
108108
self,
109109
span_exporter=memory_exporter,
@@ -2005,7 +2005,7 @@ def mock_processor_init(self, **kwargs):
20052005
kwargs.get("should_export_span") or is_default_export_span
20062006
)
20072007
self._app_root_lock = threading.Lock()
2008-
self._app_root_traces = {}
2008+
self._span_export_expectation_by_id = {}
20092009
# Use the appropriate exporter based on the project key
20102010
if self.public_key == project1_key:
20112011
exporter = exporter_project1
@@ -2388,7 +2388,7 @@ def mock_processor_init(self, **kwargs):
23882388
kwargs.get("should_export_span") or is_default_export_span
23892389
)
23902390
self._app_root_lock = threading.Lock()
2391-
self._app_root_traces = {}
2391+
self._span_export_expectation_by_id = {}
23922392

23932393
# For testing, use the appropriate exporter based on setup
23942394
exporter = kwargs.get("_test_exporter", blocked_exporter)

0 commit comments

Comments
 (0)