Skip to content

Commit b9a1c2b

Browse files
author
Luca Forstner
authored
ref: Hoist flush, close, and lastEventId into @sentry/core (#8731)
1 parent 436fdeb commit b9a1c2b

File tree

9 files changed

+61
-113
lines changed

9 files changed

+61
-113
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ jobs:
599599
needs: [job_get_metadata, job_build]
600600
if: needs.job_get_metadata.outputs.changed_browser == 'true' || github.event_name != 'pull_request'
601601
runs-on: ubuntu-20.04
602-
timeout-minutes: 10
602+
timeout-minutes: 20
603603
strategy:
604604
fail-fast: false
605605
matrix:

packages/browser/src/exports.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,14 @@ export {
2626
captureException,
2727
captureEvent,
2828
captureMessage,
29+
close,
2930
configureScope,
3031
createTransport,
32+
flush,
3133
getHubFromCarrier,
3234
getCurrentHub,
3335
Hub,
36+
lastEventId,
3437
makeMain,
3538
Scope,
3639
startTransaction,
@@ -60,16 +63,5 @@ export {
6063
} from './stack-parsers';
6164
export { eventFromException, eventFromMessage } from './eventbuilder';
6265
export { createUserFeedbackEnvelope } from './userfeedback';
63-
export {
64-
defaultIntegrations,
65-
forceLoad,
66-
init,
67-
lastEventId,
68-
onLoad,
69-
showReportDialog,
70-
flush,
71-
close,
72-
wrap,
73-
captureUserFeedback,
74-
} from './sdk';
66+
export { defaultIntegrations, forceLoad, init, onLoad, showReportDialog, wrap, captureUserFeedback } from './sdk';
7567
export { GlobalHandlers, TryCatch, Breadcrumbs, LinkedErrors, HttpContext, Dedupe } from './integrations';

packages/browser/src/sdk.ts

Lines changed: 1 addition & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@ import {
77
Integrations as CoreIntegrations,
88
} from '@sentry/core';
99
import type { UserFeedback } from '@sentry/types';
10-
import {
11-
addInstrumentationHandler,
12-
logger,
13-
resolvedSyncPromise,
14-
stackParserFromStackParserOptions,
15-
supportsFetch,
16-
} from '@sentry/utils';
10+
import { addInstrumentationHandler, logger, stackParserFromStackParserOptions, supportsFetch } from '@sentry/utils';
1711

1812
import type { BrowserClientOptions, BrowserOptions } from './client';
1913
import { BrowserClient } from './client';
@@ -179,15 +173,6 @@ export function showReportDialog(options: ReportDialogOptions = {}, hub: Hub = g
179173
}
180174
}
181175

182-
/**
183-
* This is the getter for lastEventId.
184-
*
185-
* @returns The last event id of a captured event.
186-
*/
187-
export function lastEventId(): string | undefined {
188-
return getCurrentHub().lastEventId();
189-
}
190-
191176
/**
192177
* This function is here to be API compatible with the loader.
193178
* @hidden
@@ -204,40 +189,6 @@ export function onLoad(callback: () => void): void {
204189
callback();
205190
}
206191

207-
/**
208-
* Call `flush()` on the current client, if there is one. See {@link Client.flush}.
209-
*
210-
* @param timeout Maximum time in ms the client should wait to flush its event queue. Omitting this parameter will cause
211-
* the client to wait until all events are sent before resolving the promise.
212-
* @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it
213-
* doesn't (or if there's no client defined).
214-
*/
215-
export function flush(timeout?: number): PromiseLike<boolean> {
216-
const client = getCurrentHub().getClient<BrowserClient>();
217-
if (client) {
218-
return client.flush(timeout);
219-
}
220-
__DEBUG_BUILD__ && logger.warn('Cannot flush events. No client defined.');
221-
return resolvedSyncPromise(false);
222-
}
223-
224-
/**
225-
* Call `close()` on the current client, if there is one. See {@link Client.close}.
226-
*
227-
* @param timeout Maximum time in ms the client should wait to flush its event queue before shutting down. Omitting this
228-
* parameter will cause the client to wait until all events are sent before disabling itself.
229-
* @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it
230-
* doesn't (or if there's no client defined).
231-
*/
232-
export function close(timeout?: number): PromiseLike<boolean> {
233-
const client = getCurrentHub().getClient<BrowserClient>();
234-
if (client) {
235-
return client.close(timeout);
236-
}
237-
__DEBUG_BUILD__ && logger.warn('Cannot flush events and disable SDK. No client defined.');
238-
return resolvedSyncPromise(false);
239-
}
240-
241192
/**
242193
* Wrap code within a try/catch block so the SDK is able to capture errors.
243194
*

packages/core/src/exports.ts

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,46 @@ export function captureCheckIn(checkIn: CheckIn, upsertMonitorConfig?: MonitorCo
209209

210210
return uuid4();
211211
}
212+
213+
/**
214+
* Call `flush()` on the current client, if there is one. See {@link Client.flush}.
215+
*
216+
* @param timeout Maximum time in ms the client should wait to flush its event queue. Omitting this parameter will cause
217+
* the client to wait until all events are sent before resolving the promise.
218+
* @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it
219+
* doesn't (or if there's no client defined).
220+
*/
221+
export async function flush(timeout?: number): Promise<boolean> {
222+
const client = getCurrentHub().getClient();
223+
if (client) {
224+
return client.flush(timeout);
225+
}
226+
__DEBUG_BUILD__ && logger.warn('Cannot flush events. No client defined.');
227+
return Promise.resolve(false);
228+
}
229+
230+
/**
231+
* Call `close()` on the current client, if there is one. See {@link Client.close}.
232+
*
233+
* @param timeout Maximum time in ms the client should wait to flush its event queue before shutting down. Omitting this
234+
* parameter will cause the client to wait until all events are sent before disabling itself.
235+
* @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it
236+
* doesn't (or if there's no client defined).
237+
*/
238+
export async function close(timeout?: number): Promise<boolean> {
239+
const client = getCurrentHub().getClient();
240+
if (client) {
241+
return client.close(timeout);
242+
}
243+
__DEBUG_BUILD__ && logger.warn('Cannot flush events and disable SDK. No client defined.');
244+
return Promise.resolve(false);
245+
}
246+
247+
/**
248+
* This is the getter for lastEventId.
249+
*
250+
* @returns The last event id of a captured event.
251+
*/
252+
export function lastEventId(): string | undefined {
253+
return getCurrentHub().lastEventId();
254+
}

packages/core/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,14 @@ export type { OfflineStore, OfflineTransportOptions } from './transports/offline
55
export * from './tracing';
66
export {
77
addBreadcrumb,
8+
captureCheckIn,
89
captureException,
910
captureEvent,
1011
captureMessage,
12+
close,
1113
configureScope,
14+
flush,
15+
lastEventId,
1216
startTransaction,
1317
setContext,
1418
setExtra,
@@ -17,7 +21,6 @@ export {
1721
setTags,
1822
setUser,
1923
withScope,
20-
captureCheckIn,
2124
} from './exports';
2225
export {
2326
getCurrentHub,

packages/node/src/handlers.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/* eslint-disable @typescript-eslint/no-explicit-any */
22
import {
33
captureException,
4+
flush,
45
getCurrentHub,
56
hasTracingEnabled,
67
runWithAsyncContext,
@@ -25,7 +26,7 @@ import type { NodeClient } from './client';
2526
import { extractRequestData } from './requestdata';
2627
// TODO (v8 / XXX) Remove this import
2728
import type { ParseRequestOptions } from './requestDataDeprecated';
28-
import { flush, isAutoSessionTrackingEnabled } from './sdk';
29+
import { isAutoSessionTrackingEnabled } from './sdk';
2930

3031
/**
3132
* Express-compatible tracing handler.

packages/node/src/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,16 @@ export {
2929
captureException,
3030
captureEvent,
3131
captureMessage,
32+
close,
3233
configureScope,
3334
createTransport,
3435
extractTraceparentData,
36+
flush,
3537
getActiveTransaction,
3638
getHubFromCarrier,
3739
getCurrentHub,
3840
Hub,
41+
lastEventId,
3942
makeMain,
4043
runWithAsyncContext,
4144
Scope,
@@ -57,7 +60,7 @@ export { autoDiscoverNodePerformanceMonitoringIntegrations } from './tracing';
5760

5861
export { NodeClient } from './client';
5962
export { makeNodeTransport } from './transports';
60-
export { defaultIntegrations, init, defaultStackParser, lastEventId, flush, close, getSentryRelease } from './sdk';
63+
export { defaultIntegrations, init, defaultStackParser, getSentryRelease } from './sdk';
6164
export { addRequestDataToEvent, DEFAULT_USER_INCLUDES, extractRequestData } from './requestdata';
6265
export { deepReadDirSync } from './utils';
6366
export { getModuleFromFilename } from './module';

packages/node/src/sdk.ts

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import type { SessionStatus, StackParser } from '@sentry/types';
1010
import {
1111
createStackParser,
1212
GLOBAL_OBJ,
13-
logger,
1413
nodeStackLineParser,
1514
stackParserFromStackParserOptions,
1615
tracingContextFromHeaders,
@@ -177,49 +176,6 @@ export function init(options: NodeOptions = {}): void {
177176
updateScopeFromEnvVariables();
178177
}
179178

180-
/**
181-
* This is the getter for lastEventId.
182-
*
183-
* @returns The last event id of a captured event.
184-
*/
185-
export function lastEventId(): string | undefined {
186-
return getCurrentHub().lastEventId();
187-
}
188-
189-
/**
190-
* Call `flush()` on the current client, if there is one. See {@link Client.flush}.
191-
*
192-
* @param timeout Maximum time in ms the client should wait to flush its event queue. Omitting this parameter will cause
193-
* the client to wait until all events are sent before resolving the promise.
194-
* @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it
195-
* doesn't (or if there's no client defined).
196-
*/
197-
export async function flush(timeout?: number): Promise<boolean> {
198-
const client = getCurrentHub().getClient<NodeClient>();
199-
if (client) {
200-
return client.flush(timeout);
201-
}
202-
__DEBUG_BUILD__ && logger.warn('Cannot flush events. No client defined.');
203-
return Promise.resolve(false);
204-
}
205-
206-
/**
207-
* Call `close()` on the current client, if there is one. See {@link Client.close}.
208-
*
209-
* @param timeout Maximum time in ms the client should wait to flush its event queue before shutting down. Omitting this
210-
* parameter will cause the client to wait until all events are sent before disabling itself.
211-
* @returns A promise which resolves to `true` if the queue successfully drains before the timeout, or `false` if it
212-
* doesn't (or if there's no client defined).
213-
*/
214-
export async function close(timeout?: number): Promise<boolean> {
215-
const client = getCurrentHub().getClient<NodeClient>();
216-
if (client) {
217-
return client.close(timeout);
218-
}
219-
__DEBUG_BUILD__ && logger.warn('Cannot flush events and disable SDK. No client defined.');
220-
return Promise.resolve(false);
221-
}
222-
223179
/**
224180
* Function that takes an instance of NodeClient and checks if autoSessionTracking option is enabled for that client
225181
*/

packages/node/test/handlers.test.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import * as http from 'http';
77

88
import { NodeClient } from '../src/client';
99
import { errorHandler, requestHandler, tracingHandler } from '../src/handlers';
10-
import * as SDK from '../src/sdk';
1110
import { getDefaultNodeClientOptions } from './helper/node-client-options';
1211

1312
function mockAsyncContextStrategy(getHub: () => Hub): void {
@@ -128,7 +127,7 @@ describe('requestHandler', () => {
128127
});
129128

130129
it('patches `res.end` when `flushTimeout` is specified', done => {
131-
const flush = jest.spyOn(SDK, 'flush').mockResolvedValue(true);
130+
const flush = jest.spyOn(sentryCore, 'flush').mockResolvedValue(true);
132131

133132
const sentryRequestMiddleware = requestHandler({ flushTimeout: 1337 });
134133
sentryRequestMiddleware(req, res, next);
@@ -142,7 +141,7 @@ describe('requestHandler', () => {
142141
});
143142

144143
it('prevents errors thrown during `flush` from breaking the response', done => {
145-
jest.spyOn(SDK, 'flush').mockRejectedValue(new SentryError('HTTP Error (429)'));
144+
jest.spyOn(sentryCore, 'flush').mockRejectedValue(new SentryError('HTTP Error (429)'));
146145

147146
const sentryRequestMiddleware = requestHandler({ flushTimeout: 1337 });
148147
sentryRequestMiddleware(req, res, next);

0 commit comments

Comments
 (0)