-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
feat(nestjs): Instrument @nestjs/schedule decorators
#19735
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
573bfe8
feat(nestjs): Instrument @nestjs/schedule
nicohrubec 0908a8d
feat(nestjs): Instrument @nestjs/schedule
nicohrubec b33344e
updates
nicohrubec 6c8438b
cleanup
nicohrubec da1d4a7
Do not capture exceptions from sentrycron
nicohrubec d57b9c4
remove useless unit tests and add changelog
nicohrubec 7d4a5bd
.
nicohrubec c558682
.
nicohrubec 21ab9a7
fix tests
nicohrubec 15bacc9
update mechanism type and changelog
nicohrubec 6fe21d3
update
nicohrubec 0d33b0f
try to fix lint and support v23
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
45 changes: 45 additions & 0 deletions
45
dev-packages/e2e-tests/test-applications/nestjs-basic/src/schedule.service.ts
This file contains hidden or 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,45 @@ | ||
| import { Injectable } from '@nestjs/common'; | ||
| import { Cron, Interval, SchedulerRegistry, Timeout } from '@nestjs/schedule'; | ||
| import * as Sentry from '@sentry/nestjs'; | ||
|
|
||
| @Injectable() | ||
| export class ScheduleService { | ||
| constructor(private schedulerRegistry: SchedulerRegistry) {} | ||
|
|
||
| // @Cron error test (auto-instrumentation, no @SentryCron) | ||
| @Cron('*/5 * * * * *', { name: 'test-schedule-cron-error' }) | ||
| handleCronError() { | ||
| throw new Error('Test error from schedule cron'); | ||
| } | ||
|
|
||
| // @Interval error test | ||
| @Interval('test-schedule-interval-error', 2000) | ||
| async handleIntervalError() { | ||
| throw new Error('Test error from schedule interval'); | ||
| } | ||
|
|
||
| // @Timeout error test | ||
| // Use a very long delay so this doesn't fire on its own during tests. | ||
| // The test triggers the method via an HTTP endpoint instead. | ||
| @Timeout('test-schedule-timeout-error', 60000) | ||
| async handleTimeoutError() { | ||
| throw new Error('Test error from schedule timeout'); | ||
| } | ||
|
|
||
| // Isolation scope test: adds breadcrumb that should NOT leak to HTTP requests | ||
| @Interval('test-schedule-isolation', 2000) | ||
| handleIsolationBreadcrumb() { | ||
| Sentry.addBreadcrumb({ | ||
| message: 'leaked-breadcrumb-from-schedule', | ||
| level: 'info', | ||
| }); | ||
| } | ||
|
|
||
| killCron(name: string) { | ||
| this.schedulerRegistry.deleteCronJob(name); | ||
| } | ||
|
|
||
| killInterval(name: string) { | ||
| this.schedulerRegistry.deleteInterval(name); | ||
| } | ||
| } |
This file contains hidden or 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
93 changes: 93 additions & 0 deletions
93
dev-packages/e2e-tests/test-applications/nestjs-basic/tests/schedule-instrumentation.test.ts
This file contains hidden or 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,93 @@ | ||
| import { expect, test } from '@playwright/test'; | ||
| import { waitForError, waitForTransaction } from '@sentry-internal/test-utils'; | ||
|
|
||
| test('Sends exceptions to Sentry on error in @Cron decorated method', async ({ baseURL }) => { | ||
| const errorEventPromise = waitForError('nestjs-basic', event => { | ||
| return ( | ||
| !event.type && | ||
| event.exception?.values?.[0]?.value === 'Test error from schedule cron' && | ||
| event.exception?.values?.[0]?.mechanism?.type === 'auto.function.nestjs.cron' | ||
| ); | ||
| }); | ||
|
|
||
| const errorEvent = await errorEventPromise; | ||
|
|
||
| expect(errorEvent.exception?.values).toHaveLength(1); | ||
| expect(errorEvent.exception?.values?.[0]?.mechanism).toEqual({ | ||
| handled: false, | ||
| type: 'auto.function.nestjs.cron', | ||
| }); | ||
|
|
||
| // kill cron so tests don't get stuck | ||
| await fetch(`${baseURL}/kill-test-schedule-cron/test-schedule-cron-error`); | ||
| }); | ||
|
|
||
| test('Sends exceptions to Sentry on error in @Interval decorated method', async ({ baseURL }) => { | ||
| const errorEventPromise = waitForError('nestjs-basic', event => { | ||
| return ( | ||
| !event.type && | ||
| event.exception?.values?.[0]?.value === 'Test error from schedule interval' && | ||
| event.exception?.values?.[0]?.mechanism?.type === 'auto.function.nestjs.interval' | ||
| ); | ||
| }); | ||
|
|
||
| const errorEvent = await errorEventPromise; | ||
|
|
||
| expect(errorEvent.exception?.values).toHaveLength(1); | ||
| expect(errorEvent.exception?.values?.[0]?.mechanism).toEqual({ | ||
| handled: false, | ||
| type: 'auto.function.nestjs.interval', | ||
| }); | ||
|
|
||
| // kill interval so tests don't get stuck | ||
| await fetch(`${baseURL}/kill-test-schedule-interval/test-schedule-interval-error`); | ||
| }); | ||
|
|
||
| test('Sends exceptions to Sentry on error in @Timeout decorated method', async ({ baseURL }) => { | ||
| const errorEventPromise = waitForError('nestjs-basic', event => { | ||
| return ( | ||
| !event.type && | ||
| event.exception?.values?.[0]?.value === 'Test error from schedule timeout' && | ||
| event.exception?.values?.[0]?.mechanism?.type === 'auto.function.nestjs.timeout' | ||
| ); | ||
| }); | ||
|
|
||
| // Trigger the @Timeout-decorated method via HTTP endpoint since @Timeout | ||
| // fires once and timing is unreliable across test runs. | ||
| await fetch(`${baseURL}/trigger-schedule-timeout-error`).catch(() => { | ||
| // Expected to fail since the handler throws | ||
| }); | ||
|
|
||
| const errorEvent = await errorEventPromise; | ||
|
|
||
| expect(errorEvent.exception?.values).toHaveLength(1); | ||
| expect(errorEvent.exception?.values?.[0]?.mechanism).toEqual({ | ||
| handled: false, | ||
| type: 'auto.function.nestjs.timeout', | ||
| }); | ||
| }); | ||
|
|
||
| test('Scheduled task breadcrumbs do not leak into subsequent HTTP requests', async ({ baseURL }) => { | ||
| // The app runs @Interval('test-schedule-isolation', 2000) which adds a breadcrumb. | ||
| // Without isolation scope forking, this breadcrumb leaks into the default isolation scope | ||
| // and gets cloned into subsequent HTTP requests. | ||
|
|
||
| // Wait for at least one interval tick to fire | ||
| await new Promise(resolve => setTimeout(resolve, 3000)); | ||
|
|
||
| const transactionPromise = waitForTransaction('nestjs-basic', transactionEvent => { | ||
| return transactionEvent.transaction === 'GET /test-schedule-isolation'; | ||
| }); | ||
|
|
||
| await fetch(`${baseURL}/test-schedule-isolation`); | ||
|
|
||
| const transaction = await transactionPromise; | ||
|
|
||
| const leakedBreadcrumb = (transaction.breadcrumbs || []).find( | ||
| (b: any) => b.message === 'leaked-breadcrumb-from-schedule', | ||
| ); | ||
| expect(leakedBreadcrumb).toBeUndefined(); | ||
|
|
||
| // kill interval so tests don't get stuck | ||
| await fetch(`${baseURL}/kill-test-schedule-interval/test-schedule-isolation`); | ||
| }); |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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.
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.
Uh oh!
There was an error while loading. Please reload this page.