OpenTelemetry in Production Microservices
Tracing, metrics, and logs with consistent correlation IDs across NestJS services and workers.
Nestlancer Editorial

Distributed tracing only helps when every hop shares a trace ID. In NestJS microservice fleets, consistent OpenTelemetry instrumentation turns vague "it's slow" reports into span waterfalls you can act on.
Instrumentation baseline
- Auto-instrument HTTP, gRPC, and database drivers
- Propagate
traceparentheaders through the API gateway - Attach
service.name,deployment.environment, andrequest.idattributes - Export to OTLP collector—avoid vendor-specific SDK lock-in at the edge
NestJS integration pattern
import { trace } from '@opentelemetry/api';
const tracer = trace.getTracer('blog-service');
const span = tracer.startSpan('posts.publish', { attributes: { postId } });
try {
await this.outbox.enqueue(event);
} finally {
span.end();
}
Wrap outbound HTTP calls and Prisma queries in child spans. Missing database spans hide 80% of latency.
Logs and metrics correlation
| Signal | Correlation key |
|---|---|
| Logs | trace_id, span_id |
| Metrics | Exemplars linking to traces |
| Errors | Stack trace + trace URL in alert |
Sampling strategy
Head-based 100% sampling collapses under load. Use tail sampling in the collector for errors and high-latency traces. Keep always-on sampling for payment and auth paths.
Observability is production infrastructure—invest in correlation IDs before the next cross-service outage.
Comments
Loading comments…
Related posts

Case Studies
Cutting Deploy Time from 45 Minutes to Five
CI caching, smaller artifacts, and service-level pipelines after monolith split.

Case Studies
Scaling a Freelance Marketplace Architecture
Matching algorithms, escrow flows, and dispute resolution at growing GMV.

Case Studies
GDPR Compliance Platform Rebuild
Data maps, deletion workflows, and consent logging across microservices.

Case Studies
Migrating from WebSockets to SSE
Simpler infra, CDN friendliness, and trade-offs for one-way realtime feeds.