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 calling H.startWithHeaders() and span.end(), 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, ctx } = H.startWithHeaders("functionToTrace", {}, {custom_property: input}) // ... // use the current span context with the function call to ensure child spans are tied to the current span // import api from '@opentelemetry/api' api.context.with(ctx, () => { anotherFunction() }) // ... span.end() } const anotherFunction = () => { const { span } = H.startWithHeaders("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.startWithHeaders("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.