Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"elasticApm.isStarted is not a function" crash while using @elastic/ecs-pino-format from elastic-apm-node #60

Closed
trentm opened this issue Mar 16, 2021 · 0 comments · Fixed by #61
Assignees
Labels
agent-nodejs Make available for APM Agents project planning.

Comments

@trentm
Copy link
Member

trentm commented Mar 16, 2021

During the process of moving the Elastic APM agent over to using pino and this @elastic/ecs-pino-format package, we hit this crash:

/app/node_modules/@elastic/ecs-pino-format/index.js:55
  const apm = elasticApm && elasticApm.isStarted() ? elasticApm : null
                                       ^

TypeError: elasticApm.isStarted is not a function
    at createEcsPinoOptions (/app/node_modules/@elastic/ecs-pino-format/index.js:55:40)
    at Object.createLogger (/app/node_modules/elastic-apm-node/lib/logging.js:143:8)
    at new Config (/app/node_modules/elastic-apm-node/lib/config.js:243:27)
    at config (/app/node_modules/elastic-apm-node/lib/config.js:213:10)
    at Agent._config (/app/node_modules/elastic-apm-node/lib/agent.js:133:16)
    at new Agent (/app/node_modules/elastic-apm-node/lib/agent.js:39:8)
    at Object.<anonymous> (/app/node_modules/elastic-apm-node/index.js:5:18)
    at Module._compile (internal/modules/cjs/loader.js:1138:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1158:10)
    at Module.load (internal/modules/cjs/loader.js:986:32)

The reason is that the guarded import of the apm agent:

try {
  elasticApm = require('elastic-apm-node')
} catch (ex) {
  // Silently ignore.
}

results in elasticApm === {}, because this check is done before "elastic-apm-node" is loaded: require.cache includes:

  '/app/node_modules/elastic-apm-node/lib/agent.js': Module {
    id: '/app/node_modules/elastic-apm-node/lib/agent.js',
    path: '/app/node_modules/elastic-apm-node/lib',
    exports: {},
    parent: Module {
      id: '/app/node_modules/elastic-apm-node/index.js',
      path: '/app/node_modules/elastic-apm-node',
      exports: {},
      parent: [Module],
      filename: '/app/node_modules/elastic-apm-node/index.js',
      loaded: false,
      children: [Array],
      paths: [Array]
    },
    filename: '/app/node_modules/elastic-apm-node/lib/agent.js',
    loaded: false,
    children: [ [Module], [Module], [Module], [Module], [Module] ],
    paths: [
      '/app/node_modules/elastic-apm-node/lib/node_modules',
      '/app/node_modules/elastic-apm-node/node_modules',
      '/app/node_modules',
      '/node_modules'
    ]
  },

Note loaded: false and hence exports: {}.

This should be a situation that can only be hit if the elastic APM agent itself (or one the deps that it loads) uses ecs-pino-format. I'd like to fix this with two changes:

  1. improve that guard to handle the loaded: false case
  2. provide a config option to explicitly disable the APM integration with ecs-pino-format usage. I'll do this part in a separate PR. -> feat: add 'apmIntegration: false' option to disable Elastic APM integration #62
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
agent-nodejs Make available for APM Agents project planning.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant