Skip to content

Commit 6462e0d

Browse files
authored
ref: Hoist createCheckinEnvelope to core package (#8082)
1 parent 0e36397 commit 6462e0d

File tree

10 files changed

+66
-86
lines changed

10 files changed

+66
-86
lines changed
File renamed without changes.

packages/core/src/exports.ts

+26
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import type {
22
Breadcrumb,
33
CaptureContext,
4+
CheckIn,
45
CustomSamplingContext,
56
Event,
67
EventHint,
78
Extra,
89
Extras,
10+
MonitorConfig,
911
Primitive,
1012
Severity,
1113
SeverityLevel,
1214
TransactionContext,
1315
User,
1416
} from '@sentry/types';
17+
import { logger, uuid4 } from '@sentry/utils';
1518

1619
import type { Hub } from './hub';
1720
import { getCurrentHub } from './hub';
@@ -184,3 +187,26 @@ export function startTransaction(
184187
): ReturnType<Hub['startTransaction']> {
185188
return getCurrentHub().startTransaction({ ...context }, customSamplingContext);
186189
}
190+
191+
/**
192+
* Create a cron monitor check in and send it to Sentry.
193+
*
194+
* @param checkIn An object that describes a check in.
195+
* @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
196+
* to create a monitor automatically when sending a check in.
197+
*/
198+
export function captureCheckIn(checkIn: CheckIn, upsertMonitorConfig?: MonitorConfig): string {
199+
const capturedCheckIn =
200+
checkIn.status !== 'in_progress' && checkIn.checkInId ? checkIn : { ...checkIn, checkInId: uuid4() };
201+
202+
const client = getCurrentHub().getClient();
203+
if (!client) {
204+
__DEBUG_BUILD__ && logger.warn('Cannot capture check-in. No client defined.');
205+
} else if (!client.captureCheckIn) {
206+
__DEBUG_BUILD__ && logger.warn('Cannot capture check-in. Client does not support sending check-ins.');
207+
} else {
208+
client.captureCheckIn(capturedCheckIn, upsertMonitorConfig);
209+
}
210+
211+
return capturedCheckIn.checkInId;
212+
}

packages/core/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ export {
1717
setTags,
1818
setUser,
1919
withScope,
20+
captureCheckIn,
2021
} from './exports';
2122
export {
2223
getCurrentHub,
@@ -42,6 +43,7 @@ export { SDK_VERSION } from './version';
4243
export { getIntegrationsToSetup } from './integration';
4344
export { FunctionToString, InboundFilters } from './integrations';
4445
export { prepareEvent } from './utils/prepareEvent';
46+
export { createCheckInEnvelope } from './checkin';
4547
export { hasTracingEnabled } from './utils/hasTracingEnabled';
4648
export { DEFAULT_ENVIRONMENT } from './constants';
4749

packages/node/test/checkin.test.ts renamed to packages/core/test/lib/checkin.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import type { SerializedCheckIn } from '@sentry/types';
22

3-
import { createCheckInEnvelope } from '../src/checkin';
3+
import { createCheckInEnvelope } from '../../src/checkin';
44

5-
describe('CheckIn', () => {
5+
describe('createCheckInEnvelope', () => {
66
test('creates a check in envelope header', () => {
77
const envelope = createCheckInEnvelope(
88
{

packages/core/test/lib/sdk.test.ts

+21-26
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Scope } from '@sentry/core';
1+
import { captureCheckIn, getCurrentHub } from '@sentry/core';
22
import type { Client, Integration } from '@sentry/types';
33

44
import { installedIntegrations } from '../../src/integration';
@@ -10,31 +10,6 @@ declare var global: any;
1010

1111
const PUBLIC_DSN = 'https://username@domain/123';
1212

13-
jest.mock('@sentry/core', () => {
14-
const original = jest.requireActual('@sentry/core');
15-
return {
16-
...original,
17-
getCurrentHub(): {
18-
bindClient(client: Client): boolean;
19-
getClient(): boolean;
20-
getScope(): Scope;
21-
} {
22-
return {
23-
getClient(): boolean {
24-
return false;
25-
},
26-
getScope(): Scope {
27-
return new Scope();
28-
},
29-
bindClient(client: Client): boolean {
30-
client.setupIntegrations();
31-
return true;
32-
},
33-
};
34-
},
35-
};
36-
});
37-
3813
export class MockIntegration implements Integration {
3914
public name: string;
4015
public setupOnce: () => void = jest.fn();
@@ -62,3 +37,23 @@ describe('SDK', () => {
6237
});
6338
});
6439
});
40+
41+
describe('captureCheckIn', () => {
42+
it('returns an id when client is defined', () => {
43+
const hub = getCurrentHub();
44+
jest.spyOn(hub, 'getClient').mockImplementation(() => {
45+
return {
46+
captureCheckIn: () => 'some-id-wasd-1234',
47+
} as unknown as Client;
48+
});
49+
50+
expect(captureCheckIn({ monitorSlug: 'gogogo', status: 'in_progress' })).toStrictEqual(expect.any(String));
51+
});
52+
53+
it('returns an id when client is undefined', () => {
54+
const hub = getCurrentHub();
55+
jest.spyOn(hub, 'getClient').mockImplementation(() => undefined);
56+
57+
expect(captureCheckIn({ monitorSlug: 'gogogo', status: 'in_progress' })).toStrictEqual(expect.any(String));
58+
});
59+
});

packages/node/src/client.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Scope } from '@sentry/core';
2-
import { addTracingExtensions, BaseClient, SDK_VERSION, SessionFlusher } from '@sentry/core';
2+
import { addTracingExtensions, BaseClient, createCheckInEnvelope, SDK_VERSION, SessionFlusher } from '@sentry/core';
33
import type {
44
CheckIn,
55
Event,
@@ -13,7 +13,6 @@ import { logger, resolvedSyncPromise, uuid4 } from '@sentry/utils';
1313
import * as os from 'os';
1414
import { TextEncoder } from 'util';
1515

16-
import { createCheckInEnvelope } from './checkin';
1716
import { eventFromMessage, eventFromUnknownInput } from './eventbuilder';
1817
import type { NodeClientOptions } from './types';
1918

packages/node/src/index.ts

+2-10
Original file line numberDiff line numberDiff line change
@@ -50,22 +50,14 @@ export {
5050
spanStatusfromHttpCode,
5151
trace,
5252
withScope,
53+
captureCheckIn,
5354
} from '@sentry/core';
5455
export type { SpanStatusType } from '@sentry/core';
5556
export { autoDiscoverNodePerformanceMonitoringIntegrations } from './tracing';
5657

5758
export { NodeClient } from './client';
5859
export { makeNodeTransport } from './transports';
59-
export {
60-
defaultIntegrations,
61-
init,
62-
defaultStackParser,
63-
lastEventId,
64-
flush,
65-
close,
66-
getSentryRelease,
67-
captureCheckIn,
68-
} from './sdk';
60+
export { defaultIntegrations, init, defaultStackParser, lastEventId, flush, close, getSentryRelease } from './sdk';
6961
export { addRequestDataToEvent, DEFAULT_USER_INCLUDES, extractRequestData } from './requestdata';
7062
export { deepReadDirSync } from './utils';
7163

packages/node/src/sdk.ts

+1-26
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ import {
66
initAndBind,
77
Integrations as CoreIntegrations,
88
} from '@sentry/core';
9-
import type { CheckIn, MonitorConfig, SessionStatus, StackParser } from '@sentry/types';
9+
import type { SessionStatus, StackParser } from '@sentry/types';
1010
import {
1111
createStackParser,
1212
GLOBAL_OBJ,
1313
logger,
1414
nodeStackLineParser,
1515
stackParserFromStackParserOptions,
16-
uuid4,
1716
} from '@sentry/utils';
1817

1918
import { setNodeAsyncContextStrategy } from './async';
@@ -263,30 +262,6 @@ export function getSentryRelease(fallback?: string): string | undefined {
263262
);
264263
}
265264

266-
/**
267-
* Create a cron monitor check in and send it to Sentry.
268-
*
269-
* @param checkIn An object that describes a check in.
270-
* @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
271-
* to create a monitor automatically when sending a check in.
272-
*/
273-
export function captureCheckIn(
274-
checkIn: CheckIn,
275-
upsertMonitorConfig?: MonitorConfig,
276-
): ReturnType<NodeClient['captureCheckIn']> {
277-
const capturedCheckIn =
278-
checkIn.status !== 'in_progress' && checkIn.checkInId ? checkIn : { ...checkIn, checkInId: uuid4() };
279-
280-
const client = getCurrentHub().getClient<NodeClient>();
281-
if (client) {
282-
client.captureCheckIn(capturedCheckIn, upsertMonitorConfig);
283-
} else {
284-
__DEBUG_BUILD__ && logger.warn('Cannot capture check in. No client defined.');
285-
}
286-
287-
return capturedCheckIn.checkInId;
288-
}
289-
290265
/** Node.js stack parser */
291266
export const defaultStackParser: StackParser = createStackParser(nodeStackLineParser(getModule));
292267

packages/node/test/sdk.test.ts

-20
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { getCurrentHub } from '@sentry/core';
21
import type { Integration } from '@sentry/types';
32

4-
import type { NodeClient } from '../build/types';
53
import { init } from '../src/sdk';
64
import * as sdk from '../src/sdk';
75

@@ -92,21 +90,3 @@ describe('init()', () => {
9290
expect(newIntegration.setupOnce as jest.Mock).toHaveBeenCalledTimes(1);
9391
});
9492
});
95-
96-
describe('captureCheckIn', () => {
97-
it('always returns an id', () => {
98-
const hub = getCurrentHub();
99-
const client = hub.getClient<NodeClient>();
100-
expect(client).toBeDefined();
101-
102-
const captureCheckInSpy = jest.spyOn(client!, 'captureCheckIn');
103-
104-
// test if captureCheckIn returns an id even if client is not defined
105-
hub.bindClient(undefined);
106-
107-
expect(captureCheckInSpy).toHaveBeenCalledTimes(0);
108-
expect(sdk.captureCheckIn({ monitorSlug: 'gogogo', status: 'in_progress' })).toBeTruthy();
109-
110-
hub.bindClient(client);
111-
});
112-
});

packages/types/src/client.ts

+11
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import type { Breadcrumb, BreadcrumbHint } from './breadcrumb';
2+
import type { CheckIn, MonitorConfig } from './checkin';
23
import type { EventDropReason } from './clientreport';
34
import type { DataCategory } from './datacategory';
45
import type { DsnComponents } from './dsn';
@@ -67,6 +68,16 @@ export interface Client<O extends ClientOptions = ClientOptions> {
6768
*/
6869
captureSession?(session: Session): void;
6970

71+
/**
72+
* Create a cron monitor check in and send it to Sentry. This method is not available on all clients.
73+
*
74+
* @param checkIn An object that describes a check in.
75+
* @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
76+
* to create a monitor automatically when sending a check in.
77+
* @returns A string representing the id of the check in.
78+
*/
79+
captureCheckIn?(checkIn: CheckIn, monitorConfig?: MonitorConfig): string;
80+
7081
/** Returns the current Dsn. */
7182
getDsn(): DsnComponents | undefined;
7283

0 commit comments

Comments
 (0)