Skip to content

Commit 49f0f6d

Browse files
DavidTrainacursoragenthassiebp
authored
perf(tracing): skip span attribute limit checks when the span has room (#2007)
* perf(tracing): skip span attribute limit checks when the span has room Every SDK attribute write runs the span attribute limit guard, which did a membership check on the span attributes for each new and reserved key. On OTel's BoundedAttributes each miss raises and catches a KeyError. - Return early when existing + new + reserved keys fit, an upper bound of the exact count, so no drop decision changes. - On the slow path, snapshot the existing keys into a set once. - Precompute the span-like observation types. - Write type, environment and release in one guarded call. * test: keep the observation type when environment and release do not fit * perf(tracing): keep only the span attribute limit early return Drop the key-set snapshot, the precomputed span-like type set and the batched observation init write. The early return carries almost all of the gain, and without the batched write the warnings match main exactly. Co-authored-by: hassieb.pakzad <hassieb.pakzad@clickhouse.com> --------- Co-authored-by: Cursor Agent <cursoragent@cursor.com> Co-authored-by: hassieb.pakzad <hassieb.pakzad@clickhouse.com>
1 parent a266fde commit 49f0f6d

2 files changed

Lines changed: 42 additions & 0 deletions

File tree

‎langfuse/_client/span.py‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ def _drop_attributes_over_span_limit(
147147
return attributes
148148

149149
existing = getattr(span, "attributes", None) or {}
150+
# Upper bound of the exact count below: if it fits, nothing is dropped.
151+
if len(existing) + len(attributes) + len(reserved_keys) <= max_attributes:
152+
return attributes
153+
150154
new_keys = [
151155
key
152156
for key, value in attributes.items()

‎tests/unit/test_metadata_attribute_limit.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,44 @@ def test_hard_guard_keeps_overwrites_and_drops_new_tail(caplog):
514514
assert "Dropped keys include: m, b_1, b_2" in message
515515

516516

517+
def _metadata_attributes(count):
518+
return {f"{METADATA_PREFIX}m_{i}": i for i in range(count)}
519+
520+
521+
@pytest.mark.parametrize(
522+
("existing", "attributes", "dropped"),
523+
[
524+
# 5 existing keys and 10 open reserved keys leave 5 metadata slots.
525+
({f"a_{i}": i for i in range(5)}, _metadata_attributes(5), []),
526+
(
527+
{f"a_{i}": i for i in range(5)},
528+
_metadata_attributes(6),
529+
[f"{METADATA_PREFIX}m_5"],
530+
),
531+
# Overwrites and reserved keys already on the span or in the same
532+
# write take no extra slot, which leaves 8 metadata slots.
533+
(
534+
{"a_0": 0, "a_1": 1, "r_0": 0, "r_1": 1, "r_2": 2},
535+
{"a_0": "new", "a_1": "new", "r_3": 3, **_metadata_attributes(8)},
536+
[],
537+
),
538+
(
539+
{"a_0": 0, "a_1": 1, "r_0": 0, "r_1": 1, "r_2": 2},
540+
{"a_0": "new", "a_1": "new", "r_3": 3, **_metadata_attributes(9)},
541+
[f"{METADATA_PREFIX}m_8"],
542+
),
543+
],
544+
)
545+
def test_metadata_budget_is_exact_at_the_limit(existing, attributes, dropped, caplog):
546+
span = _fake_span(20, existing)
547+
reserved_keys = tuple(f"r_{i}" for i in range(10))
548+
549+
result = _drop_attributes_over_span_limit(span, attributes, reserved_keys)
550+
551+
assert result == {k: v for k, v in attributes.items() if k not in dropped}
552+
assert len(_limit_warnings(caplog)) == (1 if dropped else 0)
553+
554+
517555
def test_hard_guard_never_raises():
518556
class BrokenAttributes:
519557
def __contains__(self, key):

0 commit comments

Comments
 (0)