Quickstart for using the Vercel OpenTelemetry Collector
Learn how to get started with OTEL on Vercel to send traces from your functions to application performance monitoring (APM) vendors.Vercel's OpenTelemetry collector is available on all plans
Vercel has an OpenTelemetry (OTEL) collector that allows you to send OTEL traces from your Serverless or Edge Functions to application performance monitoring (APM) vendors such as New Relic.
You can use the Vercel OTEL collector or configure the OTEL SDK to use custom trace exporters.
Traces are a way to collect data about the performance and behavior of your application and help identify the cause of performance issues, errors, and other problems. Learn more about traces in the OpenTelemetry docs.
To start using OpenTelemetry on Vercel, follow the steps below. This guide gives examples using Next.js and other frameworks where you can use Serverless or Edge Functions.
- DataDog: Only traces are supported in Beta to teams on Pro and Enterprise plans.
- New Relic: Traces, metrics and logs are supported in Beta to teams on Pro and Enterprise plans.
Select an integration from the Observability category in the Marketplace such as DataDog or New Relic.
Click the Add Integration button to begin the installation and follow each step to add the correct Scope.
If you already have installed an OTEL integration, you can skip this step.
To use OTEL, you must enable Traces for the Integration. You can do this either during initial setup, or if you already have the integration installed, select Manage next to the Integration in the Integrations tab and then select Configure.
For JavaScript and Typescript users on Vercel, you can use the
@vercel/otel
package to call the correct OpenTelemetry SDK for you.Install the OpenTelemetry JavaScript SDK and
@vercel/otel
:pnpm i @opentelemetry/api @vercel/otel
Create an
instrumentation.ts
file in the root of your project, or, on Next.js it must be placed in thesrc
directory if you are using one. Add the following code to initialize and configure OTEL using@vercel/otel
.instrumentation.tsimport { registerOTel } from '@vercel/otel'; export function register() { registerOTel({ serviceName: 'your-project-name' }); } // NOTE: You can replace `your-project-name` with the actual name of your project
Before receiving traces from an APM vendor (such as DataDog), it may be necessary to first create the service you intend to use as a
serviceName
within the service catalog. Refer to the APM vendor's documentation for specific requirements.You can use standard OpenTelemetry SDK to send traces to our collector. List of all supported languages by OpenTelemetry and their SDK can be found in OpenTelemetry docs.
Begin by installing the following packages:
pnpm i @opentelemetry/api @opentelemetry/sdk-node @opentelemetry/resources @opentelemetry/semantic-conventions @opentelemetry/sdk-trace-base @opentelemetry/exporter-trace-otlp-http @opentelemetry/sdk-trace-node
Next, create an
instrumentation.ts
file in the root of your project and add the following code to initialize and configure OTEL.instrumentation.tsimport { NodeSDK } from '@opentelemetry/sdk-node'; import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http'; import { Resource } from '@opentelemetry/resources'; import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions'; import { SimpleSpanProcessor } from '@opentelemetry/sdk-trace-node'; export function register() { const sdk = new NodeSDK({ resource: new Resource({ [SemanticResourceAttributes.SERVICE_NAME]: 'your-project-name', // NOTE: You can replace `your-project-name` with the actual name of your project }), spanProcessor: new SimpleSpanProcessor(new OTLPTraceExporter()), }); sdk.start(); }
The provided service name will be used in your OpenTelemetry backend to distinguish traces from different services. You need to specify what you want to do with generated spans, there are two types of span processors:
SimpleSpanProcessor
: sends each span to the collector right away. This type is shown in code aboveBatchSpanProcessor
: collects a number of spans and sends them together to the collector. If you choose this type, you'll need to callprocessor.forceFlush()
at the end of the invocation to immediately export all spans
A span processor needs to know what to do with processed traces. In order to export traces to Vercel OpenTelemetry collector, you need to use one of these exporters:
@opentelemetry/exporter-trace-otlp-http
@opentelemetry/exporter-trace-otlp-grpc
Next.js 13.4+ supports auto-instrumentation which means you no longer have to create a span for each request. To use this feature in Next.js 13 & 14, you must explicitly opt-in by adding
experimental.instrumentationHook = true
to yournext.config.js
. This is not required in Next.js 15+.For more information, please refer to the Next.js docs for auto-instrumentation.
For non-Next.js frameworks or Next.js version older than
13.4
, you will need to manually create spans for each request.pages/api/get-user.jsimport { trace, context } from '@opentelemetry/api'; export default async function getUser(_request, response) { const tracer = trace.getTracer('your-project-name'); tracer.startActiveSpan(name, async (span) => { try { const result = someFnThatMightThrowError(span); span.end(); return result; } catch (e) { span.recordException(e); span.setStatus({ code: SpanStatusCode.ERROR, message: e.message }); throw e; } }); }
In the code above we are importing the OTEL tracer to the
pages/api/get-user.ts
file to create spans for tracking performance. You'll need to include this import to any file that uses the tracer.If you have an existing project, you must trigger a redeployment to use traces.
You can either deploy your project through the dashboard or through the CLI.
The use of OTEL collector is recommended due to its performance benefits. However, if you need to export tracing to an unsupported APM vendor, you can do so using environment variables or configuration options in the @vercel/otel
package.
Most of OpenTelemetry's SDK environment variables are supported by @vercel/otel
, including OTLP exporter variables. You can configure these variables using Vercel's environment variables.
And you can supply completely custom exporter using the traceExporter
or spanProcessors
configuration options in the registerOTel()
API.
import { registerOTel, OTLPHttpJsonTraceExporter } from '@vercel/otel';
export function register() {
registerOTel({
serviceName: 'your-project-name',
traceExporter: new OTLPHttpJsonTraceExporter({
url: 'https://your-trace-exporter-url',
headers: {
'authentication-header-name': 'authentication-header-value',
'another-header-name': 'another-header-value',
},
}),
});
}
If you are using Sentry v8+, follow the Sentry documentation to learn how to use your existing custom OpenTelemetry setup.
Was this helpful?