-
-
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 nest cron monitoring support (#12781)
- Loading branch information
1 parent
9b5cf26
commit 580e6a4
Showing
7 changed files
with
94 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
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
3 changes: 2 additions & 1 deletion
3
dev-packages/e2e-tests/test-applications/nestjs/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
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
34 changes: 34 additions & 0 deletions
34
dev-packages/e2e-tests/test-applications/nestjs/tests/cron-decorator.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,34 @@ | ||
import { expect, test } from '@playwright/test'; | ||
import { waitForEnvelopeItem } from '@sentry-internal/test-utils'; | ||
|
||
test('Cron job triggers send of in_progress envelope', async ({ baseURL }) => { | ||
const inProgressEnvelopePromise = waitForEnvelopeItem('nestjs', envelope => { | ||
return envelope[0].type === 'check_in'; | ||
}); | ||
|
||
const inProgressEnvelope = await inProgressEnvelopePromise; | ||
|
||
expect(inProgressEnvelope[1]).toEqual( | ||
expect.objectContaining({ | ||
check_in_id: expect.any(String), | ||
monitor_slug: 'test-cron-slug', | ||
status: 'in_progress', | ||
environment: 'qa', | ||
monitor_config: { | ||
schedule: { | ||
type: 'crontab', | ||
value: '* * * * *', | ||
}, | ||
}, | ||
contexts: { | ||
trace: { | ||
span_id: expect.any(String), | ||
trace_id: expect.any(String), | ||
}, | ||
}, | ||
}), | ||
); | ||
|
||
// kill cron so tests don't get stuck | ||
await fetch(`${baseURL}/kill-test-cron`); | ||
}); |
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,24 @@ | ||
import * as Sentry from '@sentry/node'; | ||
import type { MonitorConfig } from '@sentry/types'; | ||
|
||
/** | ||
* A decorator wrapping the native nest Cron decorator, sending check-ins to Sentry. | ||
*/ | ||
export const SentryCron = (monitorSlug: string, monitorConfig?: MonitorConfig): MethodDecorator => { | ||
return (target: unknown, propertyKey, descriptor: PropertyDescriptor) => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const originalMethod = descriptor.value as (...args: any[]) => Promise<any>; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
descriptor.value = function (...args: any[]) { | ||
return Sentry.withMonitor( | ||
monitorSlug, | ||
() => { | ||
return originalMethod.apply(this, args); | ||
}, | ||
monitorConfig, | ||
); | ||
}; | ||
return descriptor; | ||
}; | ||
}; |
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