-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathclient.ts
More file actions
177 lines (159 loc) · 5.52 KB
/
client.ts
File metadata and controls
177 lines (159 loc) · 5.52 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
import type {
BrowserClientProfilingOptions,
BrowserClientReplayOptions,
ClientOptions,
Event,
EventHint,
Options,
ParameterizedString,
Scope,
SeverityLevel,
} from '@sentry/core';
import {
_INTERNAL_flushLogsBuffer,
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;
const DEFAULT_FLUSH_INTERVAL = 5000;
type BrowserSpecificOptions = BrowserClientReplayOptions &
BrowserClientProfilingOptions & {
/** If configured, this URL will be used as base URL for lazy loading integration. */
cdnBaseUrl?: string;
};
/**
* Configuration options for the Sentry Browser SDK.
* @see @sentry/core Options for more information.
*/
export type BrowserOptions = Options<BrowserTransportOptions> &
BrowserSpecificOptions & {
/**
* 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;
};
/**
* 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> {
private _logFlushIdleTimeout: ReturnType<typeof setTimeout> | undefined;
/**
* 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 } = this._options;
if (WINDOW.document && (sendClientReports || enableLogs)) {
WINDOW.document.addEventListener('visibilitychange', () => {
if (WINDOW.document.visibilityState === 'hidden') {
if (sendClientReports) {
this._flushOutcomes();
}
if (enableLogs) {
_INTERNAL_flushLogsBuffer(this);
}
}
});
}
if (enableLogs) {
this.on('flush', () => {
_INTERNAL_flushLogsBuffer(this);
});
this.on('afterCaptureLog', () => {
if (this._logFlushIdleTimeout) {
clearTimeout(this._logFlushIdleTimeout);
}
this._logFlushIdleTimeout = setTimeout(() => {
_INTERNAL_flushLogsBuffer(this);
}, DEFAULT_FLUSH_INTERVAL);
});
}
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,
};
}