Next.js Serverless Functions
HyperDX can ingest native OpenTelemetry traces from your Next.js serverless functions (opens in a new tab) in Next 13.2+.
This Guide Integrates: Console Logs · Traces
If you're looking for session replay/browser-side monitoring, you'll want to install the Browser integration instead.
If you're looking to collect logs from a Vercel-deployed app, you can add the Vercel logging integration.
Installing
Enable Instrumentation Hook
To get started, you'll need to enable the Next.js instrumentation hook by
setting experimental.instrumentationHook = true;
in your next.config.js
.
Example:
const nextConfig = {
experimental: {
instrumentationHook: true,
},
};
module.exports = nextConfig;
Install HyperDX OpenTelemetry SDK
npm install @hyperdx/node-opentelemetry
Create Instrumentation Files
Create a file called instrumentation.js
in your Next.js project root with the
following contents:
// instrumentation.js
export async function register() {
if (process.env.NEXT_RUNTIME === 'nodejs') {
const { initSDK } = await import('@hyperdx/node-opentelemetry');
initSDK({
consoleCapture: true, // optional, default: true
additionalInstrumentations: [], // optional, default: []
});
}
}
This will allow Next.js to import the OpenTelemetry instrumentation for any serverless function invocation.
Configure Environment Variables
If you're sending traces directly to HyperDX, you'll need to start your Next.js server with the following environment variables to point spans towards HyperDX:
HYPERDX_API_KEY=<YOUR_HYPERDX_INGESTION_API_KEY> \
OTEL_SERVICE_NAME=<MY_SERVICE_NAME> \
OTEL_EXPORTER_OTLP_ENDPOINT=https://in-otel.hyperdx.io
npm run dev
If you're deploying in Vercel, ensure that all the environment variables above are configured for your deployment.
If you have further issues, please reach out to us at support@hyperdx.io.