Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

196
Vistas
How does one setup AWS X-Ray with NestJS

I'm working in a NestJS app, and I wish to setup AWS X-Ray on it. I'm new to NestJS, and I'm not clear on how to do this.

In an Express app, the AWS docs say you do this:

var app = express();

var AWSXRay = require('aws-xray-sdk');
app.use(AWSXRay.express.openSegment('MyApp'));

app.get('/', function (req, res) {
  res.render('index');
});

app.use(AWSXRay.express.closeSegment());

But there isn't that linear flow in NestJS, so I'm not sure how AWS X-Ray would be integrated. I noticed this answer from 2 years ago (not sure if there's a better way now), but that doesn't show the middleware setup necessary, and it also doesn't show how the interceptor would integrate into the app (i.e. what will the setup be like)

ATTEMPTED SOLUTION

I created a Middleware class:

import { Injectable, NestMiddleware } from '@nestjs/common';
import { Request, Response, NextFunction } from 'express';
import AWSXRay from 'aws-xray-sdk';

@Injectable()
export class XRayMiddleware implements NestMiddleware {
  use(req: Request, res: Response, next: NextFunction) {
    AWSXRay.express.openSegment('application-name');
    next();
  }
}

I then created an Interceptor class:

import { Injectable, NestInterceptor, ExecutionContext, CallHandler } from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import AWSXRay from 'aws-xray-sdk';
import { HttpAdapterHost } from '@nestjs/core';

@Injectable()
export class XRayInterceptor implements NestInterceptor {
  constructor(private readonly httpAdapter: HttpAdapterHost) {}

  intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
    return next.handle().pipe(tap(() => this.httpAdapter.httpAdapter.use(AWSXRay.express.closeSegment())));
  }
}

I then updated the app.module.ts to:

import { AppConfigModule } from '@app-config/app-config.module';
import { Module, NestModule, MiddlewareConsumer, RequestMethod } from '@nestjs/common';
import { CustomersModule } from './customers/customers.module';
import { HealthModule } from './health/health.module';
import { XRayMiddleware } from './middleware/xray.middleware';

@Module({
  imports: [AppConfigModule, HealthModule, CustomersModule],
  providers: [],
})
export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    consumer.apply(XRayMiddleware).forRoutes({ path: '*', method: RequestMethod.ALL });
  }
}

And finally, I added this line to my main.ts:

const app = await NestFactory.create(AppModule);

app.useGlobalInterceptors(new XRayInterceptor(app.get(HttpAdapterHost)));

When I run the application, and hit an endpoint, I get the following error:

error

Thanks

about 4 years ago · Santiago Gelvez
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda