-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Default API metrics for NestJS with fastify records full url path instead of full route #245
Comments
any plans for this to be fixed? |
I did some research on this, and apparently, the issue is that fastify uses middie to support middlewares, and instead of using the express request object, it uses the req.raw value. That's why the req.route value in the code below is undefined when using fastify. here is an explanation of the middie/fastify/middleware problem It seems like if we use
|
Add a metrics interceptor so it can get the proper route path for fastify applications. See pragmaticivan#245
I worked around this by adding import type { CallHandler, ExecutionContext, NestInterceptor } from '@nestjs/common';
import { Injectable } from '@nestjs/common';
import type { FastifyRequest, RawRequestDefaultExpression } from 'fastify';
import type { Observable } from 'rxjs';
interface RequestRoute {
route: {
path: string;
};
}
/**
* Add the router path so it is visibile in the middleware as req.route.path which
* nestjs-otel uses for route metrics.
* See https://github.com/pragmaticivan/nestjs-otel/issues/245
*/
@Injectable()
export class FastifyRoutePathInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<unknown> {
const request = context.switchToHttp().getRequest<FastifyRequest>();
// eslint-disable-next-line no-type-assertion/no-type-assertion
const raw = request.raw as RawRequestDefaultExpression & RequestRoute;
raw.route = {
path: request.routeOptions.url,
};
return next.handle();
}
} |
NestJS version: 8.0.0
nestjs-otel version: 3.0.1
NestJS-otel initialization code
NestJS App Initialization code
The default metric API collects the full URL instead of the route URL
example
/api/user/1
will be reported as/api/user/1
instead of/api/user/:id
The metric endpoint generate thousands of metrics for each id parameters instead of 1 metric only for this specific route
When i debugged the code, i found out that the default metric middle ware can't retrieve the route object from the request object.
The text was updated successfully, but these errors were encountered: