Skip to content

Commit a2cda4d

Browse files
AbhiPrasadLms24
andauthored
feat(node): Add checkin envelope types (#7777)
Co-authored-by: Lukas Stracke <[email protected]>
1 parent ad8ce23 commit a2cda4d

File tree

7 files changed

+123
-4
lines changed

7 files changed

+123
-4
lines changed

packages/node/src/checkin.ts

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import type { CheckIn, CheckInEvelope, CheckInItem, DsnComponents, SdkMetadata } from '@sentry/types';
2+
import { createEnvelope, dsnToString } from '@sentry/utils';
3+
4+
/**
5+
* Create envelope from check in item.
6+
*/
7+
export function createCheckInEnvelope(
8+
checkIn: CheckIn,
9+
metadata?: SdkMetadata,
10+
tunnel?: string,
11+
dsn?: DsnComponents,
12+
): CheckInEvelope {
13+
const headers: CheckInEvelope[0] = {
14+
sent_at: new Date().toISOString(),
15+
...(metadata &&
16+
metadata.sdk && {
17+
sdk: {
18+
name: metadata.sdk.name,
19+
version: metadata.sdk.version,
20+
},
21+
}),
22+
...(!!tunnel && !!dsn && { dsn: dsnToString(dsn) }),
23+
};
24+
const item = createCheckInEnvelopeItem(checkIn);
25+
return createEnvelope<CheckInEvelope>(headers, [item]);
26+
}
27+
28+
function createCheckInEnvelopeItem(checkIn: CheckIn): CheckInItem {
29+
const checkInHeaders: CheckInItem[0] = {
30+
type: 'check_in',
31+
};
32+
return [checkInHeaders, checkIn];
33+
}

packages/node/test/checkin.test.ts

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import { createCheckInEnvelope } from '../src/checkin';
2+
3+
describe('userFeedback', () => {
4+
test('creates user feedback envelope header', () => {
5+
const envelope = createCheckInEnvelope(
6+
{
7+
check_in_id: '83a7c03ed0a04e1b97e2e3b18d38f244',
8+
monitor_slug: 'b7645b8e-b47d-4398-be9a-d16b0dac31cb',
9+
status: 'in_progress',
10+
},
11+
{
12+
sdk: {
13+
name: 'testSdkName',
14+
version: 'testSdkVersion',
15+
},
16+
},
17+
'testTunnel',
18+
{
19+
host: 'testHost',
20+
projectId: 'testProjectId',
21+
protocol: 'http',
22+
},
23+
);
24+
25+
expect(envelope[0]).toEqual({
26+
dsn: 'http://undefined@testHost/undefinedtestProjectId',
27+
sdk: {
28+
name: 'testSdkName',
29+
version: 'testSdkVersion',
30+
},
31+
sent_at: expect.any(String),
32+
});
33+
});
34+
35+
test('creates user feedback envelope item', () => {
36+
const envelope = createCheckInEnvelope({
37+
check_in_id: '83a7c03ed0a04e1b97e2e3b18d38f244',
38+
monitor_slug: 'b7645b8e-b47d-4398-be9a-d16b0dac31cb',
39+
status: 'ok',
40+
duration: 10.0,
41+
release: '1.0.0',
42+
environment: 'production',
43+
});
44+
45+
expect(envelope[1]).toEqual([
46+
[
47+
{
48+
type: 'check_in',
49+
},
50+
{
51+
check_in_id: '83a7c03ed0a04e1b97e2e3b18d38f244',
52+
monitor_slug: 'b7645b8e-b47d-4398-be9a-d16b0dac31cb',
53+
status: 'ok',
54+
duration: 10.0,
55+
release: '1.0.0',
56+
environment: 'production',
57+
},
58+
],
59+
]);
60+
});
61+
});

packages/types/src/checkin.ts

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// https://develop.sentry.dev/sdk/check-ins/
2+
export interface CheckIn {
3+
// Check-In ID (unique and client generated).
4+
check_in_id: string;
5+
// The distinct slug of the monitor.
6+
monitor_slug: string;
7+
// The status of the check-in.
8+
status: 'in_progress' | 'ok' | 'error';
9+
// The duration of the check-in in seconds. Will only take effect if the status is ok or error.
10+
duration?: number;
11+
release?: string;
12+
environment?: string;
13+
}

packages/types/src/datacategory.ts

+4-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This type is used in various places like Client Reports and Rate Limit Categories
22
// See:
33
// - https://develop.sentry.dev/sdk/rate-limiting/#definitions
4-
// - https://github.com/getsentry/relay/blob/10874b587bb676bd6d50ad42d507216513660082/relay-common/src/constants.rs#L97-L113
4+
// - https://github.com/getsentry/relay/blob/c3b339e151c1e548ede489a01c65db82472c8751/relay-common/src/constants.rs#L139-L152
55
// - https://develop.sentry.dev/sdk/client-reports/#envelope-item-payload under `discarded_events`
66
export type DataCategory =
77
// Reserved and only used in edgecases, unlikely to be ever actually used
@@ -21,4 +21,6 @@ export type DataCategory =
2121
// SDK internal event, like client_reports
2222
| 'internal'
2323
// Profile event type
24-
| 'profile';
24+
| 'profile'
25+
// Check-in event (monitor)
26+
| 'monitor';

packages/types/src/envelope.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { CheckIn } from './checkin';
12
import type { ClientReport } from './clientreport';
23
import type { DsnComponents } from './dsn';
34
import type { Event } from './event';
@@ -31,7 +32,8 @@ export type EnvelopeItemType =
3132
| 'event'
3233
| 'profile'
3334
| 'replay_event'
34-
| 'replay_recording';
35+
| 'replay_recording'
36+
| 'check_in';
3537

3638
export type BaseEnvelopeHeaders = {
3739
[key: string]: unknown;
@@ -68,6 +70,7 @@ type SessionAggregatesItemHeaders = { type: 'sessions' };
6870
type ClientReportItemHeaders = { type: 'client_report' };
6971
type ReplayEventItemHeaders = { type: 'replay_event' };
7072
type ReplayRecordingItemHeaders = { type: 'replay_recording'; length: number };
73+
type CheckInItemHeaders = { type: 'check_in' };
7174

7275
export type EventItem = BaseEnvelopeItem<EventItemHeaders, Event>;
7376
export type AttachmentItem = BaseEnvelopeItem<AttachmentItemHeaders, string | Uint8Array>;
@@ -76,18 +79,21 @@ export type SessionItem =
7679
| BaseEnvelopeItem<SessionItemHeaders, Session>
7780
| BaseEnvelopeItem<SessionAggregatesItemHeaders, SessionAggregates>;
7881
export type ClientReportItem = BaseEnvelopeItem<ClientReportItemHeaders, ClientReport>;
82+
export type CheckInItem = BaseEnvelopeItem<CheckInItemHeaders, CheckIn>;
7983
type ReplayEventItem = BaseEnvelopeItem<ReplayEventItemHeaders, ReplayEvent>;
8084
type ReplayRecordingItem = BaseEnvelopeItem<ReplayRecordingItemHeaders, ReplayRecordingData>;
8185

8286
export type EventEnvelopeHeaders = { event_id: string; sent_at: string; trace?: DynamicSamplingContext };
8387
type SessionEnvelopeHeaders = { sent_at: string };
88+
type CheckInEnvelopeHeaders = BaseEnvelopeHeaders;
8489
type ClientReportEnvelopeHeaders = BaseEnvelopeHeaders;
8590
type ReplayEnvelopeHeaders = BaseEnvelopeHeaders;
8691

8792
export type EventEnvelope = BaseEnvelope<EventEnvelopeHeaders, EventItem | AttachmentItem | UserFeedbackItem>;
8893
export type SessionEnvelope = BaseEnvelope<SessionEnvelopeHeaders, SessionItem>;
8994
export type ClientReportEnvelope = BaseEnvelope<ClientReportEnvelopeHeaders, ClientReportItem>;
9095
export type ReplayEnvelope = [ReplayEnvelopeHeaders, [ReplayEventItem, ReplayRecordingItem]];
96+
export type CheckInEvelope = BaseEnvelope<CheckInEnvelopeHeaders, CheckInItem>;
9197

92-
export type Envelope = EventEnvelope | SessionEnvelope | ClientReportEnvelope | ReplayEnvelope;
98+
export type Envelope = EventEnvelope | SessionEnvelope | ClientReportEnvelope | ReplayEnvelope | CheckInEvelope;
9399
export type EnvelopeItem = Envelope[1][number];

packages/types/src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ export type {
3030
SessionEnvelope,
3131
SessionItem,
3232
UserFeedbackItem,
33+
CheckInItem,
34+
CheckInEvelope,
3335
} from './envelope';
3436
export type { ExtendedError } from './error';
3537
export type { Event, EventHint, EventType, ErrorEvent, TransactionEvent } from './event';
@@ -104,3 +106,4 @@ export type { Instrumenter } from './instrumenter';
104106
export type { HandlerDataFetch, HandlerDataXhr, SentryXhrData, SentryWrappedXMLHttpRequest } from './instrument';
105107

106108
export type { BrowserClientReplayOptions } from './browseroptions';
109+
export type { CheckIn } from './checkin';

packages/utils/src/envelope.ts

+1
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ const ITEM_TYPE_TO_DATA_CATEGORY_MAP: Record<EnvelopeItemType, DataCategory> = {
207207
profile: 'profile',
208208
replay_event: 'replay',
209209
replay_recording: 'replay',
210+
check_in: 'monitor',
210211
};
211212

212213
/**

0 commit comments

Comments
 (0)