1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2026-06-03 18:35:08 +02:00
Files
opentelemetry-go/sdk
Robert Pająk bec9f66b45 sdk/trace: apply AttributeValueLengthLimit to attribute.SLICE (#8217)
Fixes #7955

Per the OTel spec, attribute value limits must be applied recursively to
array elements. Previously truncateAttr only handled STRING and
STRINGSLICE.

Add a SLICE case to truncateAttr and two unexported helpers:
- truncateValue: recursively truncates STRING, STRINGSLICE, and SLICE
- needsTruncation: pre-scan guard that avoids allocating a new slice
when no element would be modified (mirrors sdk/log's pattern)

```
goarch: amd64
pkg: go.opentelemetry.io/otel/sdk/trace
cpu: 13th Gen Intel(R) Core(TM) i7-13800H
BenchmarkSpanLimits/None-20                                       205869              6757 ns/op           11296 B/op         38 allocs/op
BenchmarkSpanLimits/AttributeValueLengthLimit-20                  189799              5271 ns/op           11712 B/op         45 allocs/op
BenchmarkSpanLimits/AttributeCountLimit-20                        135494              9970 ns/op           10592 B/op         38 allocs/op
BenchmarkSpanLimits/EventCountLimit-20                            121084              9672 ns/op           10224 B/op         35 allocs/op
BenchmarkSpanLimits/LinkCountLimit-20                             110524              9419 ns/op            9824 B/op         35 allocs/op
BenchmarkSpanLimits/AttributePerEventCountLimit-20                123159              9308 ns/op           11296 B/op         38 allocs/op
BenchmarkSpanLimits/AttributePerLinkCountLimit-20                 146022              9337 ns/op           11296 B/op         38 allocs/op
```

The new `attribute.SLICE` item makes
`BenchmarkSpanLimits/AttributeValueLengthLimit` ~12% slower with 5 extra
allocs in that path compared to `main`. This is fine as 3 strings and 2
arrays need to be allocated because of the limits+truncation.

```
SpanLimits/AttributeValueLengthLimit   5.030µs ± 2%   5.651µs ± 2%  +12.34% (p=0.000 n=10)
SpanLimits/AttributeValueLengthLimit   10.53KiB ± 0% 11.44KiB ± 0% +8.61%  (p=0.000 n=10)
SpanLimits/AttributeValueLengthLimit   40.00 allocs   45.00 allocs +12.50% (p=0.000 n=10)
```
2026-04-17 18:17:45 +02:00
..
2026-04-16 12:17:21 -07:00
2024-03-26 20:13:54 +01:00