Skip to content

Commit

Permalink
Add support for Cron check-ins (#210)
Browse files Browse the repository at this point in the history
* Add support for Cron check-ins

* Create heavy-hounds-love.md

---------

Co-authored-by: Robert Cepa <robertcepa@icloud.com>
  • Loading branch information
timfish and robertcepa committed Sep 23, 2023
1 parent cf7e2db commit 948583b
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 28 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-hounds-love.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"toucan-js": minor
---

Add support for Cron check-ins
8 changes: 4 additions & 4 deletions packages/toucan-js/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@
"serverless"
],
"dependencies": {
"@sentry/core": "7.65.0",
"@sentry/utils": "7.65.0",
"@sentry/types": "7.65.0",
"@sentry/integrations": "7.65.0"
"@sentry/core": "7.69.0",
"@sentry/utils": "7.69.0",
"@sentry/types": "7.69.0",
"@sentry/integrations": "7.69.0"
},
"devDependencies": {
"@rollup/plugin-commonjs": "25.0.3",
Expand Down
4 changes: 2 additions & 2 deletions packages/toucan-js/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Scope } from '@sentry/core';
import { BaseClient } from '@sentry/core';
import { ServerRuntimeClient } from '@sentry/core';
import type { Event, EventHint, SeverityLevel } from '@sentry/types';
import { resolvedSyncPromise } from '@sentry/utils';
import { eventFromMessage, eventFromUnknownInput } from './eventBuilder';
Expand All @@ -11,7 +11,7 @@ import { setOnOptional } from './utils';
/**
* The Cloudflare Workers SDK Client.
*/
export class ToucanClient extends BaseClient<ToucanClientOptions> {
export class ToucanClient extends ServerRuntimeClient<ToucanClientOptions> {
/**
* Some functions need to access the Hub (Toucan instance) this client is bound to,
* but calling 'getCurrentHub()' is unsafe because it uses globals.
Expand Down
23 changes: 22 additions & 1 deletion packages/toucan-js/src/sdk.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { getIntegrationsToSetup, Hub } from '@sentry/core';
import { getIntegrationsToSetup, Hub, Scope } from '@sentry/core';
import { stackParserFromStackParserOptions } from '@sentry/utils';
import { ToucanClient } from './client';
import { LinkedErrors, RequestData } from './integrations';
import { defaultStackParser } from './stacktrace';
import { makeFetchTransport } from './transports';
import type { Options } from './types';
import { getSentryRelease } from './utils';
import { CheckIn, MonitorConfig } from '@sentry/types';

/**
* The Cloudflare Workers SDK.
Expand Down Expand Up @@ -70,4 +71,24 @@ export class Toucan extends Hub {
setEnabled(enabled: boolean): void {
this.getClient<ToucanClient>()?.setEnabled(enabled);
}

/**
* Create a cron monitor check in and send it to Sentry.
*
* @param checkIn An object that describes a check in.
* @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
* to create a monitor automatically when sending a check in.
*/
captureCheckIn(
checkIn: CheckIn,
monitorConfig?: MonitorConfig,
scope?: Scope,
): string {
if (checkIn.status === 'in_progress') {
this.setContext('monitor', { slug: checkIn.monitorSlug });
}

const client = this.getClient<ToucanClient>() as ToucanClient;
return client.captureCheckIn(checkIn, monitorConfig, scope);
}
}
74 changes: 74 additions & 0 deletions packages/toucan-js/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,80 @@ describe('Toucan', () => {
});
});

describe('captureCheckIn', () => {
test(`is sent`, async () => {
const toucan = new Toucan({
dsn: VALID_DSN,
context,
});

const checkInId = toucan.captureCheckIn({
monitorSlug: 'my_job',
status: 'in_progress',
});

toucan.captureCheckIn({
checkInId: checkInId,
monitorSlug: 'my_job',
status: 'error',
});

const waitUntilResults = await getMiniflareWaitUntil(context);

expect(waitUntilResults.length).toBe(2);
expect(requests.length).toBe(2);

const requestBody = await requests[0].envelopePayload();

expect(requestBody).toEqual({
check_in_id: checkInId,
monitor_slug: 'my_job',
status: 'in_progress',
});

const requestBody2 = await requests[1].envelopePayload();

expect(requestBody2).toEqual({
check_in_id: checkInId,
monitor_slug: 'my_job',
status: 'error',
});

expect(requestBody.check_in_id).toBe(requestBody2.check_in_id);
});

test(`is linked to errors`, async () => {
const toucan = new Toucan({
dsn: VALID_DSN,
context,
});

const checkInId = toucan.captureCheckIn({
monitorSlug: 'my_job',
status: 'in_progress',
});

toucan.captureMessage('test');

const waitUntilResults = await getMiniflareWaitUntil(context);

expect(waitUntilResults.length).toBe(2);
expect(requests.length).toBe(2);

const checkInRequestBody = await requests[0].envelopePayload();

expect(checkInRequestBody).toEqual({
check_in_id: checkInId,
monitor_slug: 'my_job',
status: 'in_progress',
});

const errorRequestBody = await requests[1].envelopePayload();

expect(errorRequestBody.contexts.monitor).toEqual({ slug: 'my_job' });
});
});

describe('addBreadcrumb', () => {
test('captures last 100 breadcrumbs by default', async () => {
const toucan = new Toucan({
Expand Down
42 changes: 21 additions & 21 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1610,13 +1610,13 @@
"@sentry/utils" "7.23.0"
tslib "^1.9.3"

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

"@sentry/esbuild-plugin@^0.2.3":
Expand All @@ -1626,13 +1626,13 @@
dependencies:
"@sentry/bundler-plugin-core" "0.2.3"

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

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

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

"@sentry/utils@7.23.0":
version "7.23.0"
Expand All @@ -1684,12 +1684,12 @@
"@sentry/types" "7.23.0"
tslib "^1.9.3"

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

"@sentry/vite-plugin@^0.2.3":
Expand Down

0 comments on commit 948583b

Please sign in to comment.