Skip to content

Commit

Permalink
fix(v8/node/nestjs): Use method on current fastify request (#15104)
Browse files Browse the repository at this point in the history
Backports #15066

---------

Co-authored-by: tjhiggins <[email protected]>
  • Loading branch information
chargome and tjhiggins authored Jan 21, 2025
1 parent d296ce0 commit 8b870ba
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Controller, Get, Param, ParseIntPipe, UseFilters, UseGuards, UseInterceptors } from '@nestjs/common';
import { All, Controller, Get, Param, ParseIntPipe, UseFilters, UseGuards, UseInterceptors } from '@nestjs/common';
import { flush } from '@sentry/nestjs';
import { AppService } from './app.service';
import { AsyncInterceptor } from './async-example.interceptor';
Expand Down Expand Up @@ -121,4 +121,9 @@ export class AppController {
testFunctionName() {
return this.appService.getFunctionName();
}

@All('test-all')
testAll() {
return {};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -808,3 +808,8 @@ test('Calling canActivate method on service with Injectable decorator returns 20
const response = await fetch(`${baseURL}/test-service-canActivate`);
expect(response.status).toBe(200);
});

test('Calling @All method on service with Injectable decorator returns 200', async ({ baseURL }) => {
const response = await fetch(`${baseURL}/test-all`);
expect(response.status).toBe(200);
});
6 changes: 2 additions & 4 deletions packages/nestjs/src/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ import { isExpectedError } from './helpers';
// https://github.com/fastify/fastify/blob/87f9f20687c938828f1138f91682d568d2a31e53/types/request.d.ts#L41
interface FastifyRequest {
routeOptions?: {
method?: string;
url?: string;
};
method?: string;
}

// Partial extract of ExpressRequest interface
Expand Down Expand Up @@ -72,9 +72,7 @@ class SentryTracingInterceptor implements NestInterceptor {
const req = context.switchToHttp().getRequest() as FastifyRequest | ExpressRequest;
if ('routeOptions' in req && req.routeOptions && req.routeOptions.url) {
// fastify case
getIsolationScope().setTransactionName(
`${(req.routeOptions.method || 'GET').toUpperCase()} ${req.routeOptions.url}`,
);
getIsolationScope().setTransactionName(`${(req.method || 'GET').toUpperCase()} ${req.routeOptions.url}`);
} else if ('route' in req && req.route && req.route.path) {
// express case
getIsolationScope().setTransactionName(`${(req.method || 'GET').toUpperCase()} ${req.route.path}`);
Expand Down
4 changes: 2 additions & 2 deletions packages/node/src/integrations/tracing/fastify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ interface Fastify {
* Works for Fastify 3, 4 and presumably 5.
*/
interface FastifyRequestRouteInfo {
method?: string;
// since [email protected]
routeOptions?: {
url?: string;
method?: string;
};
routerPath?: string;
}
Expand Down Expand Up @@ -107,7 +107,7 @@ export function setupFastifyErrorHandler(fastify: Fastify): void {
// Taken from Otel Fastify instrumentation:
// https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts#L94-L96
const routeName = reqWithRouteInfo.routeOptions?.url || reqWithRouteInfo.routerPath;
const method = reqWithRouteInfo.routeOptions?.method || 'GET';
const method = reqWithRouteInfo.method || 'GET';

getIsolationScope().setTransactionName(`${method} ${routeName}`);
});
Expand Down
4 changes: 1 addition & 3 deletions packages/node/src/integrations/tracing/nest/nest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ export function setupNestErrorHandler(app: MinimalNestJsApp, baseFilter: NestJsE
const req = context.switchToHttp().getRequest();
if ('routeOptions' in req && req.routeOptions && req.routeOptions.url) {
// fastify case
getIsolationScope().setTransactionName(
`${req.routeOptions.method?.toUpperCase() || 'GET'} ${req.routeOptions.url}`,
);
getIsolationScope().setTransactionName(`${req.method?.toUpperCase() || 'GET'} ${req.routeOptions.url}`);
} else if ('route' in req && req.route && req.route.path) {
// express case
getIsolationScope().setTransactionName(`${req.method?.toUpperCase() || 'GET'} ${req.route.path}`);
Expand Down
2 changes: 1 addition & 1 deletion packages/node/src/integrations/tracing/nest/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
// https://github.com/fastify/fastify/blob/87f9f20687c938828f1138f91682d568d2a31e53/types/request.d.ts#L41
interface FastifyRequest {
routeOptions?: {
method?: string;
url?: string;
};
method?: string;
}

// Partial extract of ExpressRequest interface
Expand Down

0 comments on commit 8b870ba

Please sign in to comment.