Skip to content

Commit 105fa24

Browse files
committed
fix(sdk): keep oversized entity attributes exportable
1 parent 62e24c2 commit 105fa24

3 files changed

Lines changed: 31 additions & 3 deletions

File tree

packages/traceloop-sdk/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
Traceloop’s Python SDK allows you to easily start monitoring and debugging your LLM execution. Tracing is done in a non-intrusive way, built on top of OpenTelemetry. You can choose to export the traces to Traceloop, or to your existing observability stack.
44

5+
Entity input and output attributes are bounded to 1,000,000 characters by
6+
default so oversized payloads remain visible to OTLP backends instead of being
7+
dropped as invalid attributes. Set `OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT` to
8+
use a different positive limit.
9+
510
```python
611
Traceloop.init(app_name="joke_generation_service")
712

packages/traceloop-sdk/tests/test_tasks.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,22 @@ def no_truncation_task(long_input):
206206
assert output_data == result
207207

208208

209+
def test_json_large_content_is_bounded_without_otel_limit(exporter, monkeypatch):
210+
"""Large entity payloads remain exportable when no OTel limit is configured."""
211+
monkeypatch.delenv("OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT", raising=False)
212+
213+
@task(name="large_content_task")
214+
def large_content_task():
215+
return "x" * 1_100_000
216+
217+
large_content_task()
218+
219+
span = exporter.get_finished_spans()[0]
220+
output_json = span.attributes[SpanAttributes.TRACELOOP_ENTITY_OUTPUT]
221+
assert len(output_json) == 1_000_000
222+
assert output_json.startswith('"' + ("x" * 999_999))
223+
224+
209225
def test_json_truncation_with_invalid_otel_limit(exporter, monkeypatch):
210226
"""Test that invalid OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT values are ignored"""
211227
# Set environment variable to invalid value

packages/traceloop-sdk/traceloop/sdk/decorators/base.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,27 @@
3131

3232
F = TypeVar("F", bound=Callable[..., Any])
3333

34+
# Keep entity payloads below the size at which common OTLP backends reject the
35+
# entire attribute. Users can opt into the standard OTel limit with the env var.
36+
_DEFAULT_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT = 1_000_000
37+
3438

3539
def _truncate_json_if_needed(json_str: str) -> str:
3640
"""
3741
Truncate JSON string if it exceeds OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT;
3842
truncation may yield an invalid JSON string, which is expected for logging purposes.
3943
"""
4044
limit_str = os.getenv("OTEL_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT")
45+
limit = _DEFAULT_SPAN_ATTRIBUTE_VALUE_LENGTH_LIMIT
4146
if limit_str:
4247
try:
43-
limit = int(limit_str)
44-
if limit > 0 and len(json_str) > limit:
45-
return json_str[:limit]
48+
configured_limit = int(limit_str)
49+
if configured_limit > 0:
50+
limit = configured_limit
4651
except ValueError:
4752
pass
53+
if len(json_str) > limit:
54+
return json_str[:limit]
4855
return json_str
4956

5057

0 commit comments

Comments
 (0)