Nestjs-OpenTelemetry icon indicating copy to clipboard operation
Nestjs-OpenTelemetry copied to clipboard

MetricHttpEventProducer => response.once is not defined

Open azharmehmoud opened this issue 3 years ago • 2 comments

Hi,

I am using nestjs with fastify adapter. Integrated this in my application.

Whenever i hit my api i get response.once is not defined. I believe this does not supports Fastify or may be i am doing configuration wrong.

I was able to debug and came to this class MetricHttpEventProducer which was giving error. line no 14.

response.once('finish', () =>
  this.publish(request, response, startAt, exception),
);

Please help me in getting this fix.

azharmehmoud avatar Mar 13 '22 18:03 azharmehmoud

not tested in fastify before 🤔, maybe we can support fastify in the near future

for now you can disable auto metrics injector and you can implement manually via decorators

MetinSeylan avatar Mar 13 '22 18:03 MetinSeylan

Alright. I came up with a hack. Since fastify request is of type Reply. I created an interceptor and added some method in Reply prototype

import {
  Injectable,
  NestInterceptor,
  ExecutionContext,
  CallHandler,
} from '@nestjs/common';

@Injectable()
export class ReqResModifyInterceptor implements NestInterceptor {
  intercept(context: ExecutionContext, next: CallHandler) {
    const request = context.switchToHttp().getRequest();
    const response = context.switchToHttp().getResponse();
    request.route = Object.assign(request.route || {}, {
      path: request.raw.url,
    });
    response.__proto__.once = response.raw.once;
    response.__proto__.removeListener = response.raw.removeListener;
    response.__proto__.on = function (method, callback) {
      callback();
    };
    return next.handle();
  }
}

Hope this will help someone till Fastify is officially supported by this package.

azharmehmoud avatar Mar 14 '22 06:03 azharmehmoud

#49

MetinSeylan avatar Oct 15 '22 17:10 MetinSeylan