@grafana/faro-web-sdk
Version:
Faro instrumentations, metas, transports for web.
65 lines • 4.09 kB
JavaScript
import { BaseInstrumentation, defaultErrorArgsSerializer, defaultLogArgsSerializer, LogLevel, VERSION, } from '@grafana/faro-core';
import { monitorConsole } from '../_internal/monitors/consoleMonitor';
import { getDetailsFromConsoleErrorArgs } from '../errors/getErrorDetails';
export class ConsoleInstrumentation extends BaseInstrumentation {
constructor() {
super(...arguments);
this.name = '@grafana/faro-web-sdk:instrumentation-console';
this.version = VERSION;
this.errorSerializer = defaultLogArgsSerializer;
}
initialize() {
var _a, _b;
const instrumentationOptions = this.config.consoleInstrumentation;
const serializeErrors = (instrumentationOptions === null || instrumentationOptions === void 0 ? void 0 : instrumentationOptions.serializeErrors) || !!(instrumentationOptions === null || instrumentationOptions === void 0 ? void 0 : instrumentationOptions.errorSerializer);
this.errorSerializer = serializeErrors
? ((_a = instrumentationOptions === null || instrumentationOptions === void 0 ? void 0 : instrumentationOptions.errorSerializer) !== null && _a !== void 0 ? _a : defaultErrorArgsSerializer)
: defaultLogArgsSerializer;
const disabledLevels = (_b = instrumentationOptions === null || instrumentationOptions === void 0 ? void 0 : instrumentationOptions.disabledLevels) !== null && _b !== void 0 ? _b : ConsoleInstrumentation.defaultDisabledLevels;
// Pass unpatchedConsole to the monitor (only first caller's value is used)
const consoleMonitor = monitorConsole(this.unpatchedConsole);
// Subscribe this Faro instance to console events
this.subscription = consoleMonitor.subscribe(({ level, args }) => {
// Skip if this level is disabled for this instance
if (disabledLevels.includes(level)) {
return;
}
try {
if (level === LogLevel.ERROR && !(instrumentationOptions === null || instrumentationOptions === void 0 ? void 0 : instrumentationOptions.consoleErrorAsLog)) {
const { value, type, stackFrames } = getDetailsFromConsoleErrorArgs(args, this.errorSerializer);
if (value && !type && !stackFrames) {
this.api.pushError(new Error(ConsoleInstrumentation.consoleErrorPrefix + value));
return;
}
this.api.pushError(new Error(ConsoleInstrumentation.consoleErrorPrefix + value), { type, stackFrames });
}
else if (level === LogLevel.ERROR && (instrumentationOptions === null || instrumentationOptions === void 0 ? void 0 : instrumentationOptions.consoleErrorAsLog)) {
const { value, type, stackFrames } = getDetailsFromConsoleErrorArgs(args, this.errorSerializer);
this.api.pushLog(value ? [ConsoleInstrumentation.consoleErrorPrefix + value] : args, {
level,
context: {
value: value !== null && value !== void 0 ? value : '',
type: type !== null && type !== void 0 ? type : '',
stackFrames: (stackFrames === null || stackFrames === void 0 ? void 0 : stackFrames.length) ? defaultErrorArgsSerializer(stackFrames) : '',
},
});
}
else {
this.api.pushLog(args, { level });
}
}
catch (err) {
this.logError(err);
}
});
}
// Clean up subscription when instrumentation is destroyed
destroy() {
var _a;
(_a = this.subscription) === null || _a === void 0 ? void 0 : _a.unsubscribe();
this.subscription = undefined;
}
}
ConsoleInstrumentation.defaultDisabledLevels = [LogLevel.DEBUG, LogLevel.TRACE, LogLevel.LOG];
ConsoleInstrumentation.consoleErrorPrefix = 'console.error: ';
//# sourceMappingURL=instrumentation.js.map