Skip to content

Commit 8b870ba

Browse files
chargometjhiggins
andauthored
fix(v8/node/nestjs): Use method on current fastify request (#15104)
Backports #15066 --------- Co-authored-by: tjhiggins <[email protected]>
1 parent d296ce0 commit 8b870ba

File tree

6 files changed

+17
-11
lines changed

6 files changed

+17
-11
lines changed

dev-packages/e2e-tests/test-applications/nestjs-fastify/src/app.controller.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Controller, Get, Param, ParseIntPipe, UseFilters, UseGuards, UseInterceptors } from '@nestjs/common';
1+
import { All, Controller, Get, Param, ParseIntPipe, UseFilters, UseGuards, UseInterceptors } from '@nestjs/common';
22
import { flush } from '@sentry/nestjs';
33
import { AppService } from './app.service';
44
import { AsyncInterceptor } from './async-example.interceptor';
@@ -121,4 +121,9 @@ export class AppController {
121121
testFunctionName() {
122122
return this.appService.getFunctionName();
123123
}
124+
125+
@All('test-all')
126+
testAll() {
127+
return {};
128+
}
124129
}

dev-packages/e2e-tests/test-applications/nestjs-fastify/tests/transactions.test.ts

+5
Original file line numberDiff line numberDiff line change
@@ -808,3 +808,8 @@ test('Calling canActivate method on service with Injectable decorator returns 20
808808
const response = await fetch(`${baseURL}/test-service-canActivate`);
809809
expect(response.status).toBe(200);
810810
});
811+
812+
test('Calling @All method on service with Injectable decorator returns 200', async ({ baseURL }) => {
813+
const response = await fetch(`${baseURL}/test-all`);
814+
expect(response.status).toBe(200);
815+
});

packages/nestjs/src/setup.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ import { isExpectedError } from './helpers';
2727
// https://github.com/fastify/fastify/blob/87f9f20687c938828f1138f91682d568d2a31e53/types/request.d.ts#L41
2828
interface FastifyRequest {
2929
routeOptions?: {
30-
method?: string;
3130
url?: string;
3231
};
32+
method?: string;
3333
}
3434

3535
// Partial extract of ExpressRequest interface
@@ -72,9 +72,7 @@ class SentryTracingInterceptor implements NestInterceptor {
7272
const req = context.switchToHttp().getRequest() as FastifyRequest | ExpressRequest;
7373
if ('routeOptions' in req && req.routeOptions && req.routeOptions.url) {
7474
// fastify case
75-
getIsolationScope().setTransactionName(
76-
`${(req.routeOptions.method || 'GET').toUpperCase()} ${req.routeOptions.url}`,
77-
);
75+
getIsolationScope().setTransactionName(`${(req.method || 'GET').toUpperCase()} ${req.routeOptions.url}`);
7876
} else if ('route' in req && req.route && req.route.path) {
7977
// express case
8078
getIsolationScope().setTransactionName(`${(req.method || 'GET').toUpperCase()} ${req.route.path}`);

packages/node/src/integrations/tracing/fastify.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,10 @@ interface Fastify {
2525
* Works for Fastify 3, 4 and presumably 5.
2626
*/
2727
interface FastifyRequestRouteInfo {
28+
method?: string;
2829
2930
routeOptions?: {
3031
url?: string;
31-
method?: string;
3232
};
3333
routerPath?: string;
3434
}
@@ -107,7 +107,7 @@ export function setupFastifyErrorHandler(fastify: Fastify): void {
107107
// Taken from Otel Fastify instrumentation:
108108
// https://github.com/open-telemetry/opentelemetry-js-contrib/blob/main/plugins/node/opentelemetry-instrumentation-fastify/src/instrumentation.ts#L94-L96
109109
const routeName = reqWithRouteInfo.routeOptions?.url || reqWithRouteInfo.routerPath;
110-
const method = reqWithRouteInfo.routeOptions?.method || 'GET';
110+
const method = reqWithRouteInfo.method || 'GET';
111111

112112
getIsolationScope().setTransactionName(`${method} ${routeName}`);
113113
});

packages/node/src/integrations/tracing/nest/nest.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ export function setupNestErrorHandler(app: MinimalNestJsApp, baseFilter: NestJsE
9191
const req = context.switchToHttp().getRequest();
9292
if ('routeOptions' in req && req.routeOptions && req.routeOptions.url) {
9393
// fastify case
94-
getIsolationScope().setTransactionName(
95-
`${req.routeOptions.method?.toUpperCase() || 'GET'} ${req.routeOptions.url}`,
96-
);
94+
getIsolationScope().setTransactionName(`${req.method?.toUpperCase() || 'GET'} ${req.routeOptions.url}`);
9795
} else if ('route' in req && req.route && req.route.path) {
9896
// express case
9997
getIsolationScope().setTransactionName(`${req.method?.toUpperCase() || 'GET'} ${req.route.path}`);

packages/node/src/integrations/tracing/nest/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
// https://github.com/fastify/fastify/blob/87f9f20687c938828f1138f91682d568d2a31e53/types/request.d.ts#L41
55
interface FastifyRequest {
66
routeOptions?: {
7-
method?: string;
87
url?: string;
98
};
9+
method?: string;
1010
}
1111

1212
// Partial extract of ExpressRequest interface

0 commit comments

Comments
 (0)