A comprehensive, production-ready OpenTelemetry implementation for Next.js with an interactive learning dashboard.
This project teaches you the three pillars of observability:
- 📍 Traces - Follow requests through your system
- 📊 Metrics - Numerical measurements over time
- 📝 Logs - Structured event records with trace correlation
bun installStart Docker Desktop, then run:
docker-compose up -dThis starts:
- Jaeger - Trace visualization at http://localhost:16686
- Prometheus - Trace Metrics from the App on port 9090
- Loki - Visualize the App logs using pino on port 3100 but available in UI in grafana -> explore -> Loki
- Grafana - Visualize the different data from the above port 3001
- OTLP Collector - Receives telemetry on port 4318
bun devOpen http://localhost:3000 to see the interactive dashboard.
Open src/lib/telemetry/logger and change pino level config from silent to info
src/
├── instrumentation.ts # OpenTelemetry SDK initialization
├── lib/telemetry/
│ ├── config.ts # Configuration & constants
│ ├── tracer.ts # Tracing utilities
│ ├── metrics.ts # Metrics utilities
│ ├── logger.ts # Structured logging
│ ├── middleware.ts # API route middleware
│ ├── hooks.ts # React hooks for client telemetry
│ ├── init.ts # SDK setup
│ └── index.ts # Public API exports
├── app/
│ ├── page.tsx # Interactive dashboard
│ └── api/
│ ├── users/route.ts # Demo: Users API with tracing
│ ├── orders/route.ts # Demo: Complex traces
│ └── telemetry/route.ts # Client event ingestion
Start with src/app/api/users/route.ts to see:
- How to wrap API routes with
withTelemetry() - Creating custom spans with
traceAsync() - Adding attributes and events to spans
Check src/app/api/orders/route.ts for:
- Multi-step operations with child spans
- External API calls (simulated payment)
- Error handling in traces
Explore src/lib/telemetry/metrics.ts:
- Counters, histograms, and gauges
- HTTP request metrics
- Business metrics (orders, revenue)
See src/lib/telemetry/logger.ts:
- JSON structured logs
- Trace correlation (trace_id in every log)
- Log levels and best practices
Check src/lib/telemetry/hooks.ts:
- Page view tracking
- Core Web Vitals
- User interaction tracking
All telemetry configuration is in src/lib/telemetry/config.ts:
export const TELEMETRY_CONFIG = {
serviceName: "nextjs-telem-app",
serviceVersion: "1.0.0",
environment: process.env.NODE_ENV,
otlp: {
tracesEndpoint: "http://localhost:4318/v1/traces",
metricsEndpoint: "http://localhost:4318/v1/metrics",
logsEndpoint: "http://localhost:4318/v1/logs",
},
sampling: {
ratio: process.env.NODE_ENV === "production" ? 0.1 : 1.0,
},
};- Make some API calls in the dashboard
- Open http://localhost:16686
- Select service:
nextjs-telem-app - Click "Find Traces"
In development, traces and logs are also printed to the console for easy debugging.
For production, update these settings:
- Sampling - Reduce to 1-10% for high-traffic apps
- Endpoints - Point to your observability backend (Grafana Cloud, Honeycomb, Datadog, etc.)
- Authentication - Add API keys in exporter headers
- Console Export - Disable for performance
Example production config:
const TELEMETRY_CONFIG = {
sampling: { ratio: 0.1 },
otlp: {
tracesEndpoint: "https://otel.your-provider.com/v1/traces",
},
features: {
consoleExporter: false,
otlpExporter: true,
},
};- Next.js 16 - React framework
- OpenTelemetry - Observability framework
- shadcn/ui - UI components
- Tailwind CSS - Styling
- Pino - Fast JSON logger
- Jaeger - Trace visualization
MIT - Feel free to use this as a template for your projects!
Made with ❤️ for learning OpenTelemetry