-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathuniversal.ts
63 lines (59 loc) · 2.13 KB
/
universal.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { createLogger } from '@/modules/core/logging/logger';
import { FLUSH_TIMEOUT } from '@/modules/core/sentry/config';
import { UserSession } from '@/modules/core/userSession/useUserSession';
import * as Sentry from '@sentry/node';
const fileLabel = 'modules/core/sentry/universal';
const logger = createLogger({
fileLabel,
});
/**
* Configure Sentry tags related to the current user.
*
* Allows to track all Sentry events related to a particular user.
* The tracking remains anonymous, there are no personal information being tracked, only internal ids.
*
* @param userSession
* @see https://www.npmjs.com/package/@sentry/node
*/
export const configureSentryUserMetadata = (userSession: UserSession): void => {
if (process.env.SENTRY_DSN) {
Sentry.configureScope((scope) => {
scope.setTag('userId', userSession?.id);
scope.setTag('userDeviceId', userSession?.deviceId);
scope.setContext('user', userSession);
});
}
};
/**
* Configure Sentry tags for the currently used lang/locale.
*
* @param lang
* @param locale
* @see https://www.npmjs.com/package/@sentry/node
*/
export const configureSentryI18n = (lang: string, locale: string): void => {
if (process.env.SENTRY_DSN) {
Sentry.configureScope((scope) => { // See https://www.npmjs.com/package/@sentry/node
scope.setTag('lang', lang);
scope.setTag('locale', locale);
});
}
};
/**
* Flushes Sentry queue in a safe way.
*
* It's necessary to flush all Sentry events on the server, because Vercel runs on AWS Lambda, see https://vercel.com/docs/platform/limits#streaming-responses
* If you don't flush, then it's possible the Sentry events won't be sent.
* This helper is meant to be used for backend-only usage. (not frontend)
*
* There is a potential bug in Sentry that throws an exception when flushing times out, causing API endpoints to fail.
* @see https://github.com/getsentry/sentry/issues/26870
*/
export const flushSafe = async (): Promise<boolean> => {
try {
return await Sentry.flush(FLUSH_TIMEOUT);
} catch (e) {
logger.error(`[flushSafe] An exception was thrown while running Sentry.flush()`, e);
return false;
}
};