Skip to content

apm.startTransaction crash if agent not yet started #2425

@trentm

Description

@trentm
const apm = require('elastic-apm-node');

const transaction = apm.startTransaction('test', 'any');

setTimeout(() => {
  transaction.end();
}, 1000);

After the changes in #2410 to not configure (and set agent._conf) until agent.start(...) is called, the above snippet crashes:

.../kibana/node_modules/traceparent/index.js:127
      : start(Math.random() <= conf.transactionSampleRate)
                                    ^

TypeError: Cannot read properties of null (reading 'transactionSampleRate')
    at Function.startOrResume (.../kibana/node_modules/traceparent/index.js:127:37)
    at Function.startOrResume (.../kibana/node_modules/elastic-apm-node/lib/tracecontext/index.js:15:37)
    at Transaction.GenericSpan work/.../kibana/node_modules/elastic-apm-node/lib/instrumentation/generic-span.js:18:32)
    at new Transaction (.../kibana/node_modules/elastic-apm-node/lib/instrumentation/transaction.js:20:15)
    at Instrumentation.startTransaction (.../kibana/node_modules/elastic-apm-node/lib/instrumentation/index.js:371:17)
    at Agent.startTransaction (.../kibana/node_modules/elastic-apm-node/lib/agent.js:145:49)

Before #2410 the apm.startTransaction was returning a transaction based on incomplete configuration. I think the right fix here is that apm.startTransaction() should return null in this case (perhaps with a log.warning that a transaction cannot be created before the agent is started). Thankfully the interface (in index.d.ts) is already that apm.startTransaction() can return null.

Part of this fix should update the changelog entry for #2410 to notify that here is a case (the first one) where apm.startTransaction() might actually return null. That could affect user code. /cc @mshustov

Metadata

Metadata

Assignees

Labels

agent-nodejsMake available for APM Agents project planning.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions