elastic/require-in-the-middle

when using webpack & babel & import , this package can not get http&https modules in some cases

Closed this issue · 4 comments

In my React server-side rendering project, I use webpack & babel & import, but in some cases , this package can not get right modules, for example "http"&"https".

I console.log filename in Module.prototype.require, but cannot find "http" module. so elastic-apm-node cannot capture any transaction.

I have to modify elastic-apm-node , require "http" manually to make node apm agent work.
just like this : require('./modules/http')(require('http'), this._agent, version, true)

If I understand you correctly, you expect a transaction to automatically be created every time an incoming HTTP request to your Node.js server is being served and that this doesn't happen because it doesn't correctly register the hooks in the http module?

If so, would it be possible for you so share a sample app that we can use to reproduce the problem?

@watson Thanks for your reply.
I think I find out the problem. Because I use webpack to babel app.js from es6 to es5, The require order has changed, so "elastic-apm-node" dose not required in the first order.

I put the apm init script in the first place of webpack entry arrays, everything goes will.

module.exports = {
  name: 'server',
  target: 'node',
  mode: process.env.NODE_ENV,
  entry: {
    app: ['./src/apm-agent.js', './src/app.js'],
  },

Great to hear 👍

If you're using import instead of require we also have a special section in our docs for how to deal with that: https://www.elastic.co/guide/en/apm/agent/nodejs/2.x/es-modules.html