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

191
Vistas
¿Cómo se configura AWS X-Ray con NestJS?

Estoy trabajando en una aplicación NestJS y deseo configurar AWS X-Ray en ella. Soy nuevo en NestJS y no tengo claro cómo hacer esto.

En una aplicación Express, los documentos de AWS dicen que haces esto:

 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());

Pero no existe ese flujo lineal en NestJS, por lo que no estoy seguro de cómo se integraría AWS X-Ray. Me di cuenta de esta respuesta de hace 2 años (no estoy seguro de si hay una mejor manera ahora), pero eso no muestra la configuración de middleware necesaria, y tampoco muestra cómo se integraría el interceptor en la aplicación (es decir, ¿cuál será el la configuración sea como)

INTENTO DE SOLUCIÓN

Creé una clase de Middleware:

 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(); } }

Luego creé una clase Interceptor:

 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()))); } }

Luego actualicé app.module.ts a:

 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 }); } }

Y finalmente, agregué esta línea a mi main.ts :

 const app = await NestFactory.create(AppModule); app.useGlobalInterceptors(new XRayInterceptor(app.get(HttpAdapterHost)));

Cuando ejecuto la aplicación y llego a un punto final, aparece el siguiente error:

error

Gracias

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