Skip to content

Commit a2496b8

Browse files
committed
better export
1 parent 8196869 commit a2496b8

3 files changed

Lines changed: 93 additions & 91 deletions

File tree

packages/core/src/exports.ts

Lines changed: 1 addition & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import type { AttributeObject, RawAttribute, RawAttributes } from './attributes';
2-
import { getTraceData } from './browser';
3-
import { getClient, getCurrentScope, getIsolationScope, withIsolationScope } from './currentScopes';
2+
import { getClient, getCurrentScope, getIsolationScope } from './currentScopes';
43
import { DEBUG_BUILD } from './debug-build';
54
import type { CaptureContext } from './scope';
65
import { closeSession, makeSession, updateSession } from './session';
7-
import { continueTrace, startNewTrace } from './tracing/trace';
8-
import type { CheckIn, FinishedCheckIn, MonitorConfig } from './types/checkin';
96
import type { Event, EventHint } from './types/event';
107
import type { EventProcessor } from './types/eventprocessor';
118
import type { Extra, Extras } from './types/extra';
@@ -14,12 +11,9 @@ import type { Session, SessionContext } from './types/session';
1411
import type { SeverityLevel } from './types/severity';
1512
import type { User } from './types/user';
1613
import { debug } from './utils/debug-logger';
17-
import { isThenable } from './utils/is';
18-
import { uuid4 } from './utils/misc';
1914
import type { ExclusiveEventHintOrCaptureContext } from './utils/prepareEvent';
2015
import { parseEventHintOrCaptureContext } from './utils/prepareEvent';
2116
import { getCombinedScopeData } from './utils/scopeData';
22-
import { timestampInSeconds } from './utils/time';
2317
import { GLOBAL_OBJ } from './utils/worldwide';
2418

2519
/**
@@ -183,88 +177,6 @@ export function lastEventId(): string | undefined {
183177
return getIsolationScope().lastEventId();
184178
}
185179

186-
/**
187-
* Create a cron monitor check in and send it to Sentry.
188-
*
189-
* @param checkIn An object that describes a check in.
190-
* @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
191-
* to create a monitor automatically when sending a check in.
192-
*/
193-
export function captureCheckIn(checkIn: CheckIn, upsertMonitorConfig?: MonitorConfig): string {
194-
const scope = getCurrentScope();
195-
const client = getClient();
196-
if (!client) {
197-
DEBUG_BUILD && debug.warn('Cannot capture check-in. No client defined.');
198-
} else if (!client.captureCheckIn) {
199-
DEBUG_BUILD && debug.warn('Cannot capture check-in. Client does not support sending check-ins.');
200-
} else {
201-
return client.captureCheckIn(checkIn, upsertMonitorConfig, scope);
202-
}
203-
204-
return uuid4();
205-
}
206-
207-
/**
208-
* Wraps a callback with a cron monitor check in. The check in will be sent to Sentry when the callback finishes.
209-
*
210-
* @param monitorSlug The distinct slug of the monitor.
211-
* @param callback Callback to be monitored
212-
* @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
213-
* to create a monitor automatically when sending a check in.
214-
*/
215-
export function withMonitor<T>(
216-
monitorSlug: CheckIn['monitorSlug'],
217-
callback: () => T,
218-
upsertMonitorConfig?: MonitorConfig,
219-
): T {
220-
function runCallback(): T {
221-
const checkInId = captureCheckIn({ monitorSlug, status: 'in_progress' }, upsertMonitorConfig);
222-
const now = timestampInSeconds();
223-
224-
function finishCheckIn(status: FinishedCheckIn['status']): void {
225-
captureCheckIn({ monitorSlug, status, checkInId, duration: timestampInSeconds() - now });
226-
}
227-
// Default behavior without isolateTrace
228-
let maybePromiseResult: T;
229-
try {
230-
maybePromiseResult = callback();
231-
} catch (e) {
232-
finishCheckIn('error');
233-
throw e;
234-
}
235-
236-
if (isThenable(maybePromiseResult)) {
237-
return maybePromiseResult.then(
238-
r => {
239-
finishCheckIn('ok');
240-
return r;
241-
},
242-
e => {
243-
finishCheckIn('error');
244-
throw e;
245-
},
246-
) as T;
247-
}
248-
finishCheckIn('ok');
249-
250-
return maybePromiseResult;
251-
}
252-
253-
// With isolation scope resets the propagation context, so if we do not pass isolateTace, we want to manually continue the trace
254-
const traceData = getTraceData();
255-
return withIsolationScope(() =>
256-
upsertMonitorConfig?.isolateTrace
257-
? startNewTrace(runCallback)
258-
: continueTrace(
259-
{
260-
sentryTrace: traceData['sentry-trace'],
261-
baggage: traceData['baggage'],
262-
},
263-
runCallback,
264-
),
265-
);
266-
}
267-
268180
/**
269181
* Call `flush()` on the current client, if there is one. See {@link Client.flush}.
270182
*

packages/core/src/monitor.ts

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import { getClient, getCurrentScope, withIsolationScope } from './currentScopes';
2+
import { DEBUG_BUILD } from './debug-build';
3+
import { continueTrace, startNewTrace } from './tracing/trace';
4+
import type { CheckIn, FinishedCheckIn, MonitorConfig } from './types/checkin';
5+
import { debug } from './utils/debug-logger';
6+
import { isThenable } from './utils/is';
7+
import { uuid4 } from './utils/misc';
8+
import { timestampInSeconds } from './utils/time';
9+
import { getTraceData } from './utils/traceData';
10+
11+
/**
12+
* Wraps a callback with a cron monitor check in. The check in will be sent to Sentry when the callback finishes.
13+
*
14+
* @param monitorSlug The distinct slug of the monitor.
15+
* @param callback Callback to be monitored
16+
* @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
17+
* to create a monitor automatically when sending a check in.
18+
*/
19+
export function withMonitor<T>(
20+
monitorSlug: CheckIn['monitorSlug'],
21+
callback: () => T,
22+
upsertMonitorConfig?: MonitorConfig,
23+
): T {
24+
function runCallback(): T {
25+
const checkInId = captureCheckIn({ monitorSlug, status: 'in_progress' }, upsertMonitorConfig);
26+
const now = timestampInSeconds();
27+
28+
function finishCheckIn(status: FinishedCheckIn['status']): void {
29+
captureCheckIn({ monitorSlug, status, checkInId, duration: timestampInSeconds() - now });
30+
}
31+
// Default behavior without isolateTrace
32+
let maybePromiseResult: T;
33+
try {
34+
maybePromiseResult = callback();
35+
} catch (e) {
36+
finishCheckIn('error');
37+
throw e;
38+
}
39+
40+
if (isThenable(maybePromiseResult)) {
41+
return maybePromiseResult.then(
42+
r => {
43+
finishCheckIn('ok');
44+
return r;
45+
},
46+
e => {
47+
finishCheckIn('error');
48+
throw e;
49+
},
50+
) as T;
51+
}
52+
finishCheckIn('ok');
53+
54+
return maybePromiseResult;
55+
}
56+
57+
// With isolation scope resets the propagation context, so if we do not pass isolateTace, we want to manually continue the trace
58+
const traceData = getTraceData();
59+
return withIsolationScope(() =>
60+
upsertMonitorConfig?.isolateTrace
61+
? startNewTrace(runCallback)
62+
: continueTrace(
63+
{
64+
sentryTrace: traceData['sentry-trace'],
65+
baggage: traceData['baggage'],
66+
},
67+
runCallback,
68+
),
69+
);
70+
}
71+
72+
/**
73+
* Create a cron monitor check in and send it to Sentry.
74+
*
75+
* @param checkIn An object that describes a check in.
76+
* @param upsertMonitorConfig An optional object that describes a monitor config. Use this if you want
77+
* to create a monitor automatically when sending a check in.
78+
*/
79+
export function captureCheckIn(checkIn: CheckIn, upsertMonitorConfig?: MonitorConfig): string {
80+
const scope = getCurrentScope();
81+
const client = getClient();
82+
if (!client) {
83+
DEBUG_BUILD && debug.warn('Cannot capture check-in. No client defined.');
84+
} else if (!client.captureCheckIn) {
85+
DEBUG_BUILD && debug.warn('Cannot capture check-in. Client does not support sending check-ins.');
86+
} else {
87+
return client.captureCheckIn(checkIn, upsertMonitorConfig, scope);
88+
}
89+
90+
return uuid4();
91+
}

packages/core/src/shared-exports.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ export * from './tracing';
1212
export * from './semanticAttributes';
1313
export { createEventEnvelope, createSessionEnvelope } from './envelope';
1414
export {
15-
captureCheckIn,
16-
withMonitor,
1715
captureException,
1816
captureEvent,
1917
captureMessage,
@@ -36,6 +34,7 @@ export {
3634
captureSession,
3735
addEventProcessor,
3836
} from './exports';
37+
export { withMonitor, captureCheckIn } from './monitor';
3938
export {
4039
getCurrentScope,
4140
getIsolationScope,

0 commit comments

Comments
 (0)