Skip to content

Commit 948583b

Browse files
timfishrobertcepa
andauthored
Add support for Cron check-ins (#210)
* Add support for Cron check-ins * Create heavy-hounds-love.md --------- Co-authored-by: Robert Cepa <[email protected]>
1 parent cf7e2db commit 948583b

File tree

6 files changed

+128
-28
lines changed

6 files changed

+128
-28
lines changed

.changeset/heavy-hounds-love.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"toucan-js": minor
3+
---
4+
5+
Add support for Cron check-ins

packages/toucan-js/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
"serverless"
3030
],
3131
"dependencies": {
32-
"@sentry/core": "7.65.0",
33-
"@sentry/utils": "7.65.0",
34-
"@sentry/types": "7.65.0",
35-
"@sentry/integrations": "7.65.0"
32+
"@sentry/core": "7.69.0",
33+
"@sentry/utils": "7.69.0",
34+
"@sentry/types": "7.69.0",
35+
"@sentry/integrations": "7.69.0"
3636
},
3737
"devDependencies": {
3838
"@rollup/plugin-commonjs": "25.0.3",

packages/toucan-js/src/client.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Scope } from '@sentry/core';
2-
import { BaseClient } from '@sentry/core';
2+
import { ServerRuntimeClient } from '@sentry/core';
33
import type { Event, EventHint, SeverityLevel } from '@sentry/types';
44
import { resolvedSyncPromise } from '@sentry/utils';
55
import { eventFromMessage, eventFromUnknownInput } from './eventBuilder';
@@ -11,7 +11,7 @@ import { setOnOptional } from './utils';
1111
/**
1212
* The Cloudflare Workers SDK Client.
1313
*/
14-
export class ToucanClient extends BaseClient<ToucanClientOptions> {
14+
export class ToucanClient extends ServerRuntimeClient<ToucanClientOptions> {
1515
/**
1616
* Some functions need to access the Hub (Toucan instance) this client is bound to,
1717
* but calling 'getCurrentHub()' is unsafe because it uses globals.

packages/toucan-js/src/sdk.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1-
import { getIntegrationsToSetup, Hub } from '@sentry/core';
1+
import { getIntegrationsToSetup, Hub, Scope } from '@sentry/core';
22
import { stackParserFromStackParserOptions } from '@sentry/utils';
33
import { ToucanClient } from './client';
44
import { LinkedErrors, RequestData } from './integrations';
55
import { defaultStackParser } from './stacktrace';
66
import { makeFetchTransport } from './transports';
77
import type { Options } from './types';
88
import { getSentryRelease } from './utils';
9+
import { CheckIn, MonitorConfig } from '@sentry/types';
910

1011
/**
1112
* The Cloudflare Workers SDK.
@@ -70,4 +71,24 @@ export class Toucan extends Hub {
7071
setEnabled(enabled: boolean): void {
7172
this.getClient<ToucanClient>()?.setEnabled(enabled);
7273
}
74+
75+
/**
76+
* Create a cron monitor check in and send it to Sentry.
77+
*
78+
* @param checkIn An object that describes a check in.
79+
* @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
80+
* to create a monitor automatically when sending a check in.
81+
*/
82+
captureCheckIn(
83+
checkIn: CheckIn,
84+
monitorConfig?: MonitorConfig,
85+
scope?: Scope,
86+
): string {
87+
if (checkIn.status === 'in_progress') {
88+
this.setContext('monitor', { slug: checkIn.monitorSlug });
89+
}
90+
91+
const client = this.getClient<ToucanClient>() as ToucanClient;
92+
return client.captureCheckIn(checkIn, monitorConfig, scope);
93+
}
7394
}

packages/toucan-js/test/index.spec.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -565,6 +565,80 @@ describe('Toucan', () => {
565565
});
566566
});
567567

568+
describe('captureCheckIn', () => {
569+
test(`is sent`, async () => {
570+
const toucan = new Toucan({
571+
dsn: VALID_DSN,
572+
context,
573+
});
574+
575+
const checkInId = toucan.captureCheckIn({
576+
monitorSlug: 'my_job',
577+
status: 'in_progress',
578+
});
579+
580+
toucan.captureCheckIn({
581+
checkInId: checkInId,
582+
monitorSlug: 'my_job',
583+
status: 'error',
584+
});
585+
586+
const waitUntilResults = await getMiniflareWaitUntil(context);
587+
588+
expect(waitUntilResults.length).toBe(2);
589+
expect(requests.length).toBe(2);
590+
591+
const requestBody = await requests[0].envelopePayload();
592+
593+
expect(requestBody).toEqual({
594+
check_in_id: checkInId,
595+
monitor_slug: 'my_job',
596+
status: 'in_progress',
597+
});
598+
599+
const requestBody2 = await requests[1].envelopePayload();
600+
601+
expect(requestBody2).toEqual({
602+
check_in_id: checkInId,
603+
monitor_slug: 'my_job',
604+
status: 'error',
605+
});
606+
607+
expect(requestBody.check_in_id).toBe(requestBody2.check_in_id);
608+
});
609+
610+
test(`is linked to errors`, async () => {
611+
const toucan = new Toucan({
612+
dsn: VALID_DSN,
613+
context,
614+
});
615+
616+
const checkInId = toucan.captureCheckIn({
617+
monitorSlug: 'my_job',
618+
status: 'in_progress',
619+
});
620+
621+
toucan.captureMessage('test');
622+
623+
const waitUntilResults = await getMiniflareWaitUntil(context);
624+
625+
expect(waitUntilResults.length).toBe(2);
626+
expect(requests.length).toBe(2);
627+
628+
const checkInRequestBody = await requests[0].envelopePayload();
629+
630+
expect(checkInRequestBody).toEqual({
631+
check_in_id: checkInId,
632+
monitor_slug: 'my_job',
633+
status: 'in_progress',
634+
});
635+
636+
const errorRequestBody = await requests[1].envelopePayload();
637+
638+
expect(errorRequestBody.contexts.monitor).toEqual({ slug: 'my_job' });
639+
});
640+
});
641+
568642
describe('addBreadcrumb', () => {
569643
test('captures last 100 breadcrumbs by default', async () => {
570644
const toucan = new Toucan({

yarn.lock

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1610,13 +1610,13 @@
16101610
"@sentry/utils" "7.23.0"
16111611
tslib "^1.9.3"
16121612

1613-
"@sentry/core@7.65.0":
1614-
version "7.65.0"
1615-
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-7.65.0.tgz#01c1320b4e7c62ccf757258c1622d07cc743468a"
1616-
integrity sha512-EwZABW8CtAbRGXV69FqeCqcNApA+Jbq308dko0W+MFdFe+9t2RGubUkpPxpJcbWy/dN2j4LiuENu1T7nWn0ZAQ==
1613+
"@sentry/core@7.69.0":
1614+
version "7.69.0"
1615+
resolved "https://registry.yarnpkg.com/@sentry/core/-/core-7.69.0.tgz#ebbe01df573f438f8613107020a4e18eb9adca4d"
1616+
integrity sha512-V6jvK2lS8bhqZDMFUtvwe2XvNstFQf5A+2LMKCNBOV/NN6eSAAd6THwEpginabjet9dHsNRmMk7WNKvrUfQhZw==
16171617
dependencies:
1618-
"@sentry/types" "7.65.0"
1619-
"@sentry/utils" "7.65.0"
1618+
"@sentry/types" "7.69.0"
1619+
"@sentry/utils" "7.69.0"
16201620
tslib "^2.4.1 || ^1.9.3"
16211621

16221622
"@sentry/esbuild-plugin@^0.2.3":
@@ -1626,13 +1626,13 @@
16261626
dependencies:
16271627
"@sentry/bundler-plugin-core" "0.2.3"
16281628

1629-
"@sentry/integrations@7.65.0":
1630-
version "7.65.0"
1631-
resolved "https://registry.yarnpkg.com/@sentry/integrations/-/integrations-7.65.0.tgz#aeae18b9faa7db3a1d52e74085eebe7e825aab72"
1632-
integrity sha512-9b54p0UrkWe9+RAWWTObJQ2k/uStqaUj7BkNFyuaxfKQ4IZViqc4Sa7d7zX2X1oynGNL3ic7iqcgVTh7NvNsAQ==
1629+
"@sentry/integrations@7.69.0":
1630+
version "7.69.0"
1631+
resolved "https://registry.yarnpkg.com/@sentry/integrations/-/integrations-7.69.0.tgz#04c0206d9436ec7b79971e3bde5d6e1e9194595f"
1632+
integrity sha512-FEFtFqXuCo9+L7bENZxFpEAlIODwHl6FyW/DwLfniy9jOXHU7BhP/oICLrFE5J7rh1gNY7N/8VlaiQr3hCnS/g==
16331633
dependencies:
1634-
"@sentry/types" "7.65.0"
1635-
"@sentry/utils" "7.65.0"
1634+
"@sentry/types" "7.69.0"
1635+
"@sentry/utils" "7.69.0"
16361636
localforage "^1.8.1"
16371637
tslib "^2.4.1 || ^1.9.3"
16381638

@@ -1671,10 +1671,10 @@
16711671
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-7.23.0.tgz#5d2ce94d81d7c1fad702645306f3c0932708cad5"
16721672
integrity sha512-fZ5XfVRswVZhKoCutQ27UpIHP16tvyc6ws+xq+njHv8Jg8gFBCoOxlJxuFhegD2xxylAn1aiSHNAErFWdajbpA==
16731673

1674-
"@sentry/types@7.65.0":
1675-
version "7.65.0"
1676-
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-7.65.0.tgz#f0f4e6583c631408d15ee5fb46901fd195fa1cc4"
1677-
integrity sha512-YYq7IDLLhpSBTmHoyWFtq/5ZDaEJ01r7xGuhB0aSIq33cm2I7im/B3ipzoOP/ukGZSIhuYVW9t531xZEO0+6og==
1674+
"@sentry/types@7.69.0":
1675+
version "7.69.0"
1676+
resolved "https://registry.yarnpkg.com/@sentry/types/-/types-7.69.0.tgz#012b8d90d270a473cc2a5cf58a56870542739292"
1677+
integrity sha512-zPyCox0mzitzU6SIa1KIbNoJAInYDdUpdiA+PoUmMn2hFMH1llGU/cS7f4w/mAsssTlbtlBi72RMnWUCy578bw==
16781678

16791679
16801680
version "7.23.0"
@@ -1684,12 +1684,12 @@
16841684
"@sentry/types" "7.23.0"
16851685
tslib "^1.9.3"
16861686

1687-
"@sentry/utils@7.65.0":
1688-
version "7.65.0"
1689-
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-7.65.0.tgz#a7929c5b019fa33e819b08a99744fa27cd38c85f"
1690-
integrity sha512-2JEBf4jzRSClhp+LJpX/E3QgHEeKvXqFMeNhmwQ07qqd6szhfH2ckYFj4gXk6YiGGY4Act3C6oxLfdZovG71bw==
1687+
"@sentry/utils@7.69.0":
1688+
version "7.69.0"
1689+
resolved "https://registry.yarnpkg.com/@sentry/utils/-/utils-7.69.0.tgz#b7594e4eb2a88b9b25298770b841dd3f81bd2aa4"
1690+
integrity sha512-4eBixe5Y+0EGVU95R4NxH3jkkjtkE4/CmSZD4In8SCkWGSauogePtq6hyiLsZuP1QHdpPb9Kt0+zYiBb2LouBA==
16911691
dependencies:
1692-
"@sentry/types" "7.65.0"
1692+
"@sentry/types" "7.69.0"
16931693
tslib "^2.4.1 || ^1.9.3"
16941694

16951695
"@sentry/vite-plugin@^0.2.3":

0 commit comments

Comments
 (0)