Skip to content

NestJS

instrumentation.ts
import { init } from 'autotel';
init({
service: 'my-nestjs-api',
autoInstrumentations: ['http', 'nestjs-core'],
endpoint: process.env.OTLP_ENDPOINT,
});

Run with:

Terminal window
tsx --import ./instrumentation.ts src/main.ts

Use trace() inside NestJS services for business logic spans:

import { Injectable } from '@nestjs/common';
import { trace, type TraceContext } from 'autotel';
@Injectable()
export class UserService {
private readonly tracedFetchUser = trace(
(ctx: TraceContext) => async (userId: string) => {
ctx.setAttribute('db.query', 'SELECT * FROM users WHERE id = ?');
ctx.setAttribute('db.userId', userId);
return await this.prisma.user.findUnique({ where: { id: userId } });
},
);
async getUser(userId: string) {
return this.tracedFetchUser(userId);
}
}
main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();

With autoInstrumentations: ['http', 'nestjs-core']:

  • HTTP server spans — automatic per-request spans
  • NestJS controllers/services — handler execution time
  • Guards, interceptors, pipes — lifecycle timing
  • Your trace() calls — business logic spans as children
  • example-nestjs — NestJS with HTTP/NestJS auto-instrumentation and manual trace() in services.