ecs-logging-nodejs icon indicating copy to clipboard operation
ecs-logging-nodejs copied to clipboard

http.request.id not working

Open diegoneemu opened this issue 3 years ago • 0 comments

Hello everybody

I'm having trouble getting the http.request.id to log in correctly. Looking at the lib @elastic/ecs-winston-format and @elastic/ecs-helpers , I couldn't find any parse of the X-Request-ID or X-Correlation-ID headers as it says in the documentation and in our observability stack we couldn't get our logs to index the field in question. Is this already on the radar or planned for some version? If not, how can I contribute so that it can enter a future version?

I noticed that there are two issues already open on the subject, http.request.id field from ECS 1.9 and event.id -> http.request.id in formatHttpRequest, but I couldn't understand if the implementation was carried out.

Sorry for the bad English.

Imported packages from npm:

"@elastic/ecs-winston-format": "^1.3.1" "winston": "3.2.1"

Header example:

headers: {
  'x-request-id': '00162b9b-3083-40b4-8b87-42636efcc651#19974',
  'content-type': 'application/json',
  'user-agent': 'PostmanRuntime/7.29.2',
  accept: '*/*',
  'postman-token': '6d3538e6-063d-4a7d-bd9a-604ed555a920',
  host: 'localhost:3001',
  'accept-encoding': 'gzip, deflate, br',
  connection: 'keep-alive',
  'content-length': '7656'
}

Create logger using winston and @elastic/ecs-winston-format

import { createLogger, Logger, transports } from 'winston'
import ecsFormat from '@elastic/ecs-winston-format'

const logger = createLogger({
  format: ecsFormat({ convertReqRes: true }),
  transports: [new transports.Console()],
  silent: process.env.NODE_ENV === 'test',
})

export default logger

Inject logger in a express request middleware for using in other middlewares and services

import logger from './logger'

...

// implementing a express app

...

app.use((req, res, next) => {
  logger.child({ req })
  req.logger = logger;
  next();
})

// use logger in a route
app.delete('/path', (req, res) => {
  const { logger } = req;

  logger.info({"Send request to api"});
})

Resulting log:

{
  "@timestamp": "2022-09-30T13:58:00.115Z",
  "log.level": "info",
  "message": "Send request to api",
  "ecs": {
    "version": "1.6.0"
  },
  "http": {
    "version": "1.1",
    "request": {
      "method": "DELETE",
      "headers": {
        "x-request-id": "00162b9b-3083-40b4-8b87-42636efcc651#19974",
        "content-type": "application/json",
        "user-agent": "PostmanRuntime/7.29.2",
        "accept": "*/*",
        "postman-token": "95bfc606-74af-43eb-9f60-05c02ec0b3ed",
        "host": "localhost:3001",
        "accept-encoding": "gzip, deflate, br",
        "connection": "keep-alive",
        "content-length": "7656"
      },
      "body": {
        "bytes": 7656
      }
    }
  },
  "url": {
    "path": "/path",
    "query": "foo=bar&fooz=barz",
    "full": "http://localhost:3001/path?foo=bar&fooz=barz"
  },
  "client": {
    "address": "::1",
    "ip": "::1",
    "port": 52527
  },
  "user_agent": {
    "original": "PostmanRuntime/7.29.2"
  }
}

diegoneemu avatar Sep 30 '22 14:09 diegoneemu