elastic / elastic/apm-agent-nodejs

log correlation + webpack may not work

Open
#2,844 7 comments 0 reactions 0 assignees View on GitHub
agent-nodejs
Dominant language
JavaScript
Stars
594
Forks
244
Avg merge
1d 8h
Merged PRs (30d)
16

Description

(I'm splitting out one of the listed issues on https://github.com/elastic/apm-agent-nodejs/issues/1967#issuecomment-919370647 to this new issue.)

Scenario:
- A node.js project using winston, and `@elastic/ecs-winston-format` for ECS logging formatting.
- It is using elastic-apm-node for tracing and should also log correlation with the ecs formatter.
- It is also using *webpack* bundling.

Is there a way to configure and use webpack so that (a) the APM agent works *and* (b) log correlation works?
See coming docs from https://github.com/elastic/apm-agent-nodejs/pull/2837 about using the agent with webpack. Those docs should potentially be updated if there are changes required to get the above to work.

Using 'externals' in webpack.config.js like this:

```js
...
externals: {
'elastic-apm-node': 'commonjs elastic-apm-node'
},
...
```

**naively** works to get instrumentation of core node modules only, e.g. the `http` module. It *may* work for adding other modules as well:

```js
...
externals: {
'elastic-apm-node': 'commonjs elastic-apm-node',
'pg': 'commonjs pg'
},
...
```

However, it does **not** work for log correlation via:

```js
...
externals: {
'elastic-apm-node': 'commonjs elastic-apm-node',
'@elastic/ecs-winston-format': 'commonjs @elastic/ecs-winston-format'
},
...
```

because I found that the built "dist/app.js" replaces the `require('elastic-apm-node')` in ecs-winston-format with the `__webpack_require__`, breaking the sniffing. Solutions:

1. Something about the more complete 'webpack-node-externals' module works (and for all packages). So this is probably our best current workaround answer. However it may defeat the user's use case for webpack, perhaps not.

```js
// webpack.config.js
const nodeExternals = require('webpack-node-externals')
...
externals: [nodeExternals()],
...
```

2. *If* we switch to the global var, this might just work. We should probably do that anyway, to handle the "weird, hard" case.

3. Perhaps, however, it would be better for log correlation here to be from the APM side. It shouldn't just be Elastic APM here. The circular dep issue might be different here then.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.