Skip to content

Support W3C string serialization of traceparent in msgpack formatters#1445

Merged
AArnott merged 3 commits into
mainfrom
fix-1444
Jun 2, 2026
Merged

Support W3C string serialization of traceparent in msgpack formatters#1445
AArnott merged 3 commits into
mainfrom
fix-1444

Conversation

@AArnott
Copy link
Copy Markdown
Member

@AArnott AArnott commented Jun 1, 2026

Closes #1444

@AArnott AArnott added this to the v2.25 milestone Jun 1, 2026
Copilot AI review requested due to automatic review settings June 1, 2026 18:16
@AArnott AArnott requested a review from drewnoakes June 1, 2026 18:17
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds an option to serialize the W3C traceparent field as a plain string (instead of StreamJsonRpc’s compact binary MessagePack encoding) for both MessagePackFormatter and NerdbankMessagePackFormatter, and adds tests asserting the chosen serialization token type.

Changes:

  • Add TraceParentAsW3CString init-only option to both MessagePack formatters and route traceparent serialization through string/binary-specific converters/formatters.
  • Refactor TraceParent to expose a fixed serialized length constant and a WriteTo(Span<char>) helper.
  • Add unit tests validating the on-the-wire MessagePack token type for traceparent under both settings.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
test/StreamJsonRpc.Tests/NerdbankMessagePackFormatterTests.cs Adds a theory asserting traceparent serializes as Array vs String based on TraceParentAsW3CString.
test/StreamJsonRpc.Tests/MessagePackFormatterTests.cs Adds the same serialization-format assertion for MessagePackFormatter.
src/StreamJsonRpc/Protocol/TraceParent.cs Refactors string parsing/serialization helpers; introduces Length and WriteTo.
src/StreamJsonRpc/Polyfills.cs Consolidates polyfills and adds Encoding.TryGetChars extension for non-NET TFMs.
src/StreamJsonRpc/PolyfillMethods.cs Removes now-redundant polyfill type (moved into Polyfills).
src/StreamJsonRpc/NerdbankMessagePackFormatter.TraceParentConverter.cs Splits traceparent converter into binary vs string variants.
src/StreamJsonRpc/NerdbankMessagePackFormatter.cs Adds TraceParentAsW3CString option and uses it for traceparent serialization.
src/StreamJsonRpc/MessagePackFormatter.cs Adds TraceParentAsW3CString option, new traceparent formatters, and delegates traceparent serialization through them.
Comments suppressed due to low confidence (2)

src/StreamJsonRpc/NerdbankMessagePackFormatter.cs:488

  • Deserialization of traceparent/tracestate is still format-dependent: by default TraceParentConverter expects the compact binary array format, so incoming W3C strings (the scenario in #1444) will still fail unless the receiver opts into TraceParentAsW3CString. If this PR is meant to close #1444, these reads should accept both array and string encodings by inspecting reader.NextMessagePackType (and similarly accept string tracestate).
                else if (TraceParentPropertyName.TryRead(ref reader))
                {
                    TraceParent traceParent = formatter.TraceParentConverter.Read(ref reader, context);
                    result.TraceParent = traceParent.ToString();
                }
                else if (TraceStatePropertyName.TryRead(ref reader))
                {
                    result.TraceState = ReadTraceState(ref reader, context);
                }

src/StreamJsonRpc/MessagePackFormatter.cs:1547

  • JsonRpcRequest deserialization uses TraceParentFormatter/ReadTraceState unconditionally, so default settings still reject W3C string-encoded traceparent/tracestate (the interoperability issue described in #1444). If this PR closes #1444, these reads should accept both array and string encodings by switching on reader.NextMessagePackType; keep TraceParentAsW3CString as a serialization preference.
                else if (TraceParentPropertyName.TryRead(stringKey))
                {
                    TraceParent traceParent = this.formatter.TraceParentFormatter.Deserialize(ref reader, options);
                    result.TraceParent = traceParent.ToString();
                }
                else if (TraceStatePropertyName.TryRead(stringKey))
                {
                    result.TraceState = ReadTraceState(ref reader, options);
                }

Comment thread src/StreamJsonRpc/MessagePackFormatter.cs
Comment thread src/StreamJsonRpc/Protocol/TraceParent.cs Outdated
Comment thread src/StreamJsonRpc/Protocol/TraceParent.cs
AArnott added 3 commits June 1, 2026 12:28
This property causes the msgpack formatters to write out the `traceparent` as a W3C string instead of the more compact binary format we've been using.

Closes #1444
Copy link
Copy Markdown
Member

@drewnoakes drewnoakes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for fixing this so quickly Andrew.

LGTM. Could add some tests covering parsing failures.

@AArnott AArnott merged commit 60495bb into main Jun 2, 2026
9 checks passed
@AArnott AArnott deleted the fix-1444 branch June 2, 2026 01:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NerdbankMessagePackFormatter should accept plain W3C strings for traceparent/tracestate

4 participants