Nestjs-OpenTelemetry
Nestjs-OpenTelemetry copied to clipboard
MetricHttpEventProducer => response.once is not defined
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.
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
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.
#49