Repository navigation
[#1289] feat(sdk): add OpenTelemetry traceparent header propagation in BridgeWatch SDK queries Repo Avatar - #1385
Merged
Conversation
…ropagation in BridgeWatch SDK queries HTTP requests made by the SDK carried no distributed-tracing headers, so integrators could not correlate an SDK call with the backend's database spans. Adds W3C Trace Context propagation to the SDK's HTTP queries. ### `sdk/src/tracecontext.ts` A self-contained implementation of the `traceparent` / `tracestate` formats: - `parseTraceparent` / `formatTraceparent` for the header, rejecting the all-zero trace/span ids, malformed lengths, non-hex characters, and (per the spec) uppercase hex. - `resolveTraceContext` decides the outgoing context. With an inbound `traceparent` it **keeps the trace id and mints a fresh span id per request** — that new span is what shows up in the backend's trace and links back to the caller. Without one it starts a new root trace. - `injectTraceHeaders` applies the headers to a `Headers` object without clobbering a `traceparent` a caller already set, and never forwards `tracestate` on its own since it is meaningless without its traceparent. ### Wiring `BridgeWatchSdkConfig.tracing` takes the inbound `traceparent` / `tracestate` (plus optional `startNewTrace` and `sampled` overrides), and `fetchCompatibility` injects the headers into its request. The existing `X-API-Version` / `Accept` headers are preserved. The config field is intentionally left out of `Required<>` — propagation is opt-in, and a forced default would misrepresent the no-tracing case. ### Tests 37 new tests: 29 unit tests over parsing, validation, id generation, and context resolution, plus 8 integration tests asserting the header actually reaches the outgoing request — including that two requests in the same trace share a trace id but get distinct span ids, and that a malformed inbound `traceparent` is replaced rather than propagated.
|
@ToryMic Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds W3C Trace Context propagation to the SDK's HTTP queries. Previously, requests made through the SDK carried no
traceparent, so an integrating application could not correlate an SDK call with the backend's database spans.sdk/src/tracecontext.tsA self-contained implementation of the
traceparent/tracestateformats:parseTraceparent/formatTraceparent— rejects all-zero trace/span ids, malformed lengths, non-hex characters, and (per the spec) uppercase hex.resolveTraceContext— with an inboundtraceparent, it keeps the trace id and mints a fresh span id per request. That new span is what appears in the backend's trace and links back to the caller. Without one it starts a new root trace.injectTraceHeaders— applies headers without clobbering atraceparentthe caller already set, and never forwardstracestatealone (it is meaningless without its traceparent).Wiring
BridgeWatchSdkConfig.tracingaccepts the inboundtraceparent/tracestateplus optionalstartNewTraceandsampledoverrides, andfetchCompatibilityinjects the headers into its request. The existingX-API-Version/Acceptheaders are preserved. The config field is deliberately excluded fromRequired<>— propagation is opt-in, and a forced default would misrepresent the no-tracing case.No backend change is needed for this to take effect end-to-end; the header is additive and ignored by anything that doesn't read it.
Testing
tracecontext.test.ts) + 8 integration (tracePropagation.test.ts)traceparentis replaced rather than propagated.Pre-existing failures (not touched here):
sdkonmainalready has 2 type errors insrc/contract.ts(result.status === "SUCCESS"where the status union has noSUCCESS, andval.bool()which does not exist onScVal) and 4 failing tests inclient.test.ts/contract.test.ts. Those counts are identical before and after this branch, so this adds no new failures. They look like genuine bugs —status === "SUCCESS"can never be true on asendTransactionresponse, andval.bool()would throw at runtime — and I'm happy to fix them in a separate PR so they don't muddy this diff.Reviewer notes
crypto.getRandomValueswhen available with aMath.randomfallback so the SDK still works on runtimes without Web Crypto.Closes #1289