← Back to Pull requests

fix: apply attribute limits to log record and span processor attributes, including nested values #1343

Open
unknown opened this pull request with 650 lines diff across 8 files
View on GitHub
unknown unknown commented on Dec 02, 2026 19:29 UTC
Author

Attribute limits for log records and spans

Author @williazz · Reviewers: code owners · Status: ready for review · Date: 2026-12-04 · Resolves #1336

Baseline is main at f299ff7c; the proposal is head 633132aa. Tests ran on 2026-12-03. This replaces the earlier Summary/Testing description.

Background

!Today on main f299ff7c: five attribute writers each pass a count-limit stage and a length-limit stage before export. The log record builder evicts the oldest attribute and truncates only top-level strings and one level of arrays. Log record processors bypass both limits. The span builder and samplers evict the oldest and skip the length limit. Span.setAttribute counts updates as new attributes and truncates only top-level strings. Event and exception attributes evict the oldest.

Five writers set attributes, each against LogLimits or SpanLimits (count 128, length unlimited by default):

1. LogRecordBuilder.setAttribute stores into an AttributesDictionary built with both limits.
2. A log record processor, such as SessionLogRecordProcessor, calls ReadableLogRecord.setAttribute, which writes straight into a dictionary.
3. SpanBuilder.setAttribute and sampler attributes go into an AttributesDictionary built with the count limit only.
4. Span.setAttribute, including calls from SpanProcessor.onStart, runs SpanSdk.setAttribute.
5. Event and exception attributes go into a per-event AttributesDictionary.

AttributesDictionary evicts the oldest key when full and truncates top-level strings and one level of arrays.

Problem statement

Every writer breaks the common attribute limits somewhere, as the pink cells above show. The spec says:

if it is an array of strings, then apply the limit to each value within the array separately, if it is an array of AnyValue, then apply the limit to each element of the array separately (and recursively), if it is a map, then apply the limit to each value within the map separately (and recursively) … if adding an attribute to an attribute collection would result in exceeding the limit … the SDK MUST discard that attribute

and binds spans to them: "Span attributes MUST adhere to the common rules of attribute limits."

Concretely, SessionLogRecordProcessor adds session.id to a provider built with maxAttributeCount: 0; a nested string or a deprecated .stringArray passes any length limit; and a span past its count limit drops its first attributes, which usually carry the most context. The cause is that each writer enforces limits by its own code path.

A solution must:

1. Apply count and length limits to attributes from every writer, including processors.
2. Truncate strings at any depth, and in .stringArray.
3. Discard the new attribute past the count limit, keeping existing ones.
4. Keep the public API, and decode records the persistence exporter wrote with the previous release.

Out of scope

  • Event and exception attribute length. Unlimited before and after; a separate change if wanted.

  • Link attribute limits. AttributePerLinkCountLimit is unused on main; a separate issue.

  • logLimits: on UserLogRecordProcessor and VitalsLogRecordProcessor. Redundant after this lands; removed in a follow-up (#1332, #1333).
  • Proposal

    !Proposed in #1343: the same five writers and stages. Every count stage, marked CHANGED, discards a new attribute once the limit is reached, and Span.setAttribute counts distinct keys. Every length stage except event and exception attributes, marked CHANGED, truncates strings recursively through arrays, maps and the deprecated stringArray.

    What changes

    | Writer | Today | Proposed |
    | --- | --- | --- |
    | Log record builder | evicts oldest; shallow truncation | discards new; recursive |
    | Log record processor | no limits | discards new; recursive |
    | Span builder, sampler | evicts oldest; no length limit | discards new; recursive |
    | Span.setAttribute | counts updates; top-level strings | counts distinct keys; recursive |
    | Event, exception | evicts oldest | discards new |

    How truncation works

    One internal AttributeValue.truncated(toLength:) recurses through .array and .set and truncates each .stringArray string, keeping the case. LogLimits, SpanSdk, SpanBuilderSdk and AttributesDictionary all call it. With a limit of 3, .array([.array(["abcdef"])]) becomes .array([.array(["abc"])]).

    How the count limit discards

    ReadableLogRecord now carries the LogLimits it was built with and applies them in setAttribute, so processor writes obey them, and the builder writes through the same rule. AttributesDictionary.updateValue returns early for a new key once full. recordException adds the exception's own attributes first, so they keep their slots.

    Ship it

    One PR, six commits. logLimits stays out of Codable, so records persisted by the previous release still decode. swift test --enable-all-traits: 1397 tests, 0 failures on Linux (main: 1386); macOS and the iOS 27 simulator pass on Revyl; all upstream checks pass.

    Risks

  • Callers relying on eviction. The session-sampling tests and the log-limits demo scenario set session.id before the sampling decision; they now set the decision first, assertions unchanged. make integ-tests-ios has not run on the reordered scenario.

  • Oversized setAttributes calls. Dictionary order decides which keys survive, as with eviction.

  • One new deprecation warning from matching .stringArray in the SDK.
  • Alternate solutions

    Do nothing

    Processor attributes stay unbounded, and every processor needs its own logLimits: parameter, as #1332 and #1333 added.

    Limit inside each processor

    What #1332 and #1333 do. It leaves third-party processors and spans uncovered.

    Convert .stringArray to .array when truncating

    Avoids the warning but changes the value callers read back.

    Glossary

  • AttributesDictionary: the SDK's bounded attribute map behind spans, span builders and events.

  • Count limit: maxAttributeCount or attributeCountLimit, the most top-level attributes a record or span keeps.

  • Length limit: maxAttributeLength or attributeValueLengthLimit, the most characters a string value keeps.

  • Processor: a LogRecordProcessor or SpanProcessor that sees each record or span before export.
  • written by billy's 🤖

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: aranhave Review duration: 3594m
    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 #1343"
    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%