question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

currentTransaction.startSpan return null when I use express.

See original GitHub issue

Describe the bug

const currentTransaction = apm.currentTransaction;
const span = currentTransaction.startSpan(name);

I use the code above in express context, but the span is null.

Environment (please complete the following information)

  • OS: [e.g. Linux] win10
  • Node.js version: 12.18.1
  • APM Server version:3.6.1
  • Agent version: 6.3.2

How are you starting the agent? (please tick one of the boxes)

  • [√] Calling agent.start() directly (e.g. require('elastic-apm-node').start(...))
  • Requiring elastic-apm-node/start from within the source code
  • Starting node with -r elastic-apm-node/start

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ancms2600commented, Aug 30, 2021

Thank you @astorm. These details are good to know. Perhaps they can be added to the API docs? https://www.elastic.co/guide/en/apm/agent/nodejs/master/transaction-api.html#transaction-start-span

0reactions
astormcommented, Aug 30, 2021

@ancms2600 At the time of this writing there’s a handful of reasons that the agent might not be able to start a span (and, as a result, return null).

First – if there’s no current transaction when you call apm.startSpan then the agent will generate no spans. This check occurs here.

Second – if you’re sampling spans (say, via the ELASTIC_APM_TRANSACTION_SAMPLE_RATE configuration) and a transaction ends up being sampled then the agent will generate no spans. You can see this check here.

Third – if the current transaction has ended no further spans will be generated until a new transaction starts. You can see this check here.

Fourth – If you’re using the ELASTIC_APM_TRANSACTION_MAX_SPANS configuration and you’ve hit the maximum number of spans for a transaction, the agent will generate no further spans. You can see this check here.

It’s hard to say why this is happening in your specific case, but if it’s happening randomly, then my guess would be your sampling rate. You should always presume that startSpan may not return a span object. (in other words, your guard clause is appropriate)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Agent API | APM Node.js Agent Reference [master] - Elastic
Starts the Elastic APM agent for Node.js and returns itself. For the APM agent to ... If null is given, then no span...
Read more >
elastic-apm-node not register span after express responde
First express response, after 1 second, i try use startspan, but i got error: Cannot read property 'end' of null
Read more >
new "run context" management to fix numerous span ... - Demo
currentTransaction = null + agent._instrumentation. ... I've use the term "run context" for what we've been tending to call "context" or "async context"....
Read more >
sentry/tracing - UNPKG
n *\n * @param level string representation of Severity\n * @returns Severity\n ... */\n setName(name: string): void;\n\n /** Returns the current transaction ......
Read more >
elastic-apm-node @ 2.10.0 .. 2.11.0 - Package Diff
return this.currentTransaction.startSpan.apply(this.currentTransaction, arguments). } +var wrapped = Symbol('elastic-apm-wrapped-function').
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found