Prompt
How do I add OpenTelemetry tracing to a backend app
Latest observation
Adding OpenTelemetry tracing to a backend app usually involves 4 steps:
- Add the OpenTelemetry SDK/dependencies
- Configure a tracer provider and exporter
- Instrument your framework and libraries
- Send traces to an observability backend
Here’s the general approach, then I’ll show a small example.
1) Pick where traces will go
Common choices:
- OTLP collector (recommended)
- Jaeger
- Zipkin
- Datadog / New Relic / Honeycomb / Grafana Tempo, etc.
Most teams use an OpenTelemetry Collector as a middle layer, then forward to their vendor/backend.
2) Install OpenTelemetry packages
Examples by language
- Node.js:
@opentelemetry/sdk-node, instrumentations for HTTP/Express/DBs - Python:
opentelemetry-sdk,opentelemetry-exporter-otlp, instrumentations - Java: OpenTelemetry Java agent or SDK
- Go:
go.opentelemetry.io/otel, exporters, instrumentations - .NET:
OpenTelemetry,OpenTelemetry.Exporter.OpenTelemetryProtocol
3) Initialize tracing early in app startup
You generally want tracing initialized before your app creates the HTTP server or loads libraries you want auto-instrumented.
Key configuration items:
- Service name: e.g.
orders-api - Exporter endpoint: collector/backend URL
- Sampling: always, probabilistic, parent-based, etc.
- Resource attributes: environment, version, region, etc.
4) Auto-instrument common libraries
OpenTelemetry can automatically create spans for:
- Incoming HTTP requests
- Outgoing HTTP calls
- Database queries
- Message queue operations
- Framework handlers
You can also create manual spans for important business operations.
Minimal example
Node.js example
// tracing.js
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();
Then import this before your app starts:
require('./tracing');
const express = require('express');
const app = express();
app.get('/hello', (req, res) => {
res.send('hello');
});
app.listen(3000);
Python example
# tracing.py
from opentelemetry import trace
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
resource = Resource.create({"service.name": "orders-api"})
provider = TracerProvider(resource=resource)
processor = BatchSpanProcessor(OTLPSpanExporter(endpoint="http://localhost:4318/v1/traces"))
provider.add_span_processor(processor)
trace.set_tracer_provider(provider)
Then in your app:
import tracing
from fastapi import FastAPI
app = FastAPI()
@app.get("/hello")
def hello():
return {"message": "hello"}
For FastAPI/Flask/Django you’d usually also add auto-instrumentation.
Manual spans
Use manual spans for custom work that auto-instrumentation won’t capture well:
const tracer = opentelemetry.trace.getTracer('my-service');
async function processOrder(orderId) {
return tracer.startActiveSpan('process-order', async (span) => {
try {
// business logic
span.setAttribute('order.id', orderId);
return { ok: true };
} catch (err) {
span.recordException(err);
span.setStatus({ code: 2, message: err.message });
throw err;
} finally {
span.end();
}
});
}
Best practices
- Use a collector instead of sending directly to a vendor when possible
- Initialize tracing at process startup
- Propagate context across services using W3C Trace Context
- Set meaningful span names
- Add attributes for key business fields, but avoid sensitive data
- Sample appropriately in production
- Avoid high-cardinality attributes like raw user IDs in tags if they explode metrics/search
What you’ll typically need in production
- OpenTelemetry SDK
- Auto-instrumentation for your framework
- OTLP exporter
- OpenTelemetry Collector
- Backend like Tempo/Jaeger/Honeycomb/Datadog/New Relic
If you want, I can give you a ready-to-run setup for a specific stack like Node/Express, Python/FastAPI, Java/Spring, Go, or .NET.