- APM Node.js Agent: other versions:
- Introduction
- Set up the Agent
- Monitoring AWS Lambda Node.js Functions
- Monitoring Node.js Azure Functions
- Get started with Express
- Get started with Fastify
- Get started with hapi
- Get started with Koa
- Get started with Next.js
- Get started with Restify
- Get started with TypeScript
- Get started with a custom Node.js stack
- Starting the agent
- Supported technologies
- Configuration
- API Reference
- Metrics
- Logs
- OpenTelemetry bridge
- OpenTracing bridge
- Source map support
- ECMAScript module support
- Distributed tracing
- Message queues
- Performance Tuning
- Troubleshooting
- Upgrading
- Release notes
Custom spans
editCustom spans
editThis is an example of how to use custom spans. For general information about the Elastic APM Node.js Span API, see the Span API documentation.
If you want to track and time a custom event that happens in your application during a transaction, you can add a new span to an existing transaction.
In the example below, we create an Express app that times how long it takes to:
- Receive the body of an HTTP POST or PUT request
- Parse JSON sent by the client
var apm = require('elastic-apm-node').start() var app = require('express')() // body reader middleware app.use(function (req, res, next) { if (req.method !== 'POST' && req.method !== 'PUT') { return next() } // `startSpan` will only return a span if there's an // active transaction var span = apm.startSpan('receiving body') var buffers = [] req.on('data', function (chunk) { buffers.push(chunk) }) req.on('end', function () { req.body = Buffer.concat(buffers).toString() // end the span after we're done loading data from the // client if (span) span.end() next() }) }) // JSON parser middleware app.use(function (req, res, next) { if (req.headers['content-type'] !== 'application/json') { return next() } // start a span to measure the time it takes to parse // the JSON var span = apm.startSpan('parse json') try { req.json = JSON.parse(req.body) } catch (e) {} // when we've processed the json, stop the custom span if (span) span.end() next() }) // ...your route handler goes here... app.listen(3000)
ElasticON events are back!
Learn about the Elastic Search AI Platform from the experts at our live events.
Register nowWas this helpful?
Thank you for your feedback.