src/core/interceptors/trace-id.interceptor.ts
Interceptor that adds the OpenTelemetry trace ID to HTTP response headers. This enables clients to correlate their requests with backend traces in observability tools like Grafana Tempo.
Response headers added:
X-Trace-Id: The trace ID in hex format (e.g., "abc123def456...")Usage: Clients can use this header to search for traces in Grafana:
Explore → Tempo → TraceQL: { trace:id="<X-Trace-Id value>" }
Methods |
| intercept | |||||||||
intercept(context: ExecutionContext, next: CallHandler)
|
|||||||||
|
Parameters :
Returns :
Observable<any>
|
import {
CallHandler,
ExecutionContext,
Injectable,
NestInterceptor,
} from "@nestjs/common";
import { trace } from "@opentelemetry/api";
import { Response } from "express";
import { Observable, tap } from "rxjs";
/**
* Interceptor that adds the OpenTelemetry trace ID to HTTP response headers.
* This enables clients to correlate their requests with backend traces in
* observability tools like Grafana Tempo.
*
* Response headers added:
* - `X-Trace-Id`: The trace ID in hex format (e.g., "abc123def456...")
*
* Usage: Clients can use this header to search for traces in Grafana:
* Explore → Tempo → TraceQL: `{ trace:id="<X-Trace-Id value>" }`
*/
@Injectable()
export class TraceIdInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const response = context.switchToHttp().getResponse<Response>();
return next.handle().pipe(
tap(() => {
const span = trace.getActiveSpan();
if (span) {
const traceId = span.spanContext().traceId;
response.setHeader("X-Trace-Id", traceId);
}
}),
);
}
}