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

Support: Express v5

Open vernak2539 opened this issue 1 year ago • 13 comments

Hello there! Express v5 was just released. It'd be great if we could get support for this as we're encountering errors when running with dd-trace instrumented.

I've tried to put a bunch of information below, but let me know if more is needed.

I saw this old issue from a while ago, but felt it would be better to submit a new one as v5 is now out!

Extra Information

  • express: v5.0.0
  • dd-trace: v5.22.0 (tried also with 5.12.0 prior to upgrading)

We run our app using this command:

node -r ./.env/development.cjs --import ./src/node-instrumentation.mjs .
  • ./.env/development.cjs - uses dotenv-extended to load environment variables
  • ./src/node-instrumentation.mjs - imports our datadog tracing, more below
  • . runs the main file in the package.json

The ./src/node-instrumentation.mjs looks something like this:

import tracer from 'dd-trace';

const hostname = process.env.STATSD_HOST;

if (hostname) {
  tracer.init({
    hostname: process.env.STATSD_HOST,
    service: appName,
    debug: process.env.DATADOG_DEBUG_ENABLED === 'true',
  });
  tracer.use('express', {
    service: `${appName}/express`,
    hooks: {
        // hook stuff, nothing too interesting other than setting span tags
      },
    },
  });
  tracer.use('http', { service: `${appName}/http-client` });
  tracer.use('dns', { service: `${appName}/dns` });
  tracer.use('net', { service: `${appName}/tcp` });
}

This is the error we get when running the application.

TypeError: Cannot read properties of undefined (reading 'fast_star')
    at isFastStar (/Users/testuser/repo/node_modules/dd-trace/packages/datadog-instrumentations/src/router.js:124:22)
    at Object.test (/Users/testuser/repo/node_modules/dd-trace/packages/datadog-instrumentations/src/router.js:116:17)
    at errorMiddleware (/Users/testuser/repo/node_modules/dd-trace/packages/datadog-instrumentations/src/router.js:39:27)
    at errorMiddleware (/Users/testuser/repo/node_modules/dd-trace/packages/datadog-shimmer/src/shimmer.js:31:21)
    at Layer.handleError (/Users/testuser/repo/node_modules/router/lib/layer.js:113:17)
    at trimPrefix (/Users/testuser/repo/node_modules/router/index.js:335:13)
    at /Users/testuser/repo/node_modules/router/index.js:295:9
    at processParams (/Users/testuser/repo/node_modules/router/index.js:575:12)
    at next (/Users/testuser/repo/node_modules/router/index.js:289:5)
    at Layer.handleError (/Users/testuser/repo/node_modules/router/lib/layer.js:122:5)
    at trimPrefix (/Users/testuser/repo/node_modules/router/index.js:335:13)
    at /Users/testuser/repo/node_modules/router/index.js:295:9
    at processParams (/Users/testuser/repo/node_modules/router/index.js:575:12)
    at next (/Users/testuser/repo/node_modules/router/index.js:289:5)
    at Layer.handleError (/Users/testuser/repo/node_modules/router/lib/layer.js:108:12)
    at trimPrefix (/Users/testuser/repo/node_modules/router/index.js:335:13)
    at /Users/testuser/repo/node_modules/router/index.js:295:9
    at processParams (/Users/testuser/repo/node_modules/router/index.js:575:12)
    at next (/Users/testuser/repo/node_modules/router/index.js:289:5)
    at Layer.handleError (/Users/testuser/repo/node_modules/router/lib/layer.js:108:12)
    at trimPrefix (/Users/testuser/repo/node_modules/router/index.js:335:13)
    at /Users/testuser/repo/node_modules/router/index.js:295:9
    at processParams (/Users/testuser/repo/node_modules/router/index.js:575:12)
    at next (/Users/testuser/repo/node_modules/router/index.js:289:5)
    at Layer.handleError (/Users/testuser/repo/node_modules/router/lib/layer.js:108:12)
    at trimPrefix (/Users/testuser/repo/node_modules/router/index.js:335:13)
    at /Users/testuser/repo/node_modules/router/index.js:295:9
    at processParams (/Users/testuser/repo/node_modules/router/index.js:575:12)
    at next (/Users/testuser/repo/node_modules/router/index.js:289:5)
    at Layer.handleError (/Users/testuser/repo/node_modules/router/lib/layer.js:108:12)
    at trimPrefix (/Users/testuser/repo/node_modules/router/index.js:335:13)
    at /Users/testuser/repo/node_modules/router/index.js:295:9
    at processParams (/Users/testuser/repo/node_modules/router/index.js:575:12)
    at next (/Users/testuser/repo/node_modules/router/index.js:289:5)
    at Layer.handleError (/Users/testuser/repo/node_modules/router/lib/layer.js:108:12)
    at trimPrefix (/Users/testuser/repo/node_modules/router/index.js:335:13)
    at /Users/testuser/repo/node_modules/router/index.js:295:9
    at processParams (/Users/testuser/repo/node_modules/router/index.js:575:12)
    at next (/Users/testuser/repo/node_modules/router/index.js:289:5)
    at Layer.handleError (/Users/testuser/repo/node_modules/router/lib/layer.js:108:12)
    at trimPrefix (/Users/testuser/repo/node_modules/router/index.js:335:13)
    at /Users/testuser/repo/node_modules/router/index.js:295:9
    at processParams (/Users/testuser/repo/node_modules/router/index.js:575:12)
    at next (/Users/testuser/repo/node_modules/router/index.js:289:5)
    at Layer.handleError (/Users/testuser/repo/node_modules/router/lib/layer.js:108:12)
    at trimPrefix (/Users/testuser/repo/node_modules/router/index.js:335:13)
    at /Users/testuser/repo/node_modules/router/index.js:295:9
    at processParams (/Users/testuser/repo/node_modules/router/index.js:575:12)
    at next (/Users/testuser/repo/node_modules/router/index.js:289:5)
    at Layer.handleRequest (/Users/testuser/repo/node_modules/router/lib/layer.js:154:5)

vernak2539 avatar Sep 19 '24 10:09 vernak2539

Hey everyone,

Are there any updates or plans to include this in the roadmap? Our security audit requires us to move from express@5-beta to the stable express@5 release, but we’re currently facing compatibility issues with dd-trace.

Any guidance or updates would be greatly appreciated!

Vakintoshka avatar Oct 09 '24 10:10 Vakintoshka

We are running into the same issue, any updates? Express 5.0.1..

rhyschard avatar Oct 23 '24 02:10 rhyschard

Hey everyone,

I'm wondering about the status of this, I would like to update to express 5, but this issue is blocking me. Any news on what is needed to do to support it?

porfirioribeiro avatar Jan 07 '25 10:01 porfirioribeiro

Express 5 support seems already landed in https://github.com/DataDog/dd-trace-js/pull/4913 and v5.29.0

siketyan avatar Jan 07 '25 10:01 siketyan

Express 5 support seems already landed in #4913 and v5.29.0

Ohh, my bad, i guess i missed it in the middle of the release notes, maybe this issue can be closed?

porfirioribeiro avatar Jan 07 '25 20:01 porfirioribeiro

Hi all,

Express v5 support landed with #4913 and is available in v5.29.0.

wconti27 avatar Jan 08 '25 15:01 wconti27

Just encountered this error even though I'm using V5.40.0

Hareloo avatar Mar 04 '25 07:03 Hareloo

This happens when using bull-board https://github.com/felixmosh/bull-board

annieTechno avatar Apr 30 '25 14:04 annieTechno

Seems like the support is not fully implemented in all parts of the SDK

BridgeAR avatar Apr 30 '25 14:04 BridgeAR

I'm getting:

/usr/src/mc-automation/node_modules/dd-trace/packages/datadog-instrumentations/src/helpers/bundler-register.js:31
    hooks[payload.package]()
                          ^
TypeError: hooks[payload.package] is not a function
    at /usr/src/mc-automation/node_modules/dd-trace/packages/datadog-instrumentations/src/helpers/bundler-register.js:31:27
    at Channel.publish (node:diagnostics_channel:150:9)
    at node_modules/openai/resources/chat/completions/completions.js (/usr/src/mc-automation/node_modules/openai/src/resources/chat/completions/completions.ts:1655:32)
    at __require (/usr/src/mc-automation/build/server.js:33:56)
    at node_modules/openai/resources/chat/chat.js (/usr/src/mc-automation/node_modules/openai/src/resources/chat/chat.ts:5:1)
    at __require (/usr/src/mc-automation/build/server.js:33:56)
    at node_modules/openai/resources/chat/index.js (/usr/src/mc-automation/node_modules/openai/src/resources/chat/index.ts:3:1)
    at __require (/usr/src/mc-automation/build/server.js:33:56)
    at node_modules/openai/resources/index.js (/usr/src/mc-automation/node_modules/openai/src/resources/index.ts:3:1)
    at __require (/usr/src/mc-automation/build/server.js:33:56)
Node.js v22.14.0

When my application starts with:

node --enable-source-maps --require dd-trace/init build/server.js

Using [email protected] and [email protected]

Curiously, this only happens when I deploy my application to my k8s cluster in a Docker image from 22.14-alpine in the environment where I have the Datadog agent injecting the sidecars. But this doesn't happen when I run this on my local Windows machine with the same node version and start command.

This error bears similarity to an older closed issue https://github.com/DataDog/dd-trace-js/issues/5056

I'm also using the esbuild plugin.

jerazost avatar May 09 '25 22:05 jerazost

This seems to work fine for us, but the issue is that we're getting double the traces in the APM. I tested locally and it's definitely only calling the handler once, and the Postgres queries are only being listed once so I'm pretty sure this is an issue on dd-trace rather than express, but:

Image

daviduzumeri avatar May 12 '25 15:05 daviduzumeri

For reference, here's the separation bit zoomed in. This is after upgrading from Express 4.21.2 to 5.1.0.

Image

daviduzumeri avatar May 12 '25 15:05 daviduzumeri

I'm having the opposite of the double-trace reported above. I'm not getting any at all. We use the traditional method of calling tracer.init(...) and then add the Express plugin with a very simple config we used successfully in Express v4:

   tracer.use('express', {
      blocklist: ['/status'],
      validateStatus: (code: number) => code < 400,
    });

No traces get connected in the DataDog logs (although logs themselves work fine). If at any point we call tracer.scope().active() the return result is null. This is with Express 5.1.0 and dd-trace 5.53.0.

crrobinson14 avatar May 26 '25 22:05 crrobinson14

This seems to work fine for us, but the issue is that we're getting double the traces in the APM.

Hi @daviduzumeri, I ran into the same issue and wanted to share what I found.

It looks like a single request generates two distinct spans: one with an operation name of express.request and another with router.request.

My guess is that this happens because Express v5 now uses pillarjs/router internally (see package.json). Since dd-trace-js instruments both express and router (which has its own plugin), both are creating a span for the same request, leading to the duplication.

A workaround that's been effective for me is to explicitly disable the router plugin. This stops the second span from being created.

Here’s an example of how to apply the fix after initialization:

import tracer from "dd-trace";

// Standard tracer initialization
tracer.init({
  service: "your-service-name",
});

// Add this to disable the router plugin and prevent duplicate spans
tracer.use("router", {
  enabled: false,
});

Hope this helps others who might run into this!

UPDATE:

A quick follow-up on this. The duplication issue has been officially fixed in dd-trace-js v5.66.0 (see PR #6272).

With the official fix, the express and router spans are now correctly nested. Therefore, the workaround I mentioned above is no longer necessary. In fact, if you continue to use this workaround (disabling the router plugin) with the updated version, you will prevent the router span from being sent, losing valuable context like the matched route path.

The recommended action is to upgrade to dd-trace-js v5.66.0 or later and remove the workaround.

ShinyaKato avatar Aug 01 '25 05:08 ShinyaKato

The double span events got resolved in v5.66.0. Thank you @ShinyaKato for suggesting the workaround and mentioning the fix as well!

I am closing this therefore.

@jerazost if you're still facing issues, these are related to esbuild. It is very tricky to support esbuild, while we constantly improve it and your issue is hopefully already resolved as well.

BridgeAR avatar Sep 30 '25 19:09 BridgeAR