NestJS Instrumentation Library not Collecting Trace Data #3503
-
Hey guys, I recently picked up opentelemetry and Jaeger for adding observability to my projects. I'm using NestJs as my backend platform. Previously, I was using the auto instrumentation library to use all the available instrumentations to collect any sort of trace data. I noticed that it had a nestjs-core instrumentation library to collect trace data of the controllers, but it never seems to show in Jaeger. This is how I have everything setup:
import { Resource } from '@opentelemetry/resources';
import { OTLPTraceExporter } from '@opentelemetry/exporter-trace-otlp-http';
import { SemanticResourceAttributes } from '@opentelemetry/semantic-conventions';
import * as opentelemetry from '@opentelemetry/sdk-node';
import { getNodeAutoInstrumentations } from '@opentelemetry/auto-instrumentations-node';
import { dockerCGroupV1Detector } from '@opentelemetry/resource-detector-docker';
const traceExporter = new OTLPTraceExporter();
const sdk = new opentelemetry.NodeSDK({
traceExporter,
instrumentations: [
getNodeAutoInstrumentations({
'@opentelemetry/instrumentation-express': {
requestHook: (span, reqInfo) => {
span.setAttribute(
'request-headers',
JSON.stringify(reqInfo.request.headers),
);
},
},
}),
],
resourceDetectors: [dockerCGroupV1Detector],
resource: new Resource({
[SemanticResourceAttributes.SERVICE_NAME]: 'item_service',
}),
});
sdk
.start()
.then(() => console.log('Tracing initialized'))
.catch((error) => console.log('Error initializing tracing', error));
// gracefully shut down the SDK on process exit
process.on('SIGTERM', () => {
sdk
.shutdown()
.then(() => console.log('Tracing terminated'))
.catch((error) => console.log('Error terminating tracing', error))
.finally(() => process.exit(0));
});
export default sdk;
import { NestFactory } from '@nestjs/core';
import { Transport, MicroserviceOptions } from '@nestjs/microservices';
import { AppModule } from './app.module';
import tracer from './tracer';
async function bootstrap() {
await tracer.start();
const app = await NestFactory.create(AppModule);
const microservice = app.connectMicroservice({
transport: Transport.REDIS,
options: {
host: 'localhost',
port: 6379,
},
});
await app.startAllMicroservices();
await app.listen(8080);
}
bootstrap(); I made no other configurations to the other files because I expected the auto instrumentation library to handle everything for me. The only trace data I get are from HTTP and Express instrumentation. Any advice on things I may be missing? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
Also |
Beta Was this translation helpful? Give feedback.
-
I just had a similar issue. The problem was that we used a mono repo setup and bundle apps with We ended up specifying an You can find a working example here (last test app): |
Beta Was this translation helpful? Give feedback.
import tracer from './tracer';
should be the very first line in main.ts to ensure instrumentations are registered before the actual module is loaded. Once it's loaded it's to late...Also
tracer.start()
should be called before you import nestjs,...