Skip to content

http-formatters remoteAddress/remotePort doesn't function as expected #18

@SpencerLN

Description

@SpencerLN

formatHttpRequest does not appear to reliably determine the client IP address/port. The fields that are being used, remoteAddress/remotePort, do not appear to be in the API docs https://nodejs.org/docs/latest-v14.x/api/http.html.

This results in most fields in the logs generated being undefined:
image

It appears more likely that req.socket.remoteAddress and req.socket.remotePort should be being used here. Additionally, it would be useful to have a way to determine the real IP address and use that when NodeJS is behind a proxy.

Perhaps something along these lines to try to determine the best IP address to use:

  ecs.client = ecs.client || {}
  // if req.ip exists from framework (Express, etc.) defer to that http://expressjs.com/en/4x/api.html
  if (req.ip) {
    ecs.client.address = req.ip;
  } else if (req.headers['x-forwarded-for']) {
    ecs.client.address = req.headers['x-forwarded-for'].split(",")[0];
  } else if (req.socket && req.socket.remoteAddress) {
    ecs.client.address = req.socket.remoteAddress;
  }

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions