@newrelic/winston-enricher doesn't add span.id or trace.id info

Not able to enrich data

Hi,

I’m trying to follow this doc: Logs in context for Node.js

But I don’t get span.id or trace.id or anything like that.

log I’m getting

info: HTTP GET / {"meta":{"req":{"url":"/","headers":{"host":"localhost:8080","connection":"keep-alive","pragma":"no-cache","cache-control":"no-cache","dnt":"1","upgrade-insecure-requests":"1","user-agent":"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36","accept":"text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9","sec-fetch-site":"none","sec-fetch-mode":"navigate","sec-fetch-user":"?1","sec-fetch-dest":"document","accept-encoding":"gzip, deflate, br","accept-language":"en-US,en;q=0.9,it-IT;q=0.8,it;q=0.7,es;q=0.6","cookie":"session=.eJw9U8uO2zAM_BVC52A_IKduHsVimwRpskUORRHQMjciIlMGJSd1i_57KWfbk80RHzND6bc796QdCklx86IDzRyKpEE8ublzM0cdcjwPmdTiiKrjpyYmeWrpZqecz9h2LG7-jjFbsWBH2c2_u0PKBG8v68P6-GyJW_YBKcJSuVCMbNBzwwqLgKWgnek4Ys3DK33LJ0uBKSoFtn6JXaPcXsiglT7BAUf4jA0P2YAN3pWMLmyI86A15zVpayoInqUlzUlqrxVGA8qVpVbZiRICLFBLuNs4ww6pIS2GkQopTI2CwOKOyrXt0YjSCMuAcqkNiXq8Wph6xSkbvYXJB0Wx0PI0UjbE9E5Dj4X6QAIGJbO90oj0E40KwsqK6NEV1i3LpaTaZMUoCC9kQirFfeDIPbwk49TWY9P2moRq9wM15D3CxtwyHYYsUgNfhC-hBscho5hJWPu8cgdb9Psw2WPADsWPBo32_6aTzq3fYYf62Ip62Cblqdhk3Ai2g7DwZOyNW9hZjQUnNrHYwS7dPtb8j9Yeiw_d5MyGjPaepeRUL9GOfYK9JrlO67QrBl8H9jVaoqZo--7r5alESKnlEuBAQ0Pyf_oRm1jHr9vBjEkWiw_0q44fTfWROzOplOnaxQoUoth9rIl0jIZoGqqlx8c-Tshy1w_v9mE0MRlOnMn9mDmlPo72Hh7fmavP41zGvr6Zjqr97s9fiTgcVQ.X3ywtw.fm0TFKcGlNN8cFF-BZwAtoRVXPU; NG_TRANSLATE_LANG_KEY=%22fr%22; connect.sid=s%3Ads7evoJuiD9OFbiDhoeoQoJ8Arosi-1Q.jtATESzAsSCPaWXqje01FA8fvzi3ozysdOhqve%2BgGjk; io=thY2qOlVvvU5hoIFAAAA; paletteMode=dark"},"method":"GET","httpVersion":"1.1","originalUrl":"/","query":{}},"res":{"statusCode":200},"responseTime":13}}
 npm list --depth=0 | grep 'newrelic\|winston'
├── @newrelic/winston-enricher@1.0.0
├── express-winston@4.0.5
├── newrelic@6.3.0
├── winston@3.3.3

winson.js


const winston = require('winston');
const expressWinston = require('express-winston');
const newrelicFormatter = require('@newrelic/winston-enricher');

const options = {
  // file for /etc/newrelic-infra/logging.d/nodejs.yml
  file: {
    filename: 'logs/app.log',
    format: winston.format.combine(newrelicFormatter(), winston.format.label({ label: 'test' })),
    handleExceptions: true,
    json: true,
    level: 'info',
    maxFiles: 5,
    maxsize: 5 * 1024 * 1024, // 5 MB
  },
  console: {
    colorize: false,
    format: winston.format.combine(winston.format.prettyPrint(), winston.format.simple()),
    handleExceptions: true,
    level: 'debug', // lowest priority - i.e. console.log everything
  },
};
const logger = winston.createLogger({
  transports: [new winston.transports.File(options.file), new winston.transports.Console(options.console)],
  dynamicMeta: function (req, res) {
    return {
      user: req.user ? req.user.username : null,
      role: req.user ? req.user.roles : null,
    };
  },
  exitOnError: false, // do not exit on handled exceptions
});

const loggerMiddleware = expressWinston.logger({
  winstonInstance: logger,
  colorize: false,
});

const error = expressWinston.errorLogger({
  winstonInstance: logger,
});

const errorLoggerMiddleware = expressWinston.errorLogger({
  dumpExceptions: true,
  showStack: true,
  transports: [
    new winston.transports.Console({
      colorize: false,
      handleExceptions: true,
    }),
  ],
  format: winston.format.combine(winston.format.prettyPrint(), winston.format.simple()),
});

module.exports = { logger, loggerMiddleware, error, errorLoggerMiddleware };

express.js

const express = require('express');
const { loggerMiddleware, errorLoggerMiddleware } = require('./winston');

var app = express();

app.use(loggerMiddleware);

// routes

app.use(errorLoggerMiddleware);

Is there an example I can follow?

I don’t see one in the Github repo:

Thanks

I created this small repository to see how to reproduce it:

Looks like a bug to me

Found the issue.
“Distributed tracing” needs to be enabled!

newrelic.js

distributed_tracing: {
    enabled: true
  },
1 Like