dd-trace-js icon indicating copy to clipboard operation
dd-trace-js copied to clipboard

[feature-request]: `node-fetch` doesn't create `http.request` spans

Open samwho opened this issue 8 months ago • 2 comments

Tracer Version(s)

5.56.0

Node.js Version(s)

22.15.0

Bug Report

I'm not seeing http.request spans being created from fetch requests using the node-fetch library. Is this not supported?

Using node's built in fetch (e.g. removing the first line from index.ts below) works.

Here's a screenshot of spans created with and without the node-fetch import. That singular http.request span is when I tried it without node-fetch. With node-fetch all I get is tcp and dns spans.

Image

Reproduction Code

index.ts

import fetch from 'node-fetch';
import tracer from 'dd-trace';

tracer.init({
    service: "sam-test",
    env: "local",
    // @ts-ignore
    debug: true,
    logLevel: "debug",
    startupLogs: true,
    plugins: true,
});

async function main() {
    const response = await fetch("http://localhost:8080");
    console.log("Response status:", response.status);
}

main()

package.json

{
  "name": "lab",
  "module": "index.ts",
  "type": "module",
  "devDependencies": {
    "@types/bun": "latest"
  },
  "peerDependencies": {
    "typescript": "^5.0.0"
  },
  "dependencies": {
    "@types/node-fetch": "^2.6.12",
    "dd-trace": "^5.49.0",
    "graphql": "^16.11.0",
    "node-fetch": "2.6.7"
  }
}

tsconfig.json

{
  "compilerOptions": {
    // Enable latest features
    "lib": ["ESNext", "DOM"],
    "target": "ESNext",
    "module": "ESNext",
    "moduleDetection": "force",
    "jsx": "react-jsx",
    "allowJs": true,

    // Bundler mode
    "moduleResolution": "bundler",
    "allowImportingTsExtensions": true,
    "verbatimModuleSyntax": true,
    "noEmit": true,

    // Best practices
    "strict": true,
    "skipLibCheck": true,
    "noFallthroughCasesInSwitch": true,

    // Some stricter flags (disabled by default)
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "noPropertyAccessFromIndexSignature": false
  }
}

Run:

$ bun install
$ bun build index.ts --target node > index.js
$ node index.js

Output should be:

(node:16485) [DEP0040] DeprecationWarning: The `punycode` module is deprecated. Please use a userland alternative instead.
(Use `node --trace-deprecation ...` to show where the warning was created)
Response status: 200
DATADOG TRACER CONFIGURATION - {"date":"2025-06-19T11:08:01.745Z","os_name":"Darwin","os_version":"24.5.0","architecture":"arm64","version":"5.56.0","lang":"nodejs","lang_version":"22.15.0","env":"local","service":"sam-test","agent_url":"http://127.0.0.1:8126","debug":false,"sampling_rules":[],"tags":{"service":"sam-test","env":"local","runtime-id":"5f07258a-9063-4acf-a496-6e78c2b0a14b","_dd.rc.client_id":"554be8c3-e799-4df0-abdc-2fab1c0e70ee"},"log_injection_enabled":false,"runtime_metrics_enabled":false,"profiling_enabled":false,"integrations_loaded":["dns","net"],"appsec_enabled":false}

Error Logs

No response

Tracer Config

No response

Operating System

No response

Bundling

Unsure

samwho avatar Jun 19 '25 11:06 samwho

dd-trace-js has no support for node-fetch so far. We do support the native Node.js fetch, so that's why the traces show up when using that instead. Is there a strong reason for using node-fetch instead of the native fetch?

BridgeAR avatar Jun 22 '25 16:06 BridgeAR

At present, nock doesn't support native fetch. They're adding support in the next major version but until then I was hoping to use node-fetch.

samwho avatar Jun 22 '25 18:06 samwho

Thanks, that makes sense. I don't think we will be able to introduce support for it anytime soon though (if at all). The only way I see it happening anytime soon would be to open a PR that instruments node-fetch by yourself, in case you would be up for that.

You could also reach out to our support to try to get a higher prioritization: https://www.datadoghq.com/support

BridgeAR avatar Jun 23 '25 22:06 BridgeAR

I appreciate that, thanks so much for replying. I'll close this out while we think about what to do next.

samwho avatar Jun 24 '25 11:06 samwho