@webex/internal-plugin-metrics 3.12.0-llmrefactor.2 → 3.12.0-llmrefactor.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +36 -0
- package/dist/config.js +3 -0
- package/dist/config.js.map +1 -1
- package/dist/metrics.js +1 -1
- package/dist/new-metrics.js +3 -0
- package/dist/new-metrics.js.map +1 -1
- package/dist/types/config.d.ts +3 -0
- package/dist/types/unhandled-exception-telemetry/index.d.ts +60 -0
- package/dist/types/unhandled-exception-telemetry/utils.d.ts +6 -0
- package/dist/unhandled-exception-telemetry/index.js +330 -0
- package/dist/unhandled-exception-telemetry/index.js.map +1 -0
- package/dist/unhandled-exception-telemetry/utils.js +105 -0
- package/dist/unhandled-exception-telemetry/utils.js.map +1 -0
- package/package.json +3 -3
- package/src/config.js +3 -0
- package/src/new-metrics.ts +3 -0
- package/src/unhandled-exception-telemetry/index.ts +403 -0
- package/src/unhandled-exception-telemetry/utils.ts +101 -0
- package/test/unit/spec/new-metrics.ts +26 -6
- package/test/unit/spec/unhandled-exception-telemetry/utils.ts +109 -0
- package/test/unit/spec/unhandled-exception-telemetry.ts +688 -0
|
@@ -0,0 +1,403 @@
|
|
|
1
|
+
/* eslint-disable require-jsdoc, valid-jsdoc */
|
|
2
|
+
|
|
3
|
+
import {safeSetTimeout} from '@webex/common-timers';
|
|
4
|
+
import uuid from 'uuid';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
createFingerprint,
|
|
8
|
+
removeUrlDetails,
|
|
9
|
+
removeUrlDetailsFromText,
|
|
10
|
+
sanitizeResourceUrl,
|
|
11
|
+
stringifyReason,
|
|
12
|
+
truncate,
|
|
13
|
+
} from './utils';
|
|
14
|
+
|
|
15
|
+
export const UNHANDLED_EXCEPTION_METRIC_NAME = 'JS_SDK_OBSERVED_CLIENT_UNHANDLED_EXCEPTION';
|
|
16
|
+
|
|
17
|
+
const DEDUPE_WINDOW_MS = 1_000;
|
|
18
|
+
const MAX_ERROR_MESSAGE_LENGTH = 4_096;
|
|
19
|
+
const MAX_ERROR_NAME_LENGTH = 256;
|
|
20
|
+
const MAX_METADATA_LENGTH = 32_000;
|
|
21
|
+
const MAX_STACK_LENGTH = 8_192;
|
|
22
|
+
|
|
23
|
+
const TELEMETRY_LOG_IDENTIFIER = 'Unhandled Exception Telemetry -->';
|
|
24
|
+
|
|
25
|
+
type ErrorDetails = {
|
|
26
|
+
column?: number;
|
|
27
|
+
filename?: string;
|
|
28
|
+
kind: 'error' | 'unhandledrejection' | 'resource_error';
|
|
29
|
+
line?: number;
|
|
30
|
+
message?: string;
|
|
31
|
+
name: string;
|
|
32
|
+
/** Uppercase DOM tag name, such as SCRIPT, LINK, or IMG; RESOURCE when unavailable. */
|
|
33
|
+
resourceType?: string;
|
|
34
|
+
resourceUrl?: string;
|
|
35
|
+
stack?: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export type UnhandledExceptionEvent = {
|
|
39
|
+
schemaVersion: 1;
|
|
40
|
+
capturedAt: number;
|
|
41
|
+
eventId: string;
|
|
42
|
+
occurrenceCount: number;
|
|
43
|
+
common: {
|
|
44
|
+
appName: string;
|
|
45
|
+
appVersion?: string;
|
|
46
|
+
runtime: 'browser';
|
|
47
|
+
sdkVersion?: string;
|
|
48
|
+
};
|
|
49
|
+
error: ErrorDetails & {fingerprint: string};
|
|
50
|
+
metadata?: Record<string, unknown>;
|
|
51
|
+
metadataCaptureStatus?: 'invalid_type' | 'provider_error' | 'too_large';
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
type WebexForUnhandledExceptionTelemetry = {
|
|
55
|
+
canAuthorize?: boolean;
|
|
56
|
+
version?: string;
|
|
57
|
+
config?: {
|
|
58
|
+
appName?: string;
|
|
59
|
+
appVersion?: string;
|
|
60
|
+
sdkType?: string;
|
|
61
|
+
metrics?: {
|
|
62
|
+
unhandledExceptionTelemetry?: {
|
|
63
|
+
enabled?: boolean;
|
|
64
|
+
getMetadata?: () => Record<string, unknown> | undefined;
|
|
65
|
+
};
|
|
66
|
+
};
|
|
67
|
+
};
|
|
68
|
+
internal?: {
|
|
69
|
+
metrics?: {
|
|
70
|
+
submitClientMetrics?: (name: string, properties: object, preLoginId?: string) => unknown;
|
|
71
|
+
};
|
|
72
|
+
};
|
|
73
|
+
logger?: {
|
|
74
|
+
error?: (...args: unknown[]) => unknown;
|
|
75
|
+
};
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
function logTelemetryFailure(webex: WebexForUnhandledExceptionTelemetry, message: string): void {
|
|
79
|
+
try {
|
|
80
|
+
webex.logger?.error?.(TELEMETRY_LOG_IDENTIFIER, message);
|
|
81
|
+
} catch {
|
|
82
|
+
// Logging must not turn a telemetry failure into an application failure.
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* SDK-owned browser exception reporter with a single in-memory deduplication window.
|
|
88
|
+
*/
|
|
89
|
+
class UnhandledExceptionTelemetry {
|
|
90
|
+
private readonly pendingEvents = new Map<string, UnhandledExceptionEvent>();
|
|
91
|
+
private readonly eventTarget: Window;
|
|
92
|
+
private readonly errorListener = (event: ErrorEvent) => this.captureError(event);
|
|
93
|
+
private readonly preLoginId: string;
|
|
94
|
+
private readonly rejectionListener = (event: PromiseRejectionEvent) =>
|
|
95
|
+
this.captureRejection(event);
|
|
96
|
+
|
|
97
|
+
private readonly webex: WebexForUnhandledExceptionTelemetry;
|
|
98
|
+
private flushTimer?: number | NodeJS.Timeout;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Registers browser listeners after the SDK is ready.
|
|
102
|
+
* @param webex SDK instance used for configuration and submission.
|
|
103
|
+
* @param eventTarget Browser window.
|
|
104
|
+
*/
|
|
105
|
+
constructor(webex: WebexForUnhandledExceptionTelemetry, eventTarget: Window) {
|
|
106
|
+
this.webex = webex;
|
|
107
|
+
this.eventTarget = eventTarget;
|
|
108
|
+
this.preLoginId = uuid.v4();
|
|
109
|
+
eventTarget.addEventListener('error', this.errorListener, {capture: true});
|
|
110
|
+
eventTarget.addEventListener('unhandledrejection', this.rejectionListener, {capture: true});
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Removes global listeners and either flushes or discards pending events.
|
|
115
|
+
* @param flushPending Whether to submit pending telemetry before teardown.
|
|
116
|
+
* @returns {void}
|
|
117
|
+
*/
|
|
118
|
+
stop(flushPending: boolean): void {
|
|
119
|
+
this.eventTarget.removeEventListener('error', this.errorListener, {capture: true});
|
|
120
|
+
this.eventTarget.removeEventListener('unhandledrejection', this.rejectionListener, {
|
|
121
|
+
capture: true,
|
|
122
|
+
});
|
|
123
|
+
this.clearFlushTimer();
|
|
124
|
+
|
|
125
|
+
if (flushPending) {
|
|
126
|
+
this.pendingEvents.forEach((event) => this.submit(event));
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
this.pendingEvents.clear();
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
private captureError(event: ErrorEvent): void {
|
|
133
|
+
try {
|
|
134
|
+
const resourceTarget = event.target;
|
|
135
|
+
const currentSrc = resourceTarget ? Reflect.get(resourceTarget, 'currentSrc') : undefined;
|
|
136
|
+
const src = resourceTarget ? Reflect.get(resourceTarget, 'src') : undefined;
|
|
137
|
+
const href = resourceTarget ? Reflect.get(resourceTarget, 'href') : undefined;
|
|
138
|
+
const rawResourceUrl =
|
|
139
|
+
(typeof currentSrc === 'string' && currentSrc.length > 0 && currentSrc) ||
|
|
140
|
+
(typeof src === 'string' && src.length > 0 && src) ||
|
|
141
|
+
(typeof href === 'string' && href.length > 0 && href) ||
|
|
142
|
+
undefined;
|
|
143
|
+
|
|
144
|
+
if (rawResourceUrl) {
|
|
145
|
+
const resourceUrl = sanitizeResourceUrl(rawResourceUrl);
|
|
146
|
+
|
|
147
|
+
if (!resourceUrl) {
|
|
148
|
+
return;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const resourceType = String(
|
|
152
|
+
(resourceTarget && Reflect.get(resourceTarget, 'tagName')) ?? 'RESOURCE'
|
|
153
|
+
).toUpperCase();
|
|
154
|
+
|
|
155
|
+
this.capture({
|
|
156
|
+
kind: 'resource_error',
|
|
157
|
+
message: `Failed to load ${resourceType} resource`,
|
|
158
|
+
name: 'ResourceError',
|
|
159
|
+
resourceType,
|
|
160
|
+
resourceUrl,
|
|
161
|
+
});
|
|
162
|
+
|
|
163
|
+
return;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const {error} = event;
|
|
167
|
+
let message = 'Unknown uncaught error';
|
|
168
|
+
|
|
169
|
+
if (typeof error?.message === 'string') {
|
|
170
|
+
message = error.message;
|
|
171
|
+
} else if (typeof event.message === 'string') {
|
|
172
|
+
message = event.message;
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
this.capture({
|
|
176
|
+
column: event.colno,
|
|
177
|
+
filename: removeUrlDetails(event.filename),
|
|
178
|
+
kind: 'error',
|
|
179
|
+
line: event.lineno,
|
|
180
|
+
message,
|
|
181
|
+
name: typeof error?.name === 'string' ? error.name : 'Error',
|
|
182
|
+
stack: typeof error?.stack === 'string' ? error.stack : undefined,
|
|
183
|
+
});
|
|
184
|
+
} catch {
|
|
185
|
+
logTelemetryFailure(this.webex, 'Failed to extract an uncaught error.');
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
private captureRejection(event: PromiseRejectionEvent): void {
|
|
190
|
+
try {
|
|
191
|
+
const {reason} = event;
|
|
192
|
+
|
|
193
|
+
this.capture({
|
|
194
|
+
kind: 'unhandledrejection',
|
|
195
|
+
message: typeof reason?.message === 'string' ? reason.message : stringifyReason(reason),
|
|
196
|
+
name: typeof reason?.name === 'string' ? reason.name : 'UnhandledRejection',
|
|
197
|
+
stack: typeof reason?.stack === 'string' ? reason.stack : undefined,
|
|
198
|
+
});
|
|
199
|
+
} catch {
|
|
200
|
+
logTelemetryFailure(this.webex, 'Failed to extract an unhandled rejection.');
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
private capture(details: ErrorDetails): void {
|
|
205
|
+
try {
|
|
206
|
+
const error = {
|
|
207
|
+
...details,
|
|
208
|
+
name: truncate(removeUrlDetailsFromText(details.name), MAX_ERROR_NAME_LENGTH) ?? 'Error',
|
|
209
|
+
message: truncate(removeUrlDetailsFromText(details.message), MAX_ERROR_MESSAGE_LENGTH),
|
|
210
|
+
stack: truncate(removeUrlDetailsFromText(details.stack), MAX_STACK_LENGTH),
|
|
211
|
+
};
|
|
212
|
+
const fingerprint = createFingerprint(
|
|
213
|
+
[
|
|
214
|
+
error.kind,
|
|
215
|
+
error.name,
|
|
216
|
+
error.message,
|
|
217
|
+
error.stack,
|
|
218
|
+
error.filename,
|
|
219
|
+
error.line,
|
|
220
|
+
error.column,
|
|
221
|
+
error.resourceUrl,
|
|
222
|
+
].join('|')
|
|
223
|
+
);
|
|
224
|
+
const capturedAt = new Date().getTime();
|
|
225
|
+
const existingEvent = this.pendingEvents.get(fingerprint);
|
|
226
|
+
|
|
227
|
+
if (existingEvent) {
|
|
228
|
+
const isWithinDedupeWindow = capturedAt - existingEvent.capturedAt < DEDUPE_WINDOW_MS;
|
|
229
|
+
|
|
230
|
+
if (isWithinDedupeWindow) {
|
|
231
|
+
existingEvent.occurrenceCount += 1;
|
|
232
|
+
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// A throttled timer can leave an expired event pending. Its completed window must be
|
|
237
|
+
// submitted before this fingerprint is replaced with a new event and a new window.
|
|
238
|
+
this.pendingEvents.delete(fingerprint);
|
|
239
|
+
this.submit(existingEvent);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
const event: UnhandledExceptionEvent = {
|
|
243
|
+
schemaVersion: 1,
|
|
244
|
+
capturedAt,
|
|
245
|
+
eventId: uuid.v4(),
|
|
246
|
+
occurrenceCount: 1,
|
|
247
|
+
common: {
|
|
248
|
+
appName: this.webex.config?.appName ?? this.webex.config?.sdkType ?? 'webex-js-sdk',
|
|
249
|
+
appVersion: this.webex.config?.appVersion,
|
|
250
|
+
runtime: 'browser',
|
|
251
|
+
sdkVersion: this.webex.version,
|
|
252
|
+
},
|
|
253
|
+
error: {...error, fingerprint},
|
|
254
|
+
};
|
|
255
|
+
|
|
256
|
+
this.addMetadata(event);
|
|
257
|
+
this.pendingEvents.set(fingerprint, event);
|
|
258
|
+
|
|
259
|
+
if (this.flushTimer === undefined) {
|
|
260
|
+
this.flushTimer = safeSetTimeout(() => this.flush(), DEDUPE_WINDOW_MS);
|
|
261
|
+
}
|
|
262
|
+
} catch {
|
|
263
|
+
logTelemetryFailure(this.webex, 'Failed to capture an exception.');
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
private addMetadata(event: UnhandledExceptionEvent): void {
|
|
268
|
+
const getMetadata = this.webex.config?.metrics?.unhandledExceptionTelemetry?.getMetadata;
|
|
269
|
+
|
|
270
|
+
if (!getMetadata) {
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
try {
|
|
275
|
+
const metadata = getMetadata();
|
|
276
|
+
|
|
277
|
+
if (metadata === undefined) {
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const serializedMetadata = JSON.stringify(metadata);
|
|
282
|
+
|
|
283
|
+
if (serializedMetadata === undefined || serializedMetadata.length > MAX_METADATA_LENGTH) {
|
|
284
|
+
event.metadataCaptureStatus =
|
|
285
|
+
serializedMetadata === undefined ? 'invalid_type' : 'too_large';
|
|
286
|
+
|
|
287
|
+
return;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
const parsedMetadata = JSON.parse(serializedMetadata);
|
|
291
|
+
|
|
292
|
+
if (
|
|
293
|
+
parsedMetadata === null ||
|
|
294
|
+
typeof parsedMetadata !== 'object' ||
|
|
295
|
+
Array.isArray(parsedMetadata)
|
|
296
|
+
) {
|
|
297
|
+
event.metadataCaptureStatus = 'invalid_type';
|
|
298
|
+
|
|
299
|
+
return;
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
event.metadata = parsedMetadata;
|
|
303
|
+
} catch {
|
|
304
|
+
event.metadataCaptureStatus = 'provider_error';
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
private flush(): void {
|
|
309
|
+
const now = new Date().getTime();
|
|
310
|
+
let nextFlushInMs: number | undefined;
|
|
311
|
+
|
|
312
|
+
this.clearFlushTimer();
|
|
313
|
+
this.pendingEvents.forEach((event, fingerprint) => {
|
|
314
|
+
const remainingWindowMs = DEDUPE_WINDOW_MS - (now - event.capturedAt);
|
|
315
|
+
|
|
316
|
+
if (remainingWindowMs <= 0) {
|
|
317
|
+
this.pendingEvents.delete(fingerprint);
|
|
318
|
+
this.submit(event);
|
|
319
|
+
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
nextFlushInMs = Math.min(nextFlushInMs ?? remainingWindowMs, remainingWindowMs);
|
|
324
|
+
});
|
|
325
|
+
|
|
326
|
+
if (nextFlushInMs !== undefined) {
|
|
327
|
+
this.flushTimer = safeSetTimeout(() => this.flush(), nextFlushInMs);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
private submit(event: UnhandledExceptionEvent): void {
|
|
332
|
+
const submitClientMetrics = this.webex.internal?.metrics?.submitClientMetrics;
|
|
333
|
+
|
|
334
|
+
if (!submitClientMetrics) {
|
|
335
|
+
logTelemetryFailure(this.webex, 'submitClientMetrics is unavailable.');
|
|
336
|
+
|
|
337
|
+
return;
|
|
338
|
+
}
|
|
339
|
+
|
|
340
|
+
try {
|
|
341
|
+
Promise.resolve(
|
|
342
|
+
submitClientMetrics.call(
|
|
343
|
+
this.webex.internal?.metrics,
|
|
344
|
+
UNHANDLED_EXCEPTION_METRIC_NAME,
|
|
345
|
+
{
|
|
346
|
+
tags: {
|
|
347
|
+
app_name: event.common.appName,
|
|
348
|
+
exception_kind: event.error.kind,
|
|
349
|
+
runtime: event.common.runtime,
|
|
350
|
+
},
|
|
351
|
+
fields: {
|
|
352
|
+
captured_at: event.capturedAt,
|
|
353
|
+
error_fingerprint: event.error.fingerprint,
|
|
354
|
+
error_name: event.error.name,
|
|
355
|
+
event_id: event.eventId,
|
|
356
|
+
occurrence_count: event.occurrenceCount,
|
|
357
|
+
},
|
|
358
|
+
eventPayload: event,
|
|
359
|
+
},
|
|
360
|
+
this.webex.canAuthorize === true ? undefined : this.preLoginId
|
|
361
|
+
)
|
|
362
|
+
).catch(() => {
|
|
363
|
+
logTelemetryFailure(this.webex, 'Failed to submit exception telemetry.');
|
|
364
|
+
});
|
|
365
|
+
} catch {
|
|
366
|
+
logTelemetryFailure(this.webex, 'Failed to submit exception telemetry.');
|
|
367
|
+
}
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
private clearFlushTimer(): void {
|
|
371
|
+
if (this.flushTimer !== undefined) {
|
|
372
|
+
clearTimeout(this.flushTimer);
|
|
373
|
+
this.flushTimer = undefined;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
let activeTelemetry: UnhandledExceptionTelemetry | undefined;
|
|
379
|
+
|
|
380
|
+
/**
|
|
381
|
+
* Starts browser exception telemetry after SDK readiness.
|
|
382
|
+
* @param webex Initialized SDK instance.
|
|
383
|
+
* @returns {void}
|
|
384
|
+
*/
|
|
385
|
+
export function startUnhandledExceptionTelemetry(webex: WebexForUnhandledExceptionTelemetry): void {
|
|
386
|
+
const shouldStart =
|
|
387
|
+
webex.config?.metrics?.unhandledExceptionTelemetry?.enabled === true &&
|
|
388
|
+
typeof window !== 'undefined' &&
|
|
389
|
+
typeof window.addEventListener === 'function';
|
|
390
|
+
|
|
391
|
+
activeTelemetry?.stop(shouldStart);
|
|
392
|
+
activeTelemetry = undefined;
|
|
393
|
+
|
|
394
|
+
if (!shouldStart) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
try {
|
|
399
|
+
activeTelemetry = new UnhandledExceptionTelemetry(webex, window);
|
|
400
|
+
} catch {
|
|
401
|
+
logTelemetryFailure(webex, 'Failed to start exception telemetry.');
|
|
402
|
+
}
|
|
403
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/* eslint-disable require-jsdoc */
|
|
2
|
+
|
|
3
|
+
const MAX_RESOURCE_URL_LENGTH = 2_048;
|
|
4
|
+
|
|
5
|
+
export function truncate(value: string | undefined, maxLength: number): string | undefined {
|
|
6
|
+
return value?.slice(0, maxLength);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function removeUrlDetails(value: unknown): string | undefined {
|
|
10
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
const withoutQueryOrFragment = value.split(/[?#]/, 1)[0];
|
|
15
|
+
|
|
16
|
+
// Strip URL userinfo from the authority. For example,
|
|
17
|
+
// https://userinfo@host/path becomes https://host/path.
|
|
18
|
+
return withoutQueryOrFragment.replace(/^((?:[a-z][a-z0-9+.-]*:)?\/\/)[^/?#]*@/i, '$1');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// Keep HTTP(S) and relative resource URLs, remove credentials/query/fragment details, and cap them.
|
|
22
|
+
// For example, https://host/app.js?token=x becomes https://host/app.js, while
|
|
23
|
+
// data:image/svg+xml,<svg>...</svg> is rejected because its scheme is not HTTP(S).
|
|
24
|
+
export function sanitizeResourceUrl(value: unknown): string | undefined {
|
|
25
|
+
if (typeof value !== 'string' || value.length === 0) {
|
|
26
|
+
return undefined;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const url = value.trim();
|
|
30
|
+
const scheme = /^([a-z][a-z0-9+.-]*):/i.exec(url)?.[1]?.toLowerCase();
|
|
31
|
+
|
|
32
|
+
if (scheme && scheme !== 'http' && scheme !== 'https') {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
return truncate(removeUrlDetails(url), MAX_RESOURCE_URL_LENGTH);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function removeUrlDetailsFromText(value: string | undefined): string | undefined {
|
|
40
|
+
// Redact non-HTTP(S) schemes and strip credentials, queries, and fragments from other URLs.
|
|
41
|
+
// Examples: data:text/plain,secret -> [redacted-url], https://host/api?token=x ->
|
|
42
|
+
// https://host/api, api/messages?token=x -> api/messages, and GET app.js?token=x -> GET app.js.
|
|
43
|
+
return (
|
|
44
|
+
value
|
|
45
|
+
// Handles root and dot-relative paths such as /api?token=x and ../app.js#fragment.
|
|
46
|
+
// These run before the scheme matcher so query values such as x:10 are not treated as URLs.
|
|
47
|
+
?.replace(
|
|
48
|
+
/(^|[\s("'=[{])((?:\/|\.\.?\/)[^\s)"'\]}]+)/g,
|
|
49
|
+
(_, prefix, url) => `${prefix}${removeUrlDetails(url) ?? url}`
|
|
50
|
+
)
|
|
51
|
+
.replace(
|
|
52
|
+
// Handles bare paths with a slash or file extension, such as api/messages?x=1 or app.js#x.
|
|
53
|
+
/(^|[\s("'=[{])((?:(?:[a-z0-9._~%-]+\/)+(?:[a-z0-9._~%-]+)?|[a-z0-9._~%-]+\.[a-z0-9._~%-]+)[?#][^\s)"'\]}]+)/gi,
|
|
54
|
+
(_, prefix, url) => `${prefix}${removeUrlDetails(url) ?? url}`
|
|
55
|
+
)
|
|
56
|
+
.replace(
|
|
57
|
+
// Handles extensionless single-segment paths after an HTTP method, such as GET api?token=x.
|
|
58
|
+
/\b(DELETE|GET|HEAD|OPTIONS|PATCH|POST|PUT)(\s+)([a-z0-9._~%-]+[?#][^\s)"'\]}]+)/gi,
|
|
59
|
+
(_, method, spacing, url) => `${method}${spacing}${removeUrlDetails(url) ?? url}`
|
|
60
|
+
)
|
|
61
|
+
.replace(
|
|
62
|
+
// Handles URLs with a scheme, including assigned values such as url=https://host/path?x=1.
|
|
63
|
+
// HTTP(S) details are stripped; data:, blob:, and other schemes are fully redacted.
|
|
64
|
+
/(^|[\s("'=[{,;])(([a-z][a-z0-9+.-]*):[^\s)"'\]}]+)/gi,
|
|
65
|
+
(_, prefix, url, scheme) =>
|
|
66
|
+
`${prefix}${
|
|
67
|
+
['http', 'https'].includes(scheme.toLowerCase())
|
|
68
|
+
? removeUrlDetails(url) ?? url
|
|
69
|
+
: '[redacted-url]'
|
|
70
|
+
}`
|
|
71
|
+
)
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export function createFingerprint(value: string): string {
|
|
76
|
+
// DJB2-style hash: multiply by 33, add each UTF-16 code unit, and constrain the result to 32 bits.
|
|
77
|
+
// It provides a small stable deduplication key; it is not intended for cryptographic use.
|
|
78
|
+
let hash = 5381;
|
|
79
|
+
|
|
80
|
+
for (let index = 0; index < value.length; index += 1) {
|
|
81
|
+
hash = (hash * 33 + value.charCodeAt(index)) % 4_294_967_296;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return hash.toString(16).padStart(8, '0');
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function stringifyReason(reason: unknown): string {
|
|
88
|
+
if (typeof reason === 'string') {
|
|
89
|
+
return reason;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
try {
|
|
93
|
+
return JSON.stringify(reason) ?? String(reason);
|
|
94
|
+
} catch {
|
|
95
|
+
try {
|
|
96
|
+
return String(reason);
|
|
97
|
+
} catch {
|
|
98
|
+
return 'Unserializable rejection reason';
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
@@ -8,11 +8,14 @@ import {
|
|
|
8
8
|
import MockWebex from '@webex/test-helper-mock-webex';
|
|
9
9
|
import sinon from 'sinon';
|
|
10
10
|
|
|
11
|
+
import SourceNewMetrics from '@webex/internal-plugin-metrics/src/new-metrics';
|
|
12
|
+
import * as Telemetry from '@webex/internal-plugin-metrics/src/unhandled-exception-telemetry';
|
|
13
|
+
|
|
11
14
|
describe('internal-plugin-metrics', () => {
|
|
12
|
-
const mockWebex = () =>
|
|
15
|
+
const mockWebex = (NewMetricsPlugin = NewMetrics) =>
|
|
13
16
|
new MockWebex({
|
|
14
17
|
children: {
|
|
15
|
-
newMetrics:
|
|
18
|
+
newMetrics: NewMetricsPlugin,
|
|
16
19
|
},
|
|
17
20
|
meetings: {
|
|
18
21
|
getBasicMeetingInformation: sinon.stub().callsFake((meetingId) => ({
|
|
@@ -73,6 +76,10 @@ describe('internal-plugin-metrics', () => {
|
|
|
73
76
|
});
|
|
74
77
|
|
|
75
78
|
describe('new-metrics contstructor', () => {
|
|
79
|
+
afterEach(() => {
|
|
80
|
+
sinon.restore();
|
|
81
|
+
});
|
|
82
|
+
|
|
76
83
|
it('checks callDiagnosticLatencies is defined before ready emit', () => {
|
|
77
84
|
const webex = mockWebex();
|
|
78
85
|
|
|
@@ -87,10 +94,12 @@ describe('internal-plugin-metrics', () => {
|
|
|
87
94
|
|
|
88
95
|
it('can call buildClientEventFetchRequestOptions before ready', async () => {
|
|
89
96
|
const webex = mockWebex();
|
|
90
|
-
const stub = sinon
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
97
|
+
const stub = sinon
|
|
98
|
+
.stub(
|
|
99
|
+
webex.internal.newMetrics.callDiagnosticMetrics,
|
|
100
|
+
'buildClientEventFetchRequestOptions'
|
|
101
|
+
)
|
|
102
|
+
.resolves({url: 'https://metrics.example.com'});
|
|
94
103
|
|
|
95
104
|
const result = await webex.internal.newMetrics.buildClientEventFetchRequestOptions({
|
|
96
105
|
name: 'client.alert.displayed',
|
|
@@ -104,6 +113,17 @@ describe('internal-plugin-metrics', () => {
|
|
|
104
113
|
});
|
|
105
114
|
assert.deepEqual(result, {url: 'https://metrics.example.com'});
|
|
106
115
|
});
|
|
116
|
+
|
|
117
|
+
it('starts unhandled exception telemetry when webex is ready', () => {
|
|
118
|
+
const start = sinon.stub(Telemetry, 'startUnhandledExceptionTelemetry');
|
|
119
|
+
const webex = mockWebex(SourceNewMetrics);
|
|
120
|
+
|
|
121
|
+
assert.notCalled(start);
|
|
122
|
+
|
|
123
|
+
webex.emit('ready');
|
|
124
|
+
|
|
125
|
+
assert.calledOnceWithExactly(start, webex);
|
|
126
|
+
});
|
|
107
127
|
});
|
|
108
128
|
|
|
109
129
|
describe('new-metrics', () => {
|
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import {assert} from '@webex/test-helper-chai';
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
createFingerprint,
|
|
5
|
+
removeUrlDetails,
|
|
6
|
+
removeUrlDetailsFromText,
|
|
7
|
+
sanitizeResourceUrl,
|
|
8
|
+
stringifyReason,
|
|
9
|
+
truncate,
|
|
10
|
+
} from '@webex/internal-plugin-metrics/src/unhandled-exception-telemetry/utils';
|
|
11
|
+
|
|
12
|
+
const TEST_USERINFO = ['fixture-user', 'fixture-value'].join(':');
|
|
13
|
+
|
|
14
|
+
describe('Unhandled exception telemetry utilities', () => {
|
|
15
|
+
describe('#truncate()', () => {
|
|
16
|
+
it('truncates strings to the requested length', () => {
|
|
17
|
+
assert.equal(truncate('abcdef', 3), 'abc');
|
|
18
|
+
assert.equal(truncate('abc', 3), 'abc');
|
|
19
|
+
assert.isUndefined(truncate(undefined, 3));
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
describe('#removeUrlDetails()', () => {
|
|
24
|
+
[
|
|
25
|
+
{
|
|
26
|
+
input: `https://${TEST_USERINFO}@example.test/path?secret=x#fragment`,
|
|
27
|
+
expected: 'https://example.test/path',
|
|
28
|
+
},
|
|
29
|
+
{input: `//${TEST_USERINFO}@example.test/path#fragment`, expected: '//example.test/path'},
|
|
30
|
+
{input: '/api/messages?secret=x', expected: '/api/messages'},
|
|
31
|
+
{input: '', expected: undefined},
|
|
32
|
+
{input: 42, expected: undefined},
|
|
33
|
+
].forEach(({input, expected}) => {
|
|
34
|
+
it(`sanitizes ${JSON.stringify(input)}`, () => {
|
|
35
|
+
assert.strictEqual(removeUrlDetails(input), expected);
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
describe('#sanitizeResourceUrl()', () => {
|
|
41
|
+
[
|
|
42
|
+
{
|
|
43
|
+
input: ` https://${TEST_USERINFO}@example.test/app.js?secret=x `,
|
|
44
|
+
expected: 'https://example.test/app.js',
|
|
45
|
+
},
|
|
46
|
+
{input: '/assets/app.js?secret=x', expected: '/assets/app.js'},
|
|
47
|
+
{input: 'data:text/plain,secret', expected: undefined},
|
|
48
|
+
{input: 'blob:https://example.test/id', expected: undefined},
|
|
49
|
+
{input: undefined, expected: undefined},
|
|
50
|
+
].forEach(({input, expected}) => {
|
|
51
|
+
it(`sanitizes ${JSON.stringify(input)}`, () => {
|
|
52
|
+
assert.strictEqual(sanitizeResourceUrl(input), expected);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('caps resource URLs at 2048 characters', () => {
|
|
57
|
+
assert.lengthOf(sanitizeResourceUrl(`/${'x'.repeat(3_000)}`) as string, 2_048);
|
|
58
|
+
});
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
describe('#removeUrlDetailsFromText()', () => {
|
|
62
|
+
[
|
|
63
|
+
{
|
|
64
|
+
input: `url=https://${TEST_USERINFO}@example.test/path?secret=x`,
|
|
65
|
+
expected: 'url=https://example.test/path',
|
|
66
|
+
},
|
|
67
|
+
{input: 'payload=data:text/plain,secret', expected: 'payload=[redacted-url]'},
|
|
68
|
+
{input: 'GET /api/messages?secret=x', expected: 'GET /api/messages'},
|
|
69
|
+
{input: 'at load (../app.js#fragment:10:20)', expected: 'at load (../app.js)'},
|
|
70
|
+
{input: 'retry api/messages?secret=x', expected: 'retry api/messages'},
|
|
71
|
+
{input: 'at load (app.js?secret=x:10:20)', expected: 'at load (app.js)'},
|
|
72
|
+
{input: 'GET api?secret=x', expected: 'GET api'},
|
|
73
|
+
].forEach(({input, expected}) => {
|
|
74
|
+
it(`sanitizes ${input}`, () => {
|
|
75
|
+
assert.equal(removeUrlDetailsFromText(input), expected);
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
describe('#createFingerprint()', () => {
|
|
81
|
+
it('returns a stable eight-character key that changes with its input', () => {
|
|
82
|
+
const fingerprint = createFingerprint('same error');
|
|
83
|
+
|
|
84
|
+
assert.lengthOf(fingerprint, 8);
|
|
85
|
+
assert.equal(createFingerprint('same error'), fingerprint);
|
|
86
|
+
assert.notEqual(createFingerprint('different error'), fingerprint);
|
|
87
|
+
});
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
describe('#stringifyReason()', () => {
|
|
91
|
+
it('serializes strings and objects', () => {
|
|
92
|
+
assert.equal(stringifyReason('failed'), 'failed');
|
|
93
|
+
assert.equal(stringifyReason({message: 'failed'}), '{"message":"failed"}');
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('falls back safely when conversion hooks throw', () => {
|
|
97
|
+
const reason = new Proxy(
|
|
98
|
+
{},
|
|
99
|
+
{
|
|
100
|
+
get() {
|
|
101
|
+
throw new Error('unreadable');
|
|
102
|
+
},
|
|
103
|
+
}
|
|
104
|
+
);
|
|
105
|
+
|
|
106
|
+
assert.equal(stringifyReason(reason), 'Unserializable rejection reason');
|
|
107
|
+
});
|
|
108
|
+
});
|
|
109
|
+
});
|