-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathindex.ts
258 lines (221 loc) · 6.45 KB
/
index.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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import type { RumOtelWebConfig } from '@hyperdx/otel-web';
import Rum from '@hyperdx/otel-web';
import SessionRecorder, {
RumRecorderConfig,
} from '@hyperdx/otel-web-session-recorder';
import opentelemetry, { Attributes } from '@opentelemetry/api';
import { resolveAsyncGlobal } from './utils';
type ErrorBoundaryComponent = any; // TODO: Define ErrorBoundary type
type Instrumentations = RumOtelWebConfig['instrumentations'];
type IgnoreUrls = RumOtelWebConfig['ignoreUrls'];
type BrowserSDKConfig = {
advancedNetworkCapture?: boolean;
apiKey: string;
blockClass?: string;
captureConsole?: boolean; // deprecated
consoleCapture?: boolean;
debug?: boolean;
disableIntercom?: boolean;
disableReplay?: boolean;
ignoreClass?: string;
ignoreUrls?: IgnoreUrls;
instrumentations?: Instrumentations;
maskAllInputs?: boolean;
maskAllText?: boolean;
maskClass?: string;
recordCanvas?: boolean;
sampling?: RumRecorderConfig['sampling'];
service: string;
tracePropagationTargets?: (string | RegExp)[];
url?: string;
};
const URL_BASE = 'https://in-otel.hyperdx.io';
const UI_BASE = 'https://www.hyperdx.io';
function hasWindow() {
return typeof window !== 'undefined';
}
class Browser {
private _advancedNetworkCapture = false;
init({
advancedNetworkCapture = false,
apiKey,
blockClass,
captureConsole, // deprecated
consoleCapture,
debug = false,
disableIntercom = false,
disableReplay = false,
ignoreClass,
ignoreUrls,
instrumentations = {},
maskAllInputs = true,
maskAllText = false,
maskClass,
recordCanvas = false,
sampling,
service,
tracePropagationTargets,
url,
}: BrowserSDKConfig) {
if (!hasWindow()) {
return;
}
if (apiKey == null) {
console.warn('HyperDX: Missing apiKey, telemetry will not be saved.');
} else if (apiKey === '') {
console.warn(
'HyperDX: apiKey is empty string, telemetry will not be saved.',
);
} else if (typeof apiKey !== 'string') {
console.warn(
'HyperDX: apiKey must be a string, telemetry will not be saved.',
);
}
const urlBase = url ?? URL_BASE;
this._advancedNetworkCapture = advancedNetworkCapture;
Rum.init({
debug,
url: `${urlBase}/v1/traces`,
allowInsecureUrl: true,
apiKey,
applicationName: service,
ignoreUrls,
instrumentations: {
visibility: true,
console: captureConsole ?? consoleCapture ?? false,
fetch: {
...(tracePropagationTargets != null
? {
propagateTraceHeaderCorsUrls: tracePropagationTargets,
}
: {}),
advancedNetworkCapture: () => this._advancedNetworkCapture,
},
xhr: {
...(tracePropagationTargets != null
? {
propagateTraceHeaderCorsUrls: tracePropagationTargets,
}
: {}),
advancedNetworkCapture: () => this._advancedNetworkCapture,
},
...instrumentations,
},
});
if (disableReplay !== true) {
SessionRecorder.init({
apiKey,
blockClass,
debug,
ignoreClass,
maskAllInputs: maskAllInputs,
maskTextClass: maskClass,
maskTextSelector: maskAllText ? '*' : undefined,
recordCanvas,
sampling,
url: `${urlBase}/v1/logs`,
});
}
const tracer = opentelemetry.trace.getTracer('@hyperdx/browser');
if (disableIntercom !== true) {
resolveAsyncGlobal('Intercom')
.then(() => {
window.Intercom('onShow', () => {
const sessionUrl = this.getSessionUrl();
if (sessionUrl != null) {
const metadata = {
hyperdxSessionUrl: sessionUrl,
};
// Use window.Intercom directly to avoid stale references
window.Intercom('update', metadata);
window.Intercom('trackEvent', 'HyperDX', metadata);
const now = Date.now();
const span = tracer.startSpan('intercom.onShow', {
startTime: now,
});
span.setAttribute('component', 'intercom');
span.end(now);
}
});
})
.catch(() => {
// Ignore if intercom isn't installed or can't be used
});
}
}
stopSessionRecorder(): void {
if (!hasWindow()) {
return;
}
SessionRecorder.stop();
}
resumeSessionRecorder(): void {
if (!hasWindow()) {
return;
}
SessionRecorder.resume();
}
addAction(name: string, attributes?: Attributes): void {
if (!hasWindow()) {
return;
}
Rum.addAction(name, attributes);
}
recordException(error: any, attributes?: Attributes): void {
if (!hasWindow()) {
return;
}
Rum.recordException(error, attributes);
}
enableAdvancedNetworkCapture(): void {
this._advancedNetworkCapture = true;
}
disableAdvancedNetworkCapture(): void {
this._advancedNetworkCapture = false;
}
setGlobalAttributes(
attributes: Record<
'userId' | 'userEmail' | 'userName' | 'teamName' | 'teamId' | string,
string
>,
): void {
if (!hasWindow()) {
return;
}
Rum.setGlobalAttributes(attributes);
}
getSessionId(): string | undefined {
return Rum.getSessionId();
}
getSessionUrl(): string | undefined {
const now = Date.now();
// A session can only last 4 hours, so we just need to give a time hint of
// a 4 hour range
const FOUR_HOURS = 1000 * 60 * 60 * 4;
const start = now - FOUR_HOURS;
const end = now + FOUR_HOURS;
return Rum.inited
? `${UI_BASE}/sessions?q=process.tag.rum.sessionId%3A"${Rum.getSessionId()}"&sid=${Rum.getSessionId()}&sfrom=${start}&sto=${end}&ts=${now}`
: undefined;
}
attachToReactErrorBoundary(errorBoundary: ErrorBoundaryComponent) {
if (!errorBoundary) {
return console.warn(
'Attempted to attach to an ErrorBoundary that does not exist.',
);
}
const recordException = this.recordException;
const originalComponentDidCatch = errorBoundary.prototype.componentDidCatch;
errorBoundary.prototype.componentDidCatch = function (
error: Error,
errorInfo: any,
) {
const componentStack = errorInfo?.componentStack;
recordException(error, {
componentStack,
});
originalComponentDidCatch.call(this, error, errorInfo);
};
}
}
export default new Browser();