-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathculturecontext.ts
More file actions
71 lines (63 loc) · 1.66 KB
/
culturecontext.ts
File metadata and controls
71 lines (63 loc) · 1.66 KB
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
64
65
66
67
68
69
70
71
import type { CultureContext, IntegrationFn } from '@sentry/core';
import { defineIntegration, safeSetSpanJSONAttributes } from '@sentry/core';
import { WINDOW } from '../helpers';
const INTEGRATION_NAME = 'CultureContext';
const _cultureContextIntegration = (() => {
return {
name: INTEGRATION_NAME,
preprocessEvent(event) {
const culture = getCultureContext();
if (culture) {
event.contexts = {
...event.contexts,
culture: { ...culture, ...event.contexts?.culture },
};
}
},
processSegmentSpan(span) {
const culture = getCultureContext();
if (culture) {
safeSetSpanJSONAttributes(span, {
'culture.locale': culture.locale,
'culture.timezone': culture.timezone,
'culture.calendar': culture.calendar,
});
}
},
};
}) satisfies IntegrationFn;
/**
* Captures culture context from the browser.
*
* Enabled by default.
*
* @example
* ```js
* import * as Sentry from '@sentry/browser';
*
* Sentry.init({
* integrations: [Sentry.cultureContextIntegration()],
* });
* ```
*/
export const cultureContextIntegration = defineIntegration(_cultureContextIntegration);
/**
* Returns the culture context from the browser's Intl API.
*/
function getCultureContext(): CultureContext | undefined {
try {
const intl = (WINDOW as { Intl?: typeof Intl }).Intl;
if (!intl) {
return undefined;
}
const options = intl.DateTimeFormat().resolvedOptions();
return {
locale: options.locale,
timezone: options.timeZone,
calendar: options.calendar,
};
} catch {
// Ignore errors
return undefined;
}
}