Skip to content

feat(backend): implement request context propagation with trace IDs - #102

Merged
Smartdevs17 merged 3 commits into
Smartdevs17:mainfrom
menawar:issue-84
Mar 24, 2026
Merged

feat(backend): implement request context propagation with trace IDs#102
Smartdevs17 merged 3 commits into
Smartdevs17:mainfrom
menawar:issue-84

Conversation

@menawar

@menawar menawar commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

feat(backend): implement request context propagation with trace IDs

Description

closes #84.

This Pull Request introduces distributed tracing semantics to the AgenticPay backend service. Prior to this PR, there was no consistent mechanism to trace logs or context across the lifecycle of a single request, making cross-service and asynchronous log debugging difficult.

This PR resolves the issue by utilizing Node.js's built-in AsyncLocalStorage to automatically track and propagate Trace IDs throughout the lifecycle of an HTTP request without needing to manually pass the trace context into every function.

Key Changes

  • Trace ID Generation: Automatically generates a completely randomized v4 UUID via crypto.randomUUID() for any request that doesn't supply a pre-existing trace ID.
  • Trace Context Propagation: Implements an Express middleware combined with AsyncLocalStorage to bind the active trace ID to the context of the running HTTP request path.
  • Console Log Injection: Overrides the global native Node variants of console.log, console.info, console.error, and console.warn to proactively lookup the concurrent trace ID and prepend it securely to all terminal outputs automatically (e.g., [TraceID: 153a7b-83ce...]).
  • HTTP Response Integration: Appends the active trace ID to all outgoing response headers via X-Trace-Id allowing the frontend clients or sibling services to align their logging schemas accordingly.

How to Test

  1. Pull down the issue-84 branch and run the backend service:
    cd backend
    npm install && npm run build
    OPENAI_API_KEY="dummy-key" npm run dev
  2. In a separate terminal, curl the health endpoint using an arbitrary Trace ID:
    curl -i -H "x-trace-id: custom-trace-1234" http://localhost:3001/health
  3. Observe the curl terminal outputs receiving the X-Trace-Id: custom-trace-1234 response header.
  4. Observe the backend server terminal automatically logging the lifecycle injected with the custom trace ID.

@vercel

vercel Bot commented Mar 24, 2026

Copy link
Copy Markdown

@menawar is attempting to deploy a commit to the smartdevs17's projects Team on Vercel.

A member of the Team first needs to authorize it.

@drips-wave

drips-wave Bot commented Mar 24, 2026

Copy link
Copy Markdown

@menawar 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! 🚀

Learn more about application limits

@Smartdevs17

Copy link
Copy Markdown
Owner

resolve merge conflicts

@Smartdevs17
Smartdevs17 merged commit 2892756 into Smartdevs17:main Mar 24, 2026
1 of 7 checks passed
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.

Backend: Add request context propagation

2 participants