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

Express plugin not working

Open suhjohn opened this issue 1 year ago • 1 comments

I have an Express 4 app with dd-trace.

tracer.init is happening first thing in the code.

However, when I make an API request, top level request is http component not express, and everything is collapsed under the http method instead of the request path.

Screenshot 2024-02-07 at 5 00 21 PM Screenshot 2024-02-07 at 5 00 46 PM

To override this, tried to add a hook:

tracer.use('http', {
  hooks: {
    request: (span, req, _) => {
      if (span !== undefined) {
        if (req instanceof IncomingMessage) {
          logger.info(
            `[tracer.ts] req instanceof IncomingMessage setting resource.name - Span: ${span} req: ${req.url}`,
          );
        } else if (req instanceof ClientRequest) {
          logger.info(
            `[tracer.ts] req instanceof ClientRequest setting resource.name - Span: ${span} req: ${req.path}`,
          );
        }
      }
      if (req instanceof IncomingMessage) {
        logger.info(
          `[tracer.ts] req instanceof IncomingMessage span is undefined - req: ${req.url}`,
        );
      } else if (req instanceof ClientRequest) {
        logger.info(
          `[tracer.ts] req instanceof ClientRequest span is undefined - req: ${req.path}`,
        );
      }
    },
  },
});

This caused the application to crash with the following issue:

2024-02-08 00:24:32 [server] error: TypeError: Cannot read properties of undefined (reading 'length')

at DatadogSpan.toString (/usr/src/app/node_modules/dd-trace/packages/dd-trace/src/opentracing/span.js:100:35)

at Object.request (file:///usr/src/app/server/build/src/config/tracer.js:27:109)

at Object.finishSpan (/usr/src/app/node_modules/dd-trace/packages/dd-trace/src/plugins/util/web.js:302:26)

at Object.finishAll (/usr/src/app/node_modules/dd-trace/packages/dd-trace/src/plugins/util/web.js:316:9)

at HttpServerPlugin.finish (/usr/src/app/node_modules/dd-trace/packages/datadog-plugin-http/src/server.js:66:9)

at /usr/src/app/node_modules/dd-trace/packages/dd-trace/src/plugins/tracing.js:73:22

at Subscription._handler (/usr/src/app/node_modules/dd-trace/packages/dd-trace/src/plugins/plugin.js:14:9)

at Channel.publish (node:diagnostics_channel:56:9)

at ServerResponse.emit (/usr/src/app/node_modules/dd-trace/packages/datadog-instrumentations/src/http/server.js:40:22)

at onFinish (node:_http_outgoing:1001:10)

Is that the right way to use hooks? How can I override the resource.name to something more sane?

Tried both ddtrace 4.26 and 5.2, and both don't work.

suhjohn avatar Feb 08 '24 01:02 suhjohn

https://github.com/DataDog/dd-trace-js/issues/3595

suhjohn avatar Feb 08 '24 02:02 suhjohn