feat(x402): add optional tracing hooks for payment flow observability - #372
Closed
odarome132 wants to merge 1 commit into
Closed
feat(x402): add optional tracing hooks for payment flow observability#372odarome132 wants to merge 1 commit into
odarome132 wants to merge 1 commit into
Conversation
Add span start/end hooks at each internal boundary of the x402 payment flow, allowing consumers to get end-to-end visibility without importing a specific tracing library. The trace context is threaded through the flow and consumers decide how to propagate it. New module: x402-tracing.ts (zero-cost when unconfigured). Spans: request, decode-requirements, select-requirements, build-payment, sign-auth-entry, paid-retry, read-settlement. Tests cover success, error, and full-flow capture paths. README updated with observability section.
|
@odarome132 is attempting to deploy a commit to the david's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
Thanks for the contribution — but contributor PRs may only change files inside This PR touches file(s) outside that folder:
Please open a new PR with your changes scoped to Questions? Ask in the Telegram group. |
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.
Closes #250
Summary
Adds optional span start/end hooks at each internal boundary of the x402 payment flow, giving consumers end-to-end visibility into the full lifecycle of a payment (request → decode → select → build → sign → retry → settle) without importing a specific tracing library. Hooks are zero-cost when unconfigured.
What Changed
src/x402-tracing.ts(new)X402TracingHooks(onSpanStart/onSpanEnd),X402TraceContext(traceId+ optionalattributes),SpanName(7 phases),tracedSpan()helper,generateTraceId()src/x402-types.tsX402FetchInit.traceper-request context;X402Client.tracingHookssurfacesrc/x402-client.tstracedSpanat all 7 boundaries; threads the same trace context through the flowsrc/x402-facade.tstracingHooksto the clientsrc/index.tssrc/x402-tracing.test.ts(new)ok=false+ error), non-Error wrapping, duration, meta, trace propagation, full-flow capturesrc/x402-client.test.tsinit.trace)README.mdKey Design Decisions
SpanStartEvent/SpanEndEventplain objects (span name, trace context, duration, error); consumers map them into OpenTelemetry, Datadog, or their own logger. No tracing dependency added.tracedSpanshort-circuits to the rawfn(): no timing, no event allocation.traceIdinto OTel span context).onSpanEndis always called, withok:falseplus the error, and the original error is re-thrown unchanged (non-Error throws are wrapped for the event only).Acceptance Criteria
onSpanStart/onSpanEndfire at all 7 boundaries (x402.request,x402.decode-requirements,x402.select-requirements,x402.build-payment,x402.sign-auth-entry,x402.paid-retry,x402.read-settlement); covered byx402-tracing.test.tsand thex402-client.test.tstracing-integration suite.X402TraceContext(per-requestinit.trace) is threaded through every span; covered by the propagation tests in both test files.Test Output
Focused suites:
src/x402-tracing.test.ts(14 tests) andsrc/x402-client.test.ts(22 tests incl. 5 tracing-integration) all pass.npm run typecheckandnpm run check:docspass. Note: this repo does not configure a coverage provider or threshold (no@vitest/coverage-*installed, nocoverageblock invitest.config.ts, none stated in the issue), so no coverage number is reported — adding@vitest/coverage-v8+ a threshold for the x402 modules would be a separate change.Honest Follow-ups
tracedSpanusesperformance.now()(fine for browsers and Node ≥ 8.5); no shim is included for exotic runtimes.Security Note
No new permissions, network calls, or secrets are introduced. Hooks receive only span metadata (names, durations, errors, and the consumer-supplied trace context) — never key material or the signed payment payload. When unconfigured, the feature is a strict no-op.
This repo's
.github/workflows/close-prs-outside-contrib.ymlauto-closes external PRs that touch files outsidecontrib/. Issue #250's acceptance criteria require SDK-source changes (src/,README.md), so this PR necessarily touches files outsidecontrib/. Two earlier attempts at this issue (PRs #315 and #316) were auto-closed by that guard for exactly this reason — not by a maintainer. Percontrib/README.md, contributors whose assigned issue genuinely requires changes outsidecontrib/are directed to say so on the issue; maintainers may need to exempt this PR or review it manually. Happy to move/scope the work however the maintainers prefer.