Next.js
Next.js is now shipping with an experimental OpenTelemetry integration. This guide will help you instrument your Next.js application(s) with OpenTelemetry and send traces to Checkly.
Step 1: Install the @vercel/otel
package
For Next.js, Vercel has created a wrapper that should get you going very quickly. We’re just adding some extra packages so we can filter out the Checkly Traces.
Make sure to install v1.9.1 or later of the @vercel/otel
package as it contains a critical bug fix for Vercel’s Edge runtime.
Step 2: Initialize the instrumentation
Set the instrumentationHook
flag to true
in your Next.js configuration file. This will enable the OpenTelemetry instrumentation.
Create an instrumentation.ts
file in the root of your project, or, place in the src
directory if you are using one.
And add the following code:
Notice that we are using the default exporter and span processor from the @vercel/otel
package, which is configured by using the auto
value.
This package is compatible with Vercel’s Edge runtime, in contrast to the @opentelemetry/exporter-trace-otlp-http
package, which is not.
If you are not using Vercel or don’t use Vercel’s Edge runtime, you can also use the @opentelemetry/exporter-trace-otlp-http
and just follow the regular Node.js instrumentation guide.
This is however not required, the above configuration should work just fine on Node.js and Edge runtimes.
Also notice the sampler
configuration. This is a custom, head-based sampler that will only sample spans that are generated by Checkly by
inspecting the trace state. This way you only pay for the egress traffic generated by Checkly and not for any other traffic.
Step 3: Start your app with the instrumentation
Toggle on Import Traces and grab your OTel API key in the OTel API keys section of the Traces page in the Checkly app and take a note of the endpoint for the region you want to use.
Now, export your API key in your shell by setting the OTEL_EXPORTER_OTLP_HEADERS
environment variable.
Next, export the endpoint for the region you want to use and give your service a name.
During the beta we only have one region available: eu-west-1
. We will expand to US regions soon.
We are using the standard OpenTelemetry environment variables here to configure the OTLP exporter.
Variable | Description |
---|---|
OTEL_EXPORTER_OTLP_HEADERS |
The Authorization HTTP header containing your Checkly OTel API key. |
OTEL_EXPORTER_OTLP_ENDPOINT |
The Checkly OTel API endpoint for the region you want to use. |
OTEL_SERVICE_NAME |
The name of your service to identify it among the spans in the web UI. |
If you are using Vercel for hosting your Next.js app, add the above environment variables to your Vercel project settings, e.g. 👇
Further reading
Have a look at the official Next.js docs on how to enable the experimental OpenTelemetry integration.
Last updated on December 19, 2024. You can contribute to this documentation by editing this page on Github