Star us on GitHub
Star
Menu

Node.js Quick Start

Learn how to set up highlight.io in Node.js.

1

Configure client-side Highlight. (optional)

If you're using Highlight on the frontend for your application, make sure you've initialized it correctly and followed the fullstack mapping 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

Optionally, report manual errors in your app.

If you need to report exceptions outside of a handler, use the Highlight SDK.

const parsed = H.parseHeaders(request.headers) H.consumeError(error, parsed?.secureSessionId, parsed?.requestId)
Copy
5

Verify that your SDK is reporting errors.

You'll want to throw an exception in one of your Node.js handlers. Access the API handler and make sure the error shows up in Highlight.

const onError = (request, error) => { const parsed = H.parseHeaders(request.headers) H.consumeError(error, parsed.secureSessionId, parsed.requestId) } const main = () => { try { throw new Error('example error!') } catch (e) { onError(e) } }
Copy
6

Call built-in console methods.

Logs are automatically recorded by the highlight SDK. Arguments passed as a dictionary as the second parameter will be interpreted as structured key-value pairs that logs can be easily searched by.

module.exports = function() { console.log('hey there!'); console.warn('whoa there', {'key': 'value'}); }
Copy
7

Verify your backend logs are being recorded.

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

8

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
9

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
10

Verify your backend traces are being recorded.

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