Next.js Instrumentation
Next.js provides an instrumentation.ts file that runs once when the server starts — before any request is handled. This is the ideal place to initialize observability.
registerVestig() turns that single file into a complete instrumentation setup: OTLP export, automatic fetch instrumentation, console capture, and database configuration — all in one call.
Quick Start
1. Create instrumentation.ts
Add the file to your project root (next to next.config.ts):
2. Set environment variables
3. Start your app
OTLP export and fetch instrumentation are now active. Every outgoing fetch() call is traced automatically, and spans are exported to your configured endpoint.
RegisterVestigOptions
Top-level options passed to registerVestig():
| Option | Type | Default | Description |
|---|---|---|---|
serviceName |
string |
Required | Service name used in OTLP resource attributes |
otlp |
OTLPConfig |
— | OTLP export configuration (endpoint, headers, batching) |
autoInstrument |
AutoInstrumentConfig |
— | Auto-instrumentation options for fetch, console, and database |
tailSampling |
TailSamplingConfig |
— | Tail sampling configuration for wide events |
debug |
boolean |
false |
Enable debug logging for initialization diagnostics |
OTLPConfig
Controls how spans are exported to your observability backend:
| Option | Type | Default | Description |
|---|---|---|---|
endpoint |
string |
OTEL_EXPORTER_OTLP_ENDPOINT env var |
OTLP endpoint URL for traces |
headers |
Record<string, string> |
OTEL_EXPORTER_OTLP_HEADERS env var |
Custom authentication headers |
serviceVersion |
string |
— | Service version string (e.g. '1.0.0') |
environment |
string |
VERCEL_ENV or NODE_ENV |
Deployment environment name |
resourceAttributes |
Record<string, unknown> |
— | Additional OTLP resource attributes |
batchSize |
number |
100 |
Number of spans per export batch |
flushInterval |
number |
5000 |
Flush interval in milliseconds |
AutoInstrumentConfig
Controls what gets automatically instrumented at startup:
| Option | Type | Default | Description |
|---|---|---|---|
fetch |
boolean \| InstrumentFetchOptions |
true |
Auto-instrument all fetch() calls with tracing spans |
console |
boolean |
false |
Capture console.error calls as spans |
database |
DatabaseInstrumentConfig |
— | Database instrumentation config (used by instrumentPostgres()) |
RegisterVestigResult
registerVestig() returns an object describing what was enabled:
Use the result to verify initialization or trigger a graceful shutdown.
Full Configuration Example
A complete setup with all available options:
Environment Variables
registerVestig() auto-reads several environment variables when the corresponding option is not explicitly set in config:
OTEL_EXPORTER_OTLP_ENDPOINT-- OTLP traces endpoint. If missing and nootlp.endpointis provided, OTLP export is skipped.OTEL_EXPORTER_OTLP_HEADERS-- Comma-separatedkey=valuepairs for authentication headers. Merged with anyotlp.headersyou set in code (code takes precedence).VERCEL_ENV-- Deployment environment on Vercel (production,preview, ordevelopment). Used as the default forotlp.environment.NODE_ENV-- Fallback environment whenVERCEL_ENVis not set.
Header format
The OTEL_EXPORTER_OTLP_HEADERS variable uses comma-separated key-value pairs:
Values containing = are handled correctly -- only the first = is treated as the separator.
Vercel Deployment
Vercel has built-in support for OpenTelemetry. When you enable Observability in your Vercel project settings, Vercel provides an OTLP endpoint automatically.
To connect Vestig:
- Enable Observability in your Vercel project dashboard
- Set
OTEL_EXPORTER_OTLP_ENDPOINTin your project environment variables VERCEL_ENVis set automatically by Vercel toproduction,preview, ordevelopment- The endpoint auto-appends
/v1/tracesif missing -- you can provide the base URL or the full path
No additional configuration is required. registerVestig() reads OTEL_EXPORTER_OTLP_ENDPOINT and VERCEL_ENV from the environment automatically.
Debug Mode
Enable debug mode during development to see exactly what was initialized:
When enabled, registerVestig() logs initialization status to the console:
This helps verify that your endpoint is correctly resolved and all expected instrumentations are active.
Graceful Shutdown
The shutdown() function flushes any pending spans and cleans up resources. Use it to ensure no data is lost when your process exits:
shutdown() performs three things in order:
- Restores
console.errorif console capture was enabled - Clears the global database configuration
- Flushes all pending spans via the span processor pipeline
Next Steps
- OTLP Export — Configure backends like Jaeger, Honeycomb, Grafana Cloud
- Database Logging — Prisma, Drizzle, and PostgreSQL instrumentation
- Cloudflare & Edge — Edge runtime deployment guide
- Next.js Integration — Full overview of
@vestig/next