Trasys
Node.js SDKFrameworks

NestJS

Instrument a NestJS application on either the Express or Fastify adapter by applying the Trasys SDK middleware in main.ts before the app starts listening.

NestJS

NestJS runs on either Express (default) or Fastify under the hood. The SDK works with both adapters.

Express adapter (default)

// main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { sdk } from './trasys';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);

  // Apply before everything else using the underlying Express instance
  app.use(sdk.middleware());

  await app.listen(3000);
}

bootstrap();

Fastify adapter

// main.ts
import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { AppModule } from './app.module';
import { sdk } from './trasys';

async function bootstrap() {
  const app = await NestFactory.create<NestFastifyApplication>(
    AppModule,
    new FastifyAdapter(),
  );

  await app.register(sdk.fastifyplugin());
  await app.listen({ port: 3000, host: '0.0.0.0' });
}

bootstrap();

Route parameter normalization

NestJS decorators (@Get(':id'), @Post(), etc.) set the parameterized route name on req.route.path after the controller handler runs, not when the middleware first executes.

The Trasys middleware handles this automatically: it re-reads req.route.path at response finish time and updates the span name. You will see /users/:id in your dashboard, not /users/123. No extra configuration is required.

Guards and interceptors

The SDK middleware captures the full request lifecycle including time spent in guards, interceptors, and pipes. Each stage appears in the request span's timeline.

If you want to add trace context to a custom guard or interceptor, use the logger:

import { Injectable, CanActivate, ExecutionContext } from '@nestjs/common';
import { logger } from './trasys';

@Injectable()
export class AuthGuard implements CanActivate {
  canActivate(context: ExecutionContext): boolean {
    const request = context.switchToHttp().getRequest();

    if (!request.user) {
      logger.warn('Unauthenticated request blocked', { path: request.path });
      return false;
    }

    return true;
  }
}

Next steps

On this page