-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat(nestjs): Add SentryGlobalGraphQLFilter
#13545
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d03bcbf
Add graphql nestjs test application
nicohrubec 0b1c9e8
fix file formatting
nicohrubec db72c2b
Make work
nicohrubec 2ef3bc9
Improve documentation
nicohrubec bdc569c
finish tests
nicohrubec 18f9e70
Add new test app to e2e tests
nicohrubec cfaedc5
fix
nicohrubec 3e44f0e
Remove static
nicohrubec fabeb8b
Log
nicohrubec 491a95b
Do not report http exceptions
nicohrubec 171a246
.
nicohrubec File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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-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-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-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-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-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-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; |
30 changes: 30 additions & 0 deletions
30
dev-packages/e2e-tests/test-applications/nestjs-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,30 @@ | ||
import { ApolloDriver } from '@nestjs/apollo'; | ||
import { Logger, Module } from '@nestjs/common'; | ||
import { APP_FILTER } from '@nestjs/core'; | ||
import { GraphQLModule } from '@nestjs/graphql'; | ||
import { SentryGlobalGraphQLFilter, SentryModule } from '@sentry/nestjs/setup'; | ||
import { AppResolver } from './app.resolver'; | ||
|
||
@Module({ | ||
imports: [ | ||
SentryModule.forRoot(), | ||
GraphQLModule.forRoot({ | ||
driver: ApolloDriver, | ||
autoSchemaFile: true, | ||
playground: true, // sets up a playground on https://localhost:3000/graphql | ||
}), | ||
], | ||
controllers: [], | ||
providers: [ | ||
AppResolver, | ||
{ | ||
provide: APP_FILTER, | ||
useClass: SentryGlobalGraphQLFilter, | ||
}, | ||
{ | ||
provide: Logger, | ||
useClass: Logger, | ||
}, | ||
], | ||
}) | ||
export class AppModule {} |
14 changes: 14 additions & 0 deletions
14
dev-packages/e2e-tests/test-applications/nestjs-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!'); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
dev-packages/e2e-tests/test-applications/nestjs-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, | ||
}); |
15 changes: 15 additions & 0 deletions
15
dev-packages/e2e-tests/test-applications/nestjs-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,15 @@ | ||
// Import this first | ||
import './instrument'; | ||
|
||
// Import other modules | ||
import { NestFactory } from '@nestjs/core'; | ||
import { AppModule } from './app.module'; | ||
|
||
const PORT = 3030; | ||
|
||
async function bootstrap() { | ||
const app = await NestFactory.create(AppModule); | ||
await app.listen(PORT); | ||
} | ||
|
||
bootstrap(); |
6 changes: 6 additions & 0 deletions
6
dev-packages/e2e-tests/test-applications/nestjs-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-graphql', | ||
}); |
49 changes: 49 additions & 0 deletions
49
dev-packages/e2e-tests/test-applications/nestjs-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,49 @@ | ||
import { expect, test } from '@playwright/test'; | ||
import { waitForError } from '@sentry-internal/test-utils'; | ||
|
||
test('Sends exception to Sentry', async ({ baseURL }) => { | ||
const errorEventPromise = waitForError('nestjs-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-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"] | ||
} |
22 changes: 22 additions & 0 deletions
22
dev-packages/e2e-tests/test-applications/nestjs-graphql/tsconfig.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,22 @@ | ||
{ | ||
"compilerOptions": { | ||
"module": "commonjs", | ||
"declaration": true, | ||
"removeComments": true, | ||
"emitDecoratorMetadata": true, | ||
"experimentalDecorators": true, | ||
"allowSyntheticDefaultImports": true, | ||
"target": "ES2021", | ||
"sourceMap": true, | ||
"outDir": "./dist", | ||
"baseUrl": "./", | ||
"incremental": true, | ||
"skipLibCheck": true, | ||
"strictNullChecks": false, | ||
"noImplicitAny": false, | ||
"strictBindCallApply": false, | ||
"forceConsistentCasingInFileNames": false, | ||
"noFallthroughCasesInSwitch": false, | ||
"moduleResolution": "Node16" | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
m: I am a bit sus because we identified previously for HTTP contexts taht throwing in error filters will not make the behaviour transparent. Is this the case for graphql?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to be fine, the response from the test error endpoint looks like this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Did you check what the server returns before/after you added the filter? We need to be careful not to modify any responses.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, responses are the same with/without filter. I also now added the logging logic from the
ExtenalExceptionFilter
so now output also aligns perfectly.