-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathclient.ts
More file actions
186 lines (168 loc) · 5.97 KB
/
client.ts
File metadata and controls
186 lines (168 loc) · 5.97 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
import type {
BrowserClientProfilingOptions,
BrowserClientReplayOptions,
ClientOptions,
Event,
EventHint,
Options as CoreOptions,
ParameterizedString,
Scope,
SeverityLevel,
} from '@sentry/core';
import {
_INTERNAL_flushLogsBuffer,
_INTERNAL_flushMetricsBuffer,
addAutoIpAddressToSession,
applySdkMetadata,
Client,
getSDKSource,
} from '@sentry/core';
import { eventFromException, eventFromMessage } from './eventbuilder';
import { WINDOW } from './helpers';
import type { BrowserTransportOptions } from './transports/types';
/**
* A magic string that build tooling can leverage in order to inject a release value into the SDK.
*/
declare const __SENTRY_RELEASE__: string | undefined;
type BrowserSpecificOptions = BrowserClientReplayOptions &
BrowserClientProfilingOptions & {
/** If configured, this URL will be used as base URL for lazy loading integration. */
cdnBaseUrl?: string;
/**
* Important: Only set this option if you know what you are doing!
*
* By default, the SDK will check if `Sentry.init` is called in a browser extension.
* In case it is, it will stop initialization and log a warning
* because browser extensions require a different Sentry initialization process:
* https://docs.sentry.io/platforms/javascript/best-practices/shared-environments/
*
* Setting up the SDK in a browser extension with global error monitoring is not recommended
* and will likely flood you with errors from other web sites or extensions. This can heavily
* impact your quota and cause interference with your and other Sentry SDKs in shared environments.
*
* If this check wrongfully flags your setup as a browser extension, you can set this
* option to `true` to skip the check.
*
* @default false
*/
skipBrowserExtensionCheck?: boolean;
/**
* If you use Spotlight by Sentry during development, use
* this option to forward captured Sentry events to Spotlight.
*
* Either set it to true, or provide a specific Spotlight Sidecar URL.
*
* More details: https://spotlightjs.com/
*
* IMPORTANT: Only set this option to `true` while developing, not in production!
*/
spotlight?: boolean | string;
};
/**
* Configuration options for the Sentry Browser SDK.
* @see @sentry/core Options for more information.
*/
export type BrowserOptions = CoreOptions<BrowserTransportOptions> & BrowserSpecificOptions;
/**
* Configuration options for the Sentry Browser SDK Client class
* @see BrowserClient for more information.
*/
export type BrowserClientOptions = ClientOptions<BrowserTransportOptions> & BrowserSpecificOptions;
/**
* The Sentry Browser SDK Client.
*
* @see BrowserOptions for documentation on configuration options.
* @see SentryClient for usage documentation.
*/
export class BrowserClient extends Client<BrowserClientOptions> {
/**
* Creates a new Browser SDK instance.
*
* @param options Configuration options for this SDK.
*/
public constructor(options: BrowserClientOptions) {
const opts = applyDefaultOptions(options);
const sdkSource = WINDOW.SENTRY_SDK_SOURCE || getSDKSource();
applySdkMetadata(opts, 'browser', ['browser'], sdkSource);
// Only allow IP inferral by Relay if sendDefaultPii is true
if (opts._metadata?.sdk) {
opts._metadata.sdk.settings = {
infer_ip: opts.sendDefaultPii ? 'auto' : 'never',
// purposefully allowing already passed settings to override the default
...opts._metadata.sdk.settings,
};
}
super(opts);
const {
sendDefaultPii,
sendClientReports,
enableLogs,
_experiments,
enableMetrics: enableMetricsOption,
} = this._options;
// todo(v11): Remove the experimental flag
// eslint-disable-next-line deprecation/deprecation
const enableMetrics = enableMetricsOption ?? _experiments?.enableMetrics ?? true;
// Flush logs and metrics when page becomes hidden (e.g., tab switch, navigation)
// todo(v11): Remove the experimental flag
if (WINDOW.document && (sendClientReports || enableLogs || enableMetrics)) {
WINDOW.document.addEventListener('visibilitychange', () => {
if (WINDOW.document.visibilityState === 'hidden') {
if (sendClientReports) {
this._flushOutcomes();
}
if (enableLogs) {
_INTERNAL_flushLogsBuffer(this);
}
if (enableMetrics) {
_INTERNAL_flushMetricsBuffer(this);
}
}
});
}
if (sendDefaultPii) {
this.on('beforeSendSession', addAutoIpAddressToSession);
}
}
/**
* @inheritDoc
*/
public eventFromException(exception: unknown, hint?: EventHint): PromiseLike<Event> {
return eventFromException(this._options.stackParser, exception, hint, this._options.attachStacktrace);
}
/**
* @inheritDoc
*/
public eventFromMessage(
message: ParameterizedString,
level: SeverityLevel = 'info',
hint?: EventHint,
): PromiseLike<Event> {
return eventFromMessage(this._options.stackParser, message, level, hint, this._options.attachStacktrace);
}
/**
* @inheritDoc
*/
protected _prepareEvent(
event: Event,
hint: EventHint,
currentScope: Scope,
isolationScope: Scope,
): PromiseLike<Event | null> {
event.platform = event.platform || 'javascript';
return super._prepareEvent(event, hint, currentScope, isolationScope);
}
}
/** Exported only for tests. */
export function applyDefaultOptions<T extends Partial<BrowserClientOptions>>(optionsArg: T): T {
return {
release:
typeof __SENTRY_RELEASE__ === 'string' // This allows build tooling to find-and-replace __SENTRY_RELEASE__ to inject a release value
? __SENTRY_RELEASE__
: WINDOW.SENTRY_RELEASE?.id, // This supports the variable that sentry-webpack-plugin injects
sendClientReports: true,
// We default this to true, as it is the safer scenario
parentSpanIsAlwaysRootSpan: true,
...optionsArg,
};
}