A production-ready, reusable observability module for Node.js APIs with OpenTelemetry, Prometheus metrics, and distributed tracing.
- β Automatic HTTP Metrics - Request count, duration, active requests
- β Distributed Tracing - Full request traces with Jaeger
- β Zero Configuration - Works out of the box with sensible defaults
- β Completely Reusable - Drop into any Node.js API project
- β Framework Agnostic - Works with Express, Fastify, Koa, Native HTTP
- β Production Ready - Battle-tested patterns and best practices
- β Extensible - Easy to add custom business metrics
npm install express \
@opentelemetry/api \
@opentelemetry/sdk-node \
@opentelemetry/sdk-metrics \
@opentelemetry/exporter-prometheus \
@opentelemetry/exporter-trace-otlp-http \
@opentelemetry/auto-instrumentations-node \
@opentelemetry/resources \
@opentelemetry/semantic-conventionsCopy the obervability/ folder to your project:
your-project/
βββ obervability/
β βββ metrics.js
β βββ tracing.js
β βββ README.md
βββ server.js
const express = require('express');
const { initTracing } = require('./obervability/tracing');
const { initMetrics } = require('./obervability/metrics');
// Initialize observability
initTracing();
const { middleware } = initMetrics({
serviceName: 'your-api',
port: 9464
});
// Create your app
const app = express();
app.use(middleware);
// Your routes
app.get('/api/users', (req, res) => {
res.json({ users: [] });
});
app.listen(3000);That's it! You now have full observability! π
http_requests_total- Total HTTP requests by method, route, statushttp_request_duration_ms- Request duration histogramhttp_active_requests- Currently active requests gauge
- Automatic span creation for all HTTP requests
- Context propagation across services
- Integration with Jaeger for visualization
const { initTracing } = require('./obervability/tracing');
const { initMetrics } = require('./obervability/metrics');
initTracing();
const { middleware } = initMetrics();
app.use(middleware);const { initMetrics, getMeter } = require('./obervability/metrics');
const { middleware } = initMetrics();
app.use(middleware);
// Create custom metrics
const meter = getMeter();
const ordersProcessed = meter.createCounter('orders_processed_total');
app.post('/orders', (req, res) => {
ordersProcessed.add(1, { status: 'success' });
res.json({ orderId: '12345' });
});// Service A
initMetrics({ serviceName: 'user-service', port: 9464 });
// Service B
initMetrics({ serviceName: 'order-service', port: 9465 });
// Service C
initMetrics({ serviceName: 'payment-service', port: 9466 });version: '3.8'
services:
api:
build: .
ports:
- "3000:3000"
- "9464:9464"
environment:
SERVICE_NAME: my-api
prometheus:
image: prom/prometheus
ports:
- "9090:9090"
volumes:
- ./docker/prometheus.yml:/etc/prometheus/prometheus.yml
jaeger:
image: jaegertracing/all-in-one
ports:
- "16686:16686"
- "4318:4318"
grafana:
image: grafana/grafana
ports:
- "3001:3000"# docker/prometheus.yml
global:
scrape_interval: 15s
scrape_configs:
- job_name: 'api'
static_configs:
- targets: ['api:9464']- API: http://localhost:3000
- Metrics Endpoint: http://localhost:9464/metrics
- Prometheus UI: http://localhost:9090
- Jaeger UI: http://localhost:16686
- Grafana: http://localhost:3001
- Quick Start Guide - Get started in 5 minutes
- Usage Examples - 10 real-world examples
- Architecture - System design and data flow
- Module README - Detailed API documentation
Initialize metrics infrastructure and create HTTP instruments.
const { middleware, meter, instruments } = initMetrics({
port: 9464, // Prometheus exporter port
endpoint: '/metrics', // Metrics endpoint path
serviceName: 'my-api' // Service name for metrics
});Get meter instance for creating custom metrics.
const meter = getMeter();
const customCounter = meter.createCounter('my_custom_metric');
customCounter.add(1, { label: 'value' });Get pre-created HTTP metric instruments.
const { httpRequestCounter, httpRequestDuration, activeRequests } = getHttpInstruments();- Initialize metrics before creating Express app
- Apply middleware before route handlers
- Use meaningful service names
- Keep label cardinality low
- Create custom metrics for business logic
- Don't create metrics inside route handlers
- Don't use high-cardinality labels (user IDs, timestamps)
- Don't skip middleware application
- Don't expose sensitive data in labels
# Request rate (requests per second)
rate(http_requests_total[1m])
# Average request duration
rate(http_request_duration_ms_sum[5m]) / rate(http_request_duration_ms_count[5m])
# 95th percentile latency
histogram_quantile(0.95, rate(http_request_duration_ms_bucket[5m]))
# Error rate
sum(rate(http_requests_total{status=~"5.."}[1m])) / sum(rate(http_requests_total[1m]))
# Active requests
http_active_requests
- OpenTelemetry - Observability framework
- Prometheus - Metrics storage and querying
- Jaeger - Distributed tracing backend
- Grafana - Metrics visualization
- Node.js - Runtime environment
- Express - Web framework (example)
{
"@opentelemetry/api": "^1.7.0",
"@opentelemetry/sdk-node": "^0.211.0",
"@opentelemetry/sdk-metrics": "^1.18.0",
"@opentelemetry/exporter-prometheus": "^0.48.0",
"@opentelemetry/exporter-trace-otlp-http": "^0.211.0",
"@opentelemetry/auto-instrumentations-node": "^0.69.0",
"@opentelemetry/resources": "^1.18.0",
"@opentelemetry/semantic-conventions": "^1.18.0"
}- Check middleware is applied:
app.use(middleware) - Generate traffic to your API
- Verify endpoint:
curl http://localhost:9464/metrics
Change the port in configuration:
initMetrics({ port: 9465 })Check Prometheus scrape configuration targets your service:
- targets: ['api:9464'] # or 'localhost:9464'Contributions are welcome! Please feel free to submit a Pull Request.
MIT License - feel free to use this in your projects!
- OpenTelemetry - Observability framework
- Prometheus - Metrics and monitoring
- Jaeger - Distributed tracing
- Grafana - Visualization platform
For questions and support, please open an issue on GitHub.
Made with β€οΈ for the Node.js community
Drop this module into any API and get instant observability! π