test(langfuse_otel): set the span attribute limit the full-span tests run at and keep Final locals out of the app-trace loop

This commit is contained in:
yucheng 2026-10-27 00:38:53 -07:00
parent caf2ae25a2
commit de0811e4fd
3 changed files with 27 additions and 12 deletions

View file

@ -262,9 +262,17 @@ def _rig(
yield Rig(proxy, sink, openai_model, anthropic_model, embedding_model, key)
SPAN_ATTRIBUTE_LIMIT: Final = 128
@pytest.fixture(scope="module")
def v1(tmp_path_factory: pytest.TempPathFactory) -> Iterator[Rig]:
yield from _rig(tmp_path_factory, "langfuse-v1", {}, {"langfuse_default_tags": ["cache_hit"]})
yield from _rig(
tmp_path_factory,
"langfuse-v1",
{"OTEL_SPAN_ATTRIBUTE_COUNT_LIMIT": str(SPAN_ATTRIBUTE_LIMIT)},
{"langfuse_default_tags": ["cache_hit"]},
)
V2_ENVIRONMENT: Final = {"LITELLM_OTEL_V2": "1", "OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT": "span_only"}

View file

@ -347,12 +347,16 @@ def test_sdk_call_inside_an_app_trace_leaves_the_app_trace_name_and_io_alone(
output: Final = child.communicate(input=b"", timeout=60)[0].decode()
assert sorted(call for call, spans in traces.items() if _logged(spans)) == sorted(calls), output
for call, spans in traces.items():
app = {
"langfuse.trace.name": f"checkout-{marker}-{call}",
"langfuse.trace.input": json.dumps({"cart": f"{marker}-{call}"}),
"langfuse.trace.output": json.dumps({"order": f"{marker}-{call}"}),
}
root = next(span for span in spans if not span.parent_span_id)
assert {key: root.attributes.get(key) for key in app} == app, (call, root.attributes)
written = set(_trace_values(spans, (*app, "langfuse.trace.tags")))
assert written == set(app.items()), (call, written)
_assert_the_app_trace_fields_stand(marker, call, spans)
def _assert_the_app_trace_fields_stand(marker: str, call: str, spans: tuple[TraceSpan, ...]) -> None:
app: Final = {
"langfuse.trace.name": f"checkout-{marker}-{call}",
"langfuse.trace.input": json.dumps({"cart": f"{marker}-{call}"}),
"langfuse.trace.output": json.dumps({"order": f"{marker}-{call}"}),
}
root: Final = next(span for span in spans if not span.parent_span_id)
assert {key: root.attributes.get(key) for key in app} == app, (call, root.attributes)
written: Final = set(_trace_values(spans, (*app, "langfuse.trace.tags")))
assert written == set(app.items()), (call, written)

View file

@ -11,7 +11,7 @@ import pytest
from openai.types.responses import ResponseFunctionToolCall
from opentelemetry import trace
from opentelemetry.context import Context
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace import SpanLimits, TracerProvider
from opentelemetry.trace import Span
import litellm
@ -1411,6 +1411,9 @@ ERROR_KEYS: Final = (
"error.stack_trace",
)
SPAN_ATTRIBUTE_LIMIT: Final = 128
_LIMITED_TRACER: Final = TracerProvider(span_limits=SpanLimits(max_span_attributes=SPAN_ATTRIBUTE_LIMIT)).get_tracer(
"langfuse-otel-limit-tests"
)
@dataclass(frozen=True, slots=True)
@ -1420,7 +1423,7 @@ class ExportedSpan:
def _exported_full_span(filler: int, *, joins_caller: bool, failed: bool) -> ExportedSpan:
span: Final = _TRACER.start_span(
span: Final = _LIMITED_TRACER.start_span(
"litellm_request",
context=trace.set_span_in_context(trace.NonRecordingSpan(CALLER)) if joins_caller else Context(),
)