Skip to content

Commit 5a9947d

Browse files
committed
fix(node): Make sure we use same ID for checkIns
1 parent 2e6147a commit 5a9947d

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

packages/node/src/client.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,9 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
153153
* @param checkIn An object that describes a check in.
154154
* @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
155155
* to create a monitor automatically when sending a check in.
156+
* @returns A string representing the id of the check in.
156157
*/
157-
public captureCheckIn(checkIn: CheckIn, monitorConfig?: MonitorConfig): void {
158+
public captureCheckIn(checkIn: CheckIn, monitorConfig?: MonitorConfig): string | undefined {
158159
if (!this._isEnabled()) {
159160
__DEBUG_BUILD__ && logger.warn('SDK not enabled, will not capture checkin.');
160161
return;
@@ -163,8 +164,10 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
163164
const options = this.getOptions();
164165
const { release, environment, tunnel } = options;
165166

167+
const id = checkIn.checkInId || uuid4();
168+
166169
const serializedCheckIn: SerializedCheckIn = {
167-
check_in_id: uuid4(),
170+
check_in_id: id,
168171
monitor_slug: checkIn.monitorSlug,
169172
status: checkIn.status,
170173
duration: checkIn.duration,
@@ -183,6 +186,7 @@ export class NodeClient extends BaseClient<NodeClientOptions> {
183186

184187
const envelope = createCheckInEnvelope(serializedCheckIn, this.getSdkMetadata(), tunnel, this.getDsn());
185188
void this._sendEnvelope(envelope);
189+
return id;
186190
}
187191

188192
/**

packages/node/src/sdk.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ export function captureCheckIn(
279279
}
280280

281281
__DEBUG_BUILD__ && logger.warn('Cannot capture check in. No client defined.');
282+
return;
282283
}
283284

284285
/** Node.js stack parser */

packages/node/test/client.test.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,8 @@ describe('NodeClient', () => {
294294
// @ts-ignore accessing private method
295295
const sendEnvelopeSpy = jest.spyOn(client, '_sendEnvelope');
296296

297-
client.captureCheckIn(
298-
{ monitorSlug: 'foo', status: 'ok', duration: 1222 },
297+
const id = client.captureCheckIn(
298+
{ monitorSlug: 'foo', status: 'in_progress' },
299299
{
300300
schedule: {
301301
type: 'crontab',
@@ -315,9 +315,8 @@ describe('NodeClient', () => {
315315
expect.any(Object),
316316
{
317317
check_in_id: expect.any(String),
318-
duration: 1222,
319318
monitor_slug: 'foo',
320-
status: 'ok',
319+
status: 'in_progress',
321320
release: '1.0.0',
322321
environment: 'dev',
323322
monitor_config: {
@@ -333,6 +332,26 @@ describe('NodeClient', () => {
333332
],
334333
],
335334
]);
335+
336+
client.captureCheckIn({ monitorSlug: 'foo', status: 'ok', duration: 1222, checkInId: id });
337+
338+
expect(sendEnvelopeSpy).toHaveBeenCalledTimes(2);
339+
expect(sendEnvelopeSpy).toHaveBeenCalledWith([
340+
expect.any(Object),
341+
[
342+
[
343+
expect.any(Object),
344+
{
345+
check_in_id: id,
346+
monitor_slug: 'foo',
347+
duration: 1222,
348+
status: 'ok',
349+
release: '1.0.0',
350+
environment: 'dev',
351+
},
352+
],
353+
],
354+
]);
336355
});
337356

338357
it('does not send a checkIn envelope if disabled', () => {

packages/types/src/checkin.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,16 @@ export interface SerializedCheckIn {
3838
};
3939
}
4040

41-
export interface CheckIn {
41+
export type CheckIn = {
4242
// The distinct slug of the monitor.
4343
monitorSlug: SerializedCheckIn['monitor_slug'];
44+
// Check-In ID (unique and client generated).
45+
checkInId?: SerializedCheckIn['check_in_id'];
4446
// The status of the check-in.
4547
status: SerializedCheckIn['status'];
4648
// The duration of the check-in in seconds. Will only take effect if the status is ok or error.
4749
duration?: SerializedCheckIn['duration'];
48-
}
50+
};
4951

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

0 commit comments

Comments
 (0)