← Back to Pull requests

sdk/trace: apply AttributeValueLengthLimit to event and link attributes #9523

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Nov 17, 2026 20:22 UTC
Author

Fixes #9514

Summary

SpanLimits.AttributeValueLengthLimit previously applied only to span attributes (SetAttributes via normAttr), while addEvent (AddEvent, RecordError) and AddLink (trace.WithLinks, span.AddLink) normalized nested maps and attribute depth without truncating attribute values.

Add normAttrs and normalizeLimitedAttrs in sdk/trace so event and link attributes apply per-event/per-link count limits first and then run only retained attributes through normAttr when AttributeValueLengthLimit >= 0:

  • truncates string values on UTF-8 rune boundaries, byte slices by byte length, and composite slice/map values recursively (0 truncates to empty, < 0 remains unlimited via KeyValuesDedupLimitDepth);

  • applies AttributePerEventCountLimit / AttributePerLinkCountLimit before value normalization, recording DroppedAttributeCount from the original length and avoiding truncation work on discarded attributes (preserving negative unlimited counts and caller-slice immutability);

  • skips allocating when retained values already fit within limits;

  • updates the SpanLimits.AttributeValueLengthLimit documentation and CHANGELOG.md.
  • Benchmarks

    Base: 34915e1 (semconv: adopt v1.45.0) + sdk/trace/attr_limit_bench_test.go only.
    Head: 18a920f7 (cd sdk && go test -bench=BenchmarkAttributeLimitNormalization -benchmem -count=6 -run='^$' ./trace/, Go 1.27.1, darwin/arm64, Apple M4 Max).

    goos: darwin
    goarch: arm64
    pkg: go.opentelemetry.io/otel/sdk/trace
    cpu: Apple M4 Max
                                                                            │    base.txt    │              head.txt               │
                                                                            │     sec/op     │     sec/op      vs base             │
    AttributeLimitNormalization/AddEvent/length=finite/retained-16               130.3n ± 9%    9766.5n ± 10%   +7392.52% (p=0.002 n=6)
    AttributeLimitNormalization/AddEvent/length=finite/count=0-16                138.2n ± 3%     105.6n ±  6%     -23.59% (p=0.002 n=6)
    AttributeLimitNormalization/AddEvent/length=finite/discard-suffix-16         109.3n ± 4%     133.2n ±  3%     +21.91% (p=0.002 n=6)
    AttributeLimitNormalization/AddEvent/length=unlimited/retained-16            135.8n ± 3%     134.2n ±  2%           ~ (p=0.240 n=6)
    AttributeLimitNormalization/AddEvent/length=unlimited/count=0-16             135.2n ± 4%     108.1n ±  1%     -20.07% (p=0.002 n=6)
    AttributeLimitNormalization/AddEvent/length=unlimited/discard-suffix-16      109.9n ± 2%     107.0n ±  5%      -2.64% (p=0.002 n=6)
    AttributeLimitNormalization/AddLink/length=finite/retained-16                38.55n ± 2%   9141.50n ±  4%  +23610.28% (p=0.002 n=6)
    AttributeLimitNormalization/AddLink/length=finite/count=0-16                 38.77n ± 1%     14.46n ±  2%     -62.70% (p=0.002 n=6)
    AttributeLimitNormalization/AddLink/length=finite/discard-suffix-16          26.79n ± 1%     45.37n ±  1%     +69.34% (p=0.002 n=6)
    AttributeLimitNormalization/AddLink/length=unlimited/retained-16             38.59n ± 1%     39.22n ±  1%      +1.62% (p=0.024 n=6)
    AttributeLimitNormalization/AddLink/length=unlimited/count=0-16              38.91n ± 2%     14.48n ±  1%     -62.79% (p=0.002 n=6)
    AttributeLimitNormalization/AddLink/length=unlimited/discard-suffix-16       26.74n ± 1%     21.03n ±  1%     -21.35% (p=0.002 n=6)
    AttributeLimitNormalization/SetAttributes/length=finite/deferred-16          804.9n ± 6%     819.8n ±  5%           ~ (p=0.589 n=6)
    AttributeLimitNormalization/SetAttributes/length=finite/eager-16             1.006µ ± 4%     1.060µ ±  5%      +5.42% (p=0.006 n=6)
    AttributeLimitNormalization/SetAttributes/length=unlimited/deferred-16       383.7n ± 2%     406.3n ±  5%      +5.89% (p=0.002 n=6)
    AttributeLimitNormalization/SetAttributes/length=unlimited/eager-16          582.5n ± 1%     585.5n ±  3%           ~ (p=0.310 n=6)
    geomean                                                                      116.5n          191.1n           +63.98%
    
                                                                            │    base.txt    │             head.txt              │
                                                                            │      B/op      │     B/op      vs base             │
    AttributeLimitNormalization/AddEvent/length=finite/retained-16              296.0 ± 0%     55848.0 ± 0%  +18767.57% (p=0.002 n=6)
    AttributeLimitNormalization/AddEvent/length=finite/count=0-16               296.0 ± 0%       296.0 ± 0%           ~ (p=1.000 n=6)
    AttributeLimitNormalization/AddEvent/length=finite/discard-suffix-16        168.0 ± 0%       232.0 ± 0%     +38.10% (p=0.002 n=6)
    AttributeLimitNormalization/AddEvent/length=unlimited/retained-16           296.0 ± 0%       296.0 ± 0%           ~ (p=1.000 n=6)
    AttributeLimitNormalization/AddEvent/length=unlimited/count=0-16            296.0 ± 0%       296.0 ± 0%           ~ (p=1.000 n=6)
    AttributeLimitNormalization/AddEvent/length=unlimited/discard-suffix-16     168.0 ± 0%       168.0 ± 0%           ~ (p=1.000 n=6)
    AttributeLimitNormalization/AddLink/length=finite/retained-16              0.00Ki ± 0%     54.25Ki ± 0%           ? (p=0.002 n=6)
    AttributeLimitNormalization/AddLink/length=finite/count=0-16                0.000 ± 0%       0.000 ± 0%           ~ (p=1.000 n=6)
    AttributeLimitNormalization/AddLink/length=finite/discard-suffix-16          0.00 ± 0%       64.00 ± 0%           ? (p=0.002 n=6)
    AttributeLimitNormalization/AddLink/length=unlimited/retained-16            0.000 ± 0%       0.000 ± 0%           ~ (p=1.000 n=6)
    AttributeLimitNormalization/AddLink/length=unlimited/count=0-16             0.000 ± 0%       0.000 ± 0%           ~ (p=1.000 n=6)
    AttributeLimitNormalization/AddLink/length=unlimited/discard-suffix-16      0.000 ± 0%       0.000 ± 0%           ~ (p=1.000 n=6)
    AttributeLimitNormalization/SetAttributes/length=finite/deferred-16       2.328Ki ± 0%     2.328Ki ± 0%           ~ (p=1.000 n=6)
    AttributeLimitNormalization/SetAttributes/length=finite/eager-16          2.078Ki ± 0%     2.078Ki ± 0%           ~ (p=1.000 n=6)
    AttributeLimitNormalization/SetAttributes/length=unlimited/deferred-16      848.0 ± 0%       848.0 ± 0%           ~ (p=1.000 n=6)
    AttributeLimitNormalization/SetAttributes/length=unlimited/eager-16         592.0 ± 0%       592.0 ± 0%           ~ (p=1.000 n=6)
    
                                                                            │    base.txt    │           head.txt            │
                                                                            │   allocs/op    │ allocs/op   vs base           │
    AttributeLimitNormalization/AddEvent/length=finite/retained-16              3.000 ± 0%     7.000 ± 0%  +133.33% (p=0.002 n=6)
    AttributeLimitNormalization/AddEvent/length=finite/count=0-16               3.000 ± 0%     3.000 ± 0%         ~ (p=1.000 n=6)
    AttributeLimitNormalization/AddEvent/length=finite/discard-suffix-16        3.000 ± 0%     4.000 ± 0%   +33.33% (p=0.002 n=6)
    AttributeLimitNormalization/AddEvent/length=unlimited/retained-16           3.000 ± 0%     3.000 ± 0%         ~ (p=1.000 n=6)
    AttributeLimitNormalization/AddEvent/length=unlimited/count=0-16            3.000 ± 0%     3.000 ± 0%         ~ (p=1.000 n=6)
    AttributeLimitNormalization/AddEvent/length=unlimited/discard-suffix-16     3.000 ± 0%     3.000 ± 0%         ~ (p=1.000 n=6)
    AttributeLimitNormalization/AddLink/length=finite/retained-16               0.000 ± 0%     4.000 ± 0%         ? (p=0.002 n=6)
    AttributeLimitNormalization/AddLink/length=finite/count=0-16                0.000 ± 0%     0.000 ± 0%         ~ (p=1.000 n=6)
    AttributeLimitNormalization/AddLink/length=finite/discard-suffix-16          0.000 ± 0%     1.000 ± 0%         ? (p=0.002 n=6)
    AttributeLimitNormalization/AddLink/length=unlimited/retained-16            0.000 ± 0%     0.000 ± 0%         ~ (p=1.000 n=6)
    AttributeLimitNormalization/AddLink/length=unlimited/count=0-16             0.000 ± 0%     0.000 ± 0%         ~ (p=1.000 n=6)
    AttributeLimitNormalization/AddLink/length=unlimited/discard-suffix-16      0.000 ± 0%     0.000 ± 0%         ~ (p=1.000 n=6)
    AttributeLimitNormalization/SetAttributes/length=finite/deferred-16         6.000 ± 0%     6.000 ± 0%         ~ (p=1.000 n=6)
    AttributeLimitNormalization/SetAttributes/length=finite/eager-16            6.000 ± 0%     6.000 ± 0%         ~ (p=1.000 n=6)
    AttributeLimitNormalization/SetAttributes/length=unlimited/deferred-16      3.000 ± 0%     3.000 ± 0%         ~ (p=1.000 n=6)
    AttributeLimitNormalization/SetAttributes/length=unlimited/eager-16         3.000 ± 0%     3.000 ± 0%         ~ (p=1.000 n=6)
    

    Performance impact

  • Expected cost of truncating retained values: With a finite AttributeValueLengthLimit and a retained 1,024-element string slice of oversized strings, AddLink now actually truncates the slice (54.25 KiB / 4 allocs/op, 9.14 µs) and AddEvent adds the same truncation cost on top of EventConfig's 296 B / 3 allocs copy. Base skipped value-length truncation on events and links altogether. With unlimited AttributeValueLengthLimit (-1), retained AddEvent and AddLink remain flat (0 B / 0 allocs/op for AddLink, unchanged 296 B / 3 allocs/op for AddEvent).

  • Avoidable work eliminated on count-discarded attributes: Applying AttributePerEventCountLimit / AttributePerLinkCountLimit before normalization avoids walking or allocating discarded attributes. When count=0, AddLink stays at 0 B / 0 allocs/op and drops from 38.8 ns to 14.5 ns across both finite and unlimited length limits; AddEvent drops from 138 ns to 106 ns with no extra allocations above EventConfig. In discard-suffix (keeping 1 oversized string and dropping the 1,024-element slice), AddLink pays only 64 B / 1 alloc/op (45.4 ns) for the retained string instead of copying the discarded 54 KiB slice.

  • Regular SetAttributes (deferred and eager deduplication): Allocations and bytes/op are identical across all four cases (finite/unlimited × deferred/eager), with timing flat within noise.
  • Verification

  • cd sdk && go test -race -count=1 ./trace/... ./internal/attrnorm/... ./log/...

  • make golangci-lint/./sdk golangci-lint/./sdk/log misspell

  • cd sdk && go test -bench=BenchmarkAttributeLimitNormalization -benchmem -count=6 -run='^$' ./trace/
  • Diff: 650 lines (+487 / -163) across 8 files Reviewer: MrAlias Review duration: 19418m
    All Vouch review confidence checks passed
    Residual risk 0.28 = Change Risk (0.52) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #9523"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 27.7% · Safe to merge
    27.7%