OpenTelemetry provides a vendor-neutral API for traces, metrics, and logs. Instrument once, export to any backend — Jaeger, Prometheus, Datadog, Grafana, or your own.
Auto-Instrumentation (Zero Code Changes)
npm install @opentelemetry/sdk-node @opentelemetry/auto-instrumentations-node
const { NodeSDK } = require('@opentelemetry/sdk-node');
const { getNodeAutoInstrumentations } = require('@opentelemetry/auto-instrumentations-node');
const { OTLPTraceExporter } = require('@opentelemetry/exporter-trace-otlp-http');
const sdk = new NodeSDK({
traceExporter: new OTLPTraceExporter({
url: 'http://localhost:4318/v1/traces'
}),
instrumentations: [getNodeAutoInstrumentations()]
});
sdk.start();
// Now ALL HTTP requests, DB queries, and framework calls are traced automatically
Manual Instrumentation
const { trace } = require('@opentelemetry/api');
const tracer = trace.getTracer('my-service');
async function processOrder(orderId) {
return tracer.startActiveSpan('process-order', async (span) => {
span.setAttribute('order.id', orderId);
try {
const items = await tracer.startActiveSpan('fetch-items', async (childSpan) => {
const result = await db.query('SELECT * FROM items WHERE order_id = ?', [orderId]);
childSpan.setAttribute('items.count', result.length);
childSpan.end();
return result;
});
await tracer.startActiveSpan('charge-payment', async (childSpan) => {
await paymentService.charge(orderId, items);
childSpan.end();
});
span.setStatus({ code: 1 }); // OK
} catch (err) {
span.setStatus({ code: 2, message: err.message });
span.recordException(err);
throw err;
} finally {
span.end();
}
});
}
Metrics API
const { metrics } = require('@opentelemetry/api');
const meter = metrics.getMeter('my-service');
const requestCounter = meter.createCounter('http.requests', {
description: 'Total HTTP requests'
});
const responseTime = meter.createHistogram('http.response_time', {
description: 'Response time in ms', unit: 'ms'
});
app.use((req, res, next) => {
const start = Date.now();
res.on('finish', () => {
requestCounter.add(1, { method: req.method, status: res.statusCode });
responseTime.record(Date.now() - start, { method: req.method });
});
next();
});
Why This Matters
- Vendor-neutral: Switch backends without code changes
- Auto-instrumentation: Traces for free with zero code
- Full stack: Traces + metrics + logs in one SDK
- Industry standard: CNCF graduated project, supported everywhere
Need custom observability tooling or monitoring automation? I build developer tools and data pipelines. Check out my web scraping actors on Apify or reach out at spinov001@gmail.com for custom solutions.
Top comments (0)