Skip to content

Commit

Permalink
feat(crons): Add interface for heartbeat checkin (#9706)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst committed Nov 30, 2023
1 parent b7fdb7d commit 0830866
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/server-runtime-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ export class ServerRuntimeClient<
* to create a monitor automatically when sending a check in.
*/
public captureCheckIn(checkIn: CheckIn, monitorConfig?: MonitorConfig, scope?: Scope): string {
const id = checkIn.status !== 'in_progress' && checkIn.checkInId ? checkIn.checkInId : uuid4();
const id = 'checkInId' in checkIn && checkIn.checkInId ? checkIn.checkInId : uuid4();
if (!this._isEnabled()) {
DEBUG_BUILD && logger.warn('SDK not enabled, will not capture checkin.');
return id;
Expand All @@ -164,7 +164,7 @@ export class ServerRuntimeClient<
environment,
};

if (checkIn.status !== 'in_progress') {
if ('duration' in checkIn) {
serializedCheckIn.duration = checkIn.duration;
}

Expand Down
32 changes: 32 additions & 0 deletions packages/node/test/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,38 @@ describe('NodeClient', () => {
]);
});

it('sends a checkIn envelope for heartbeat checkIns', () => {
const options = getDefaultNodeClientOptions({
dsn: PUBLIC_DSN,
serverName: 'server',
release: '1.0.0',
environment: 'dev',
});
client = new NodeClient(options);

// @ts-expect-error accessing private method
const sendEnvelopeSpy = jest.spyOn(client, '_sendEnvelope');

const id = client.captureCheckIn({ monitorSlug: 'heartbeat-monitor', status: 'ok' });

expect(sendEnvelopeSpy).toHaveBeenCalledTimes(1);
expect(sendEnvelopeSpy).toHaveBeenCalledWith([
expect.any(Object),
[
[
expect.any(Object),
{
check_in_id: id,
monitor_slug: 'heartbeat-monitor',
status: 'ok',
release: '1.0.0',
environment: 'dev',
},
],
],
]);
});

it('does not send a checkIn envelope if disabled', () => {
const options = getDefaultNodeClientOptions({ dsn: PUBLIC_DSN, serverName: 'bar', enabled: false });
client = new NodeClient(options);
Expand Down
9 changes: 8 additions & 1 deletion packages/types/src/checkin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ export interface SerializedCheckIn {
};
}

export interface HeartbeatCheckIn {
// The distinct slug of the monitor.
monitorSlug: SerializedCheckIn['monitor_slug'];
// The status of the check-in.
status: 'ok' | 'error';
}

export interface InProgressCheckIn {
// The distinct slug of the monitor.
monitorSlug: SerializedCheckIn['monitor_slug'];
Expand All @@ -61,7 +68,7 @@ export interface FinishedCheckIn {
duration?: SerializedCheckIn['duration'];
}

export type CheckIn = InProgressCheckIn | FinishedCheckIn;
export type CheckIn = HeartbeatCheckIn | InProgressCheckIn | FinishedCheckIn;

type SerializedMonitorConfig = NonNullable<SerializedCheckIn['monitor_config']>;

Expand Down

0 comments on commit 0830866

Please sign in to comment.