Star us on GitHub
Star
Menu

Manual Node.js Tracing

Learn how to set up highlight.io tracing for your Node.js application.
1
Set up your frontend highlight.io integration.

First, make sure you've followed the frontend getting started guide.

2
Install the relevant Highlight SDK(s).

Install @highlight-run/node with your package manager.

npm install --save @highlight-run/node
Copy
3
Initialize the Highlight JS SDK.

Initialize the Highlight JS SDK with your project ID.

import { H } from '@highlight-run/node' H.init({ projectID: '<YOUR_PROJECT_ID>', serviceName: '<YOUR_SERVICE_NAME>', environment: 'production', })
Copy
4
Wrap your code using the Node.js SDK.

By wrapping your code with startSpan and endSpan, the @highlight-run/node SDK will record a span. You can create more child spans or add custom attributes to each span.

const functionToTrace = async (input int) => { const span = await H.startActiveSpan("functionToTrace", {custom_property: input}) // ... anotherFunction() // ... span.end() } const anotherFunction = () => { const span = H.startActiveSpan("anotherFunction", {}) // ... span.end() } module.exports = function() { console.log('hey there!'); functionToTrace() }
Copy
5
Pass HTTP headers to the SDK

H.runWithHeaders takes request headers and spreads them across all related spans, automatically relating spans to your session and request headers.

app.get('/', async (req, res) => { await H.runWithHeaders(req.headers, () => { const span = H.startActiveSpan("custom-span", {}) const err = new Error('this is a test error') console.info('Sending error to highlight') H.consumeError(err) res.send('Hello World!') span.end() }) })
Copy
6
Verify your backend traces are being recorded.

Visit the highlight traces portal and check that backend traces are coming in.