Skip to content

fix(api): preserve tracestate member order when truncating over 32 items - #7072

Open
zigzagdev wants to merge 2 commits into
open-telemetry:mainfrom
zigzagdev:fix/tracestate-truncation-order
Open

fix(api): preserve tracestate member order when truncating over 32 items#7072
zigzagdev wants to merge 2 commits into
open-telemetry:mainfrom
zigzagdev:fix/tracestate-truncation-order

Conversation

@zigzagdev

@zigzagdev zigzagdev commented Sep 6, 2026

Copy link
Copy Markdown

Which problem is this PR solving?

Please include a summary of the change and which issue is fixed. Please also include relevant motivation and context. List any dependencies that are required for this change.

TraceStateImpl (in @opentelemetry/api, api/src/trace/internal/tracestate-impl.ts) truncates tracestate entries when there are more than 32 members.
The truncation logic reversed the entry list to slice off the oldest members, but never reversed the result back before rebuilding the internal Map, breaking the map's reverse-insertion-order convention.
As a result, serialize() on a truncated TraceStateImpl emitted members in reverse order, violating the W3C tracestate ordering requirement (most recently updated member first).

Fixes # (issue)

Short description of the changes

  • Added a .reverse() call after slice() in _parse() so the internal map's insertion order is restored after truncation.
  • Extended the existing truncation test to assert serialize() preserves the original member order after truncating over-limit entries.

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Ran api/test/common/trace/tracestate.test.ts (transpile-only mocha run) — all 12 tests pass, including the new order assertion.

  • Manually reproduced the truncation logic in a standalone Node script with a 5-member tracestate truncated to 3 members, confirming the serialized order matches the expected "e=5,d=4,c=3" (previously produced "c=3,d=4,e=5").

  • Test A: npx mocha 'test/common/trace/tracestate.test.ts in api/

Checklist:

  • Followed the style guidelines of this project
  • Unit tests have been added
  • Documentation has been updated

TraceStateImpl truncated over-limit tracestate entries by slicing the
reversed entry list but never reversing it back, so the internal map's
reverse-insertion-order convention was broken and serialize() emitted
members in reverse order.
The existing truncation test only checked item count and key presence,
so the serialize() order regression went undetected.
@zigzagdev
zigzagdev requested a review from a team as a code owner September 6, 2026 06:28
@github-actions

github-actions Bot commented Sep 6, 2026

Copy link
Copy Markdown

Welcome, contributor! Thank you for your contribution to opentelemetry-js.

Important reminders:

@opentelemetry-pr-dashboard

Copy link
Copy Markdown

Pull request dashboard status

Waiting on reviewers · refreshed 2026-09-06 07:15 UTC

Review the latest changes.

Status above doesn't look right?
  • Just replied or pushed? Anything around or after the refresh time above may not be picked up yet — give it a few minutes.
  • Anything look wrong? Report it with what you expected; it helps us improve the dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant