Skip to content

Commit 98653a2

Browse files
authored
fix(otel): keep last duplicate span in masking batches (#1803)
1 parent bb4fc5b commit 98653a2

4 files changed

Lines changed: 96 additions & 17 deletions

File tree

‎langfuse/_client/client.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ class Langfuse:
222222
223223
The hook receives one OpenTelemetry export batch. A batch is not guaranteed to contain a complete trace, request, or Langfuse observation tree. The hook usually runs on the OpenTelemetry batch span processor worker thread; during `flush()` and shutdown it may run on the caller thread. Keep it synchronous, deterministic, and fast.
224224
225-
Return `None` to leave the batch unchanged. Return `MaskOtelSpansResult` with `OtelSpanPatch` values to delete or replace attributes on selected spans. If the hook raises or returns an invalid batch result, Langfuse drops the whole export batch. If one returned span patch is invalid, Langfuse drops only that span from the Langfuse export.
225+
Return `None` to leave the batch unchanged. Return `MaskOtelSpansResult` with `OtelSpanPatch` values to delete or replace attributes on selected spans. If a batch contains duplicate trace and span identifiers, Langfuse keeps only the last matching span. If the hook raises or returns an invalid batch result, Langfuse drops the whole export batch. If one returned span patch is invalid, Langfuse drops only that span from the Langfuse export.
226226
227227
Example:
228228
```python

‎langfuse/_client/span_exporter.py‎

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -264,10 +264,9 @@ def _apply_mask_otel_spans(
264264
span_attributes: Sequence[tuple[ReadableSpan, Dict[str, AttributeValue]]],
265265
) -> Optional[list[tuple[ReadableSpan, Dict[str, AttributeValue]]]]:
266266
mask_otel_spans = cast(MaskOtelSpansFunction, self._mask_otel_spans)
267-
maskable_span_attributes: list[
268-
tuple[ReadableSpan, Dict[str, AttributeValue]]
269-
] = []
270-
span_data_by_identifier: Dict[OtelSpanIdentifier, OtelSpanData] = {}
267+
span_attributes_by_identifier: Dict[
268+
OtelSpanIdentifier, tuple[ReadableSpan, Dict[str, AttributeValue]]
269+
] = {}
271270

272271
for span, attributes in span_attributes:
273272
if not _has_valid_span_context(span):
@@ -279,22 +278,23 @@ def _apply_mask_otel_spans(
279278

280279
identifier = _create_otel_span_identifier(span)
281280

282-
if identifier in span_data_by_identifier:
283-
langfuse_logger.error(
284-
"Masking error: mask_otel_spans received duplicate span identifiers. "
285-
"Dropping export batch. "
286-
f"trace_id='{identifier.trace_id}' span_id='{identifier.span_id}'"
287-
)
288-
return None
281+
if identifier in span_attributes_by_identifier:
282+
span_attributes_by_identifier.pop(identifier)
289283

290-
span_data_by_identifier[identifier] = _create_otel_span_data(
291-
span=span, attributes=attributes, identifier=identifier
292-
)
293-
maskable_span_attributes.append((span, attributes))
284+
span_attributes_by_identifier[identifier] = (span, attributes)
285+
286+
maskable_span_attributes = list(span_attributes_by_identifier.values())
294287

295288
if not maskable_span_attributes:
296289
return []
297290

291+
span_data_by_identifier = {
292+
identifier: _create_otel_span_data(
293+
span=span, attributes=attributes, identifier=identifier
294+
)
295+
for identifier, (span, attributes) in span_attributes_by_identifier.items()
296+
}
297+
298298
try:
299299
result: Any = mask_otel_spans(
300300
params=MaskOtelSpansParams(

‎langfuse/types.py‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ class MaskOtelSpansParams:
126126
A single call receives one OpenTelemetry export batch, not necessarily a
127127
complete trace, request, or Langfuse observation tree. Batch contents depend
128128
on OpenTelemetry span processor settings such as `flush_at`,
129-
`flush_interval`, explicit `flush()`, and shutdown.
129+
`flush_interval`, explicit `flush()`, and shutdown. If a batch contains
130+
duplicate trace and span identifiers, Langfuse keeps only the last matching
131+
span before calling the masking function.
130132
131133
Example:
132134
```python

‎tests/unit/test_mask_otel_spans.py‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,83 @@ def mask_otel_spans(*, params: MaskOtelSpansParams):
574574
)
575575

576576

577+
def test_mask_otel_spans_keeps_last_duplicate_without_dropping_batch():
578+
exporter = InMemorySpanExporter()
579+
seen_params: list[MaskOtelSpansParams] = []
580+
581+
def mask_otel_spans(*, params: MaskOtelSpansParams):
582+
seen_params.append(params)
583+
duplicate_identifier = next(
584+
identifier
585+
for identifier, span in params.spans.items()
586+
if span.name == "duplicate-last"
587+
)
588+
589+
return MaskOtelSpansResult(
590+
span_patches={
591+
duplicate_identifier: OtelSpanPatch(
592+
set_attributes={"masking.applied": True}
593+
)
594+
}
595+
)
596+
597+
transforming_exporter = span_exporter_module.LangfuseTransformingSpanExporter(
598+
exporter=exporter,
599+
media_manager=None,
600+
mask_otel_spans=mask_otel_spans,
601+
)
602+
duplicate_context = SpanContext(
603+
trace_id=1,
604+
span_id=2,
605+
is_remote=False,
606+
trace_flags=TraceFlags(TraceFlags.SAMPLED),
607+
trace_state=TraceState(),
608+
)
609+
unrelated_context = SpanContext(
610+
trace_id=3,
611+
span_id=4,
612+
is_remote=False,
613+
trace_flags=TraceFlags(TraceFlags.SAMPLED),
614+
trace_state=TraceState(),
615+
)
616+
spans = [
617+
ReadableSpan(
618+
name="duplicate-first",
619+
context=duplicate_context,
620+
attributes={"attempt": 1},
621+
),
622+
ReadableSpan(
623+
name="unrelated",
624+
context=unrelated_context,
625+
attributes={"unrelated": True},
626+
),
627+
ReadableSpan(
628+
name="duplicate-last",
629+
context=duplicate_context,
630+
attributes={"attempt": 2},
631+
),
632+
]
633+
634+
result = transforming_exporter.export(spans)
635+
636+
assert result == SpanExportResult.SUCCESS
637+
assert len(seen_params) == 1
638+
assert [span.name for span in seen_params[0].spans.values()] == [
639+
"unrelated",
640+
"duplicate-last",
641+
]
642+
643+
exported_spans = exporter.get_finished_spans()
644+
assert [span.name for span in exported_spans] == [
645+
"unrelated",
646+
"duplicate-last",
647+
]
648+
assert exported_spans[1].attributes == {
649+
"attempt": 2,
650+
"masking.applied": True,
651+
}
652+
653+
577654
def test_exporter_exception_does_not_stop_background_export_thread():
578655
exporter = FailsOnceSpanExporter()
579656
media_manager, _ = _media_manager()

0 commit comments

Comments
 (0)