UNPKG

@grafana/faro-web-sdk

Version:

Faro instrumentations, metas, transports for web.

69 lines 4.38 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConsoleInstrumentation = void 0; const faro_core_1 = require("@grafana/faro-core"); const consoleMonitor_1 = require("../_internal/monitors/consoleMonitor"); const getErrorDetails_1 = require("../errors/getErrorDetails"); class ConsoleInstrumentation extends faro_core_1.BaseInstrumentation { constructor() { super(...arguments); this.name = '@grafana/faro-web-sdk:instrumentation-console'; this.version = faro_core_1.VERSION; this.errorSerializer = faro_core_1.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 : faro_core_1.defaultErrorArgsSerializer) : faro_core_1.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 = (0, consoleMonitor_1.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 === faro_core_1.LogLevel.ERROR && !(instrumentationOptions === null || instrumentationOptions === void 0 ? void 0 : instrumentationOptions.consoleErrorAsLog)) { const { value, type, stackFrames } = (0, getErrorDetails_1.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 === faro_core_1.LogLevel.ERROR && (instrumentationOptions === null || instrumentationOptions === void 0 ? void 0 : instrumentationOptions.consoleErrorAsLog)) { const { value, type, stackFrames } = (0, getErrorDetails_1.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) ? (0, faro_core_1.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; } } exports.ConsoleInstrumentation = ConsoleInstrumentation; ConsoleInstrumentation.defaultDisabledLevels = [faro_core_1.LogLevel.DEBUG, faro_core_1.LogLevel.TRACE, faro_core_1.LogLevel.LOG]; ConsoleInstrumentation.consoleErrorPrefix = 'console.error: '; //# sourceMappingURL=instrumentation.js.map