-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(nestjs): Add
SentryGlobalGenericFilter
and allow specifying ap…
…plication ref in global filter (#13673)
- Loading branch information
Showing
17 changed files
with
414 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
dev-packages/e2e-tests/test-applications/nestjs-basic-with-graphql/.gitignore
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# compiled output | ||
/dist | ||
/node_modules | ||
/build | ||
|
||
# Logs | ||
logs | ||
*.log | ||
npm-debug.log* | ||
pnpm-debug.log* | ||
yarn-debug.log* | ||
yarn-error.log* | ||
lerna-debug.log* | ||
|
||
# OS | ||
.DS_Store | ||
|
||
# Tests | ||
/coverage | ||
/.nyc_output | ||
|
||
# IDEs and editors | ||
/.idea | ||
.project | ||
.classpath | ||
.c9/ | ||
*.launch | ||
.settings/ | ||
*.sublime-workspace | ||
|
||
# IDE - VSCode | ||
.vscode/* | ||
!.vscode/settings.json | ||
!.vscode/tasks.json | ||
!.vscode/launch.json | ||
!.vscode/extensions.json | ||
|
||
# dotenv environment variable files | ||
.env | ||
.env.development.local | ||
.env.test.local | ||
.env.production.local | ||
.env.local | ||
|
||
# temp directory | ||
.temp | ||
.tmp | ||
|
||
# Runtime data | ||
pids | ||
*.pid | ||
*.seed | ||
*.pid.lock | ||
|
||
# Diagnostic reports (https://nodejs.org/api/report.html) | ||
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json |
2 changes: 2 additions & 0 deletions
2
dev-packages/e2e-tests/test-applications/nestjs-basic-with-graphql/.npmrc
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
@sentry:registry=http://127.0.0.1:4873 | ||
@sentry-internal:registry=http://127.0.0.1:4873 |
8 changes: 8 additions & 0 deletions
8
dev-packages/e2e-tests/test-applications/nestjs-basic-with-graphql/nest-cli.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"$schema": "https://json.schemastore.org/nest-cli", | ||
"collection": "@nestjs/schematics", | ||
"sourceRoot": "src", | ||
"compilerOptions": { | ||
"deleteOutDir": true | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
dev-packages/e2e-tests/test-applications/nestjs-basic-with-graphql/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
{ | ||
"name": "nestjs-basic-with-graphql", | ||
"version": "0.0.1", | ||
"private": true, | ||
"scripts": { | ||
"build": "nest build", | ||
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"", | ||
"start": "nest start", | ||
"start:dev": "nest start --watch", | ||
"start:debug": "nest start --debug --watch", | ||
"start:prod": "node dist/main", | ||
"clean": "npx rimraf node_modules pnpm-lock.yaml", | ||
"test": "playwright test", | ||
"test:build": "pnpm install", | ||
"test:assert": "pnpm test" | ||
}, | ||
"dependencies": { | ||
"@apollo/server": "^4.10.4", | ||
"@nestjs/apollo": "^12.2.0", | ||
"@nestjs/common": "^10.3.10", | ||
"@nestjs/core": "^10.3.10", | ||
"@nestjs/graphql": "^12.2.0", | ||
"@nestjs/platform-express": "^10.3.10", | ||
"@sentry/nestjs": "^8.21.0", | ||
"graphql": "^16.9.0", | ||
"reflect-metadata": "^0.1.13", | ||
"rxjs": "^7.8.1" | ||
}, | ||
"devDependencies": { | ||
"@playwright/test": "^1.44.1", | ||
"@sentry-internal/test-utils": "link:../../../test-utils", | ||
"@nestjs/cli": "^10.0.0", | ||
"@nestjs/schematics": "^10.0.0", | ||
"@nestjs/testing": "^10.0.0", | ||
"@types/express": "^4.17.17", | ||
"@types/node": "18.15.1", | ||
"@types/supertest": "^6.0.0", | ||
"@typescript-eslint/eslint-plugin": "^6.0.0", | ||
"@typescript-eslint/parser": "^6.0.0", | ||
"eslint": "^8.42.0", | ||
"eslint-config-prettier": "^9.0.0", | ||
"eslint-plugin-prettier": "^5.0.0", | ||
"prettier": "^3.0.0", | ||
"source-map-support": "^0.5.21", | ||
"supertest": "^6.3.3", | ||
"ts-loader": "^9.4.3", | ||
"tsconfig-paths": "^4.2.0", | ||
"typescript": "^4.9.5" | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
dev-packages/e2e-tests/test-applications/nestjs-basic-with-graphql/playwright.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { getPlaywrightConfig } from '@sentry-internal/test-utils'; | ||
|
||
const config = getPlaywrightConfig({ | ||
startCommand: `pnpm start`, | ||
}); | ||
|
||
export default config; |
22 changes: 22 additions & 0 deletions
22
dev-packages/e2e-tests/test-applications/nestjs-basic-with-graphql/src/app.controller.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Controller, Get, Param } from '@nestjs/common'; | ||
import { AppService } from './app.service'; | ||
|
||
@Controller() | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
|
||
@Get('test-exception/:id') | ||
async testException(@Param('id') id: string) { | ||
return this.appService.testException(id); | ||
} | ||
|
||
@Get('test-expected-400-exception/:id') | ||
async testExpected400Exception(@Param('id') id: string) { | ||
return this.appService.testExpected400Exception(id); | ||
} | ||
|
||
@Get('test-expected-500-exception/:id') | ||
async testExpected500Exception(@Param('id') id: string) { | ||
return this.appService.testExpected500Exception(id); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
dev-packages/e2e-tests/test-applications/nestjs-basic-with-graphql/src/app.module.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { ApolloDriver } from '@nestjs/apollo'; | ||
import { Logger, Module } from '@nestjs/common'; | ||
import { GraphQLModule } from '@nestjs/graphql'; | ||
import { SentryModule } from '@sentry/nestjs/setup'; | ||
import { AppController } from './app.controller'; | ||
import { AppResolver } from './app.resolver'; | ||
import { AppService } from './app.service'; | ||
|
||
@Module({ | ||
imports: [ | ||
SentryModule.forRoot(), | ||
GraphQLModule.forRoot({ | ||
driver: ApolloDriver, | ||
autoSchemaFile: true, | ||
playground: true, // sets up a playground on https://localhost:3000/graphql | ||
}), | ||
], | ||
controllers: [AppController], | ||
providers: [ | ||
AppService, | ||
AppResolver, | ||
{ | ||
provide: Logger, | ||
useClass: Logger, | ||
}, | ||
], | ||
}) | ||
export class AppModule {} |
14 changes: 14 additions & 0 deletions
14
dev-packages/e2e-tests/test-applications/nestjs-basic-with-graphql/src/app.resolver.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Query, Resolver } from '@nestjs/graphql'; | ||
|
||
@Resolver() | ||
export class AppResolver { | ||
@Query(() => String) | ||
test(): string { | ||
return 'Test endpoint!'; | ||
} | ||
|
||
@Query(() => String) | ||
error(): string { | ||
throw new Error('This is an exception!'); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
dev-packages/e2e-tests/test-applications/nestjs-basic-with-graphql/src/app.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
testException(id: string) { | ||
throw new Error(`This is an exception with id ${id}`); | ||
} | ||
|
||
testExpected400Exception(id: string) { | ||
throw new HttpException(`This is an expected 400 exception with id ${id}`, HttpStatus.BAD_REQUEST); | ||
} | ||
|
||
testExpected500Exception(id: string) { | ||
throw new HttpException(`This is an expected 500 exception with id ${id}`, HttpStatus.INTERNAL_SERVER_ERROR); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
dev-packages/e2e-tests/test-applications/nestjs-basic-with-graphql/src/instrument.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import * as Sentry from '@sentry/nestjs'; | ||
|
||
Sentry.init({ | ||
environment: 'qa', // dynamic sampling bias to keep transactions | ||
dsn: process.env.E2E_TEST_DSN, | ||
tunnel: `http://localhost:3031/`, // proxy server | ||
tracesSampleRate: 1, | ||
}); |
20 changes: 20 additions & 0 deletions
20
dev-packages/e2e-tests/test-applications/nestjs-basic-with-graphql/src/main.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Import this first | ||
import './instrument'; | ||
|
||
// Import other modules | ||
import { HttpAdapterHost, NestFactory } from '@nestjs/core'; | ||
import { SentryGlobalGenericFilter } from '@sentry/nestjs/setup'; | ||
import { AppModule } from './app.module'; | ||
|
||
const PORT = 3030; | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.create(AppModule); | ||
|
||
const { httpAdapter } = app.get(HttpAdapterHost); | ||
app.useGlobalFilters(new SentryGlobalGenericFilter(httpAdapter as any)); | ||
|
||
await app.listen(PORT); | ||
} | ||
|
||
bootstrap(); |
6 changes: 6 additions & 0 deletions
6
dev-packages/e2e-tests/test-applications/nestjs-basic-with-graphql/start-event-proxy.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { startEventProxyServer } from '@sentry-internal/test-utils'; | ||
|
||
startEventProxyServer({ | ||
port: 3031, | ||
proxyServerName: 'nestjs-basic-with-graphql', | ||
}); |
118 changes: 118 additions & 0 deletions
118
dev-packages/e2e-tests/test-applications/nestjs-basic-with-graphql/tests/errors.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import { expect, test } from '@playwright/test'; | ||
import { waitForError, waitForTransaction } from '@sentry-internal/test-utils'; | ||
|
||
test('Sends exception to Sentry', async ({ baseURL }) => { | ||
const errorEventPromise = waitForError('nestjs-basic-with-graphql', event => { | ||
return !event.type && event.exception?.values?.[0]?.value === 'This is an exception with id 123'; | ||
}); | ||
|
||
const response = await fetch(`${baseURL}/test-exception/123`); | ||
expect(response.status).toBe(500); | ||
|
||
const errorEvent = await errorEventPromise; | ||
|
||
expect(errorEvent.exception?.values).toHaveLength(1); | ||
expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an exception with id 123'); | ||
|
||
expect(errorEvent.request).toEqual({ | ||
method: 'GET', | ||
cookies: {}, | ||
headers: expect.any(Object), | ||
url: 'http://localhost:3030/test-exception/123', | ||
}); | ||
|
||
expect(errorEvent.transaction).toEqual('GET /test-exception/:id'); | ||
|
||
expect(errorEvent.contexts?.trace).toEqual({ | ||
trace_id: expect.any(String), | ||
span_id: expect.any(String), | ||
}); | ||
}); | ||
|
||
test('Does not send HttpExceptions to Sentry', async ({ baseURL }) => { | ||
let errorEventOccurred = false; | ||
|
||
waitForError('nestjs-basic-with-graphql', event => { | ||
if (!event.type && event.exception?.values?.[0]?.value === 'This is an expected 400 exception with id 123') { | ||
errorEventOccurred = true; | ||
} | ||
|
||
return event?.transaction === 'GET /test-expected-400-exception/:id'; | ||
}); | ||
|
||
waitForError('nestjs-basic-with-graphql', event => { | ||
if (!event.type && event.exception?.values?.[0]?.value === 'This is an expected 500 exception with id 123') { | ||
errorEventOccurred = true; | ||
} | ||
|
||
return event?.transaction === 'GET /test-expected-500-exception/:id'; | ||
}); | ||
|
||
const transactionEventPromise400 = waitForTransaction('nestjs-basic-with-graphql', transactionEvent => { | ||
return transactionEvent?.transaction === 'GET /test-expected-400-exception/:id'; | ||
}); | ||
|
||
const transactionEventPromise500 = waitForTransaction('nestjs-basic-with-graphql', transactionEvent => { | ||
return transactionEvent?.transaction === 'GET /test-expected-500-exception/:id'; | ||
}); | ||
|
||
const response400 = await fetch(`${baseURL}/test-expected-400-exception/123`); | ||
expect(response400.status).toBe(400); | ||
|
||
const response500 = await fetch(`${baseURL}/test-expected-500-exception/123`); | ||
expect(response500.status).toBe(500); | ||
|
||
await transactionEventPromise400; | ||
await transactionEventPromise500; | ||
|
||
(await fetch(`${baseURL}/flush`)).text(); | ||
|
||
expect(errorEventOccurred).toBe(false); | ||
}); | ||
|
||
test('Sends graphql exception to Sentry', async ({ baseURL }) => { | ||
const errorEventPromise = waitForError('nestjs-basic-with-graphql', event => { | ||
return !event.type && event.exception?.values?.[0]?.value === 'This is an exception!'; | ||
}); | ||
|
||
const response = await fetch(`${baseURL}/graphql`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
query: `query { error }`, | ||
}), | ||
}); | ||
|
||
const json_response = await response.json(); | ||
const errorEvent = await errorEventPromise; | ||
|
||
expect(json_response?.errors[0]).toEqual({ | ||
message: 'This is an exception!', | ||
locations: expect.any(Array), | ||
path: ['error'], | ||
extensions: { | ||
code: 'INTERNAL_SERVER_ERROR', | ||
stacktrace: expect.any(Array), | ||
}, | ||
}); | ||
|
||
expect(errorEvent.exception?.values).toHaveLength(1); | ||
expect(errorEvent.exception?.values?.[0]?.value).toBe('This is an exception!'); | ||
|
||
expect(errorEvent.request).toEqual({ | ||
method: 'POST', | ||
cookies: {}, | ||
data: '{"query":"query { error }"}', | ||
headers: expect.any(Object), | ||
url: 'http://localhost:3030/graphql', | ||
}); | ||
|
||
expect(errorEvent.transaction).toEqual('POST /graphql'); | ||
|
||
expect(errorEvent.contexts?.trace).toEqual({ | ||
trace_id: expect.any(String), | ||
span_id: expect.any(String), | ||
}); | ||
}); |
4 changes: 4 additions & 0 deletions
4
dev-packages/e2e-tests/test-applications/nestjs-basic-with-graphql/tsconfig.build.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"extends": "./tsconfig.json", | ||
"exclude": ["node_modules", "test", "dist"] | ||
} |
Oops, something went wrong.