Skip to content

[BUG]: missing trace headers on fetch requests when using Request object and RequestInit with headers #6129

@anakorn-wm

Description

@anakorn-wm

Tracer Version(s)

5.57.1

Node.js Version(s)

22.17.0

Bug Report

When using the dd-trace fetch plugin, fetch requests made using a Request object and with headers on the RequestInit object result in HTTP requests with missing trace headers.

In comparison, fetch requests made with a string URL and with headers on the RequestInit object are working as expected, and trace headers are present on the resulting HTTP request.

import ddTrace from 'dd-trace';

ddTrace.init();
ddTrace.tracer.use('fetch');

const url = 'https://petstore.swagger.io/v2/pet/1';

const request = new Request(url);
const init = { headers: { foo: 'bar' } };

// trace headers are missing, e.g. `x-datadog-trace-id`, `x-datadog-parent-id`, `x-datadog-tags`
await fetch(request, init);

// trace headers are present
await fetch(url, init);

I'm also able to replicate the issue by swapping in a Request object in place of the string URL in the below test:

fetch(`http://localhost:${port}/user?foo=bar`, { headers: { foo: 'bar' } })

i.e. using this instead:

          const req = new globalThis.Request(`http://localhost:${port}/user?foo=bar`)
          fetch(req, { headers: { foo: 'bar' } })

I'm not very familiar with dd-trace-js code, but I'm able to fix the issue by removing this snippet from the fetch instrumentation code:

if (input instanceof Request) {
const ctx = { req: input }
return ch.tracePromise(() => fetch.call(this, input, init), ctx)
}

But I realize this was added to address #4258 where errors were being produced when Request objects were being reused, so I'm not sure about the right path forward here.

Reproduction Code

(attached on bug report)

Error Logs

No response

Tracer Config

No response

Operating System

Darwin Kernel Version 24.5.0

Bundling

No Bundling

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions