UNPKG

@elastic/apm-rum

Version:
255 lines (205 loc) 8.59 kB
"use strict"; exports.__esModule = true; exports.default = void 0; var _apmRumCore = require("@elastic/apm-rum-core"); var ApmBase = function () { function ApmBase(serviceFactory, disable) { this._disable = disable; this.serviceFactory = serviceFactory; this._initialized = false; } var _proto = ApmBase.prototype; _proto.isEnabled = function isEnabled() { return !this._disable; }; _proto.isActive = function isActive() { var configService = this.serviceFactory.getService(_apmRumCore.CONFIG_SERVICE); return this.isEnabled() && this._initialized && configService.get('active'); }; _proto.init = function init(config) { var _this = this; if (this.isEnabled() && !this._initialized) { this._initialized = true; var _this$serviceFactory$ = this.serviceFactory.getService([_apmRumCore.CONFIG_SERVICE, _apmRumCore.LOGGING_SERVICE, _apmRumCore.TRANSACTION_SERVICE]), configService = _this$serviceFactory$[0], loggingService = _this$serviceFactory$[1], transactionService = _this$serviceFactory$[2]; configService.setVersion('5.17.0'); this.config(config); var logLevel = configService.get('logLevel'); loggingService.setLevel(logLevel); var isConfigActive = configService.get('active'); if (isConfigActive) { this.serviceFactory.init(); var flags = (0, _apmRumCore.getInstrumentationFlags)(configService.get('instrument'), configService.get('disableInstrumentations')); var performanceMonitoring = this.serviceFactory.getService(_apmRumCore.PERFORMANCE_MONITORING); performanceMonitoring.init(flags); if (flags[_apmRumCore.ERROR]) { var errorLogging = this.serviceFactory.getService(_apmRumCore.ERROR_LOGGING); errorLogging.registerListeners(); } if (configService.get('session')) { var localConfig = configService.getLocalConfig(); if (localConfig && localConfig.session) { configService.setConfig({ session: localConfig.session }); } } var sendPageLoad = function sendPageLoad() { return flags[_apmRumCore.PAGE_LOAD] && _this._sendPageLoadMetrics(); }; if (configService.get('centralConfig')) { this.fetchCentralConfig().then(sendPageLoad); } else { sendPageLoad(); } (0, _apmRumCore.observePageVisibility)(configService, transactionService); if (flags[_apmRumCore.EVENT_TARGET] && flags[_apmRumCore.CLICK]) { (0, _apmRumCore.observePageClicks)(transactionService); } (0, _apmRumCore.observeUserInteractions)(); } else { this._disable = true; loggingService.warn('RUM agent is inactive'); } } return this; }; _proto.fetchCentralConfig = function fetchCentralConfig() { var _this$serviceFactory$2 = this.serviceFactory.getService([_apmRumCore.APM_SERVER, _apmRumCore.LOGGING_SERVICE, _apmRumCore.CONFIG_SERVICE]), apmServer = _this$serviceFactory$2[0], loggingService = _this$serviceFactory$2[1], configService = _this$serviceFactory$2[2]; return apmServer.fetchConfig(configService.get('serviceName'), configService.get('environment')).then(function (config) { var transactionSampleRate = config['transaction_sample_rate']; if (transactionSampleRate) { transactionSampleRate = Number(transactionSampleRate); var _config2 = { transactionSampleRate: transactionSampleRate }; var _configService$valida = configService.validate(_config2), invalid = _configService$valida.invalid; if (invalid.length === 0) { configService.setConfig(_config2); } else { var _invalid$ = invalid[0], key = _invalid$.key, value = _invalid$.value, allowed = _invalid$.allowed; loggingService.warn("invalid value \"" + value + "\" for " + key + ". Allowed: " + allowed + "."); } } return config; }).catch(function (error) { loggingService.warn('failed fetching config:', error); }); }; _proto._sendPageLoadMetrics = function _sendPageLoadMetrics() { var tr = this.startTransaction(undefined, _apmRumCore.PAGE_LOAD, { managed: true, canReuse: true }); if (!tr) { return; } tr.addTask(_apmRumCore.PAGE_LOAD); var sendPageLoadMetrics = function sendPageLoadMetrics() { setTimeout(function () { return tr.removeTask(_apmRumCore.PAGE_LOAD); }, _apmRumCore.PAGE_LOAD_DELAY); }; if (document.readyState === 'complete') { sendPageLoadMetrics(); } else { window.addEventListener('load', sendPageLoadMetrics); } }; _proto.observe = function observe(name, fn) { var configService = this.serviceFactory.getService(_apmRumCore.CONFIG_SERVICE); configService.events.observe(name, fn); }; _proto.config = function config(_config) { var _this$serviceFactory$3 = this.serviceFactory.getService([_apmRumCore.CONFIG_SERVICE, _apmRumCore.LOGGING_SERVICE]), configService = _this$serviceFactory$3[0], loggingService = _this$serviceFactory$3[1]; var _configService$valida2 = configService.validate(_config), missing = _configService$valida2.missing, invalid = _configService$valida2.invalid, unknown = _configService$valida2.unknown; if (unknown.length > 0) { var message = 'Unknown config options are specified for RUM agent: ' + unknown.join(', '); loggingService.warn(message); } if (missing.length === 0 && invalid.length === 0) { configService.setConfig(_config); } else { var separator = ', '; var _message = "RUM agent isn't correctly configured. "; if (missing.length > 0) { _message += missing.join(separator) + ' is missing'; if (invalid.length > 0) { _message += separator; } } invalid.forEach(function (_ref, index) { var key = _ref.key, value = _ref.value, allowed = _ref.allowed; _message += key + " \"" + value + "\" contains invalid characters! (allowed: " + allowed + ")" + (index !== invalid.length - 1 ? separator : ''); }); loggingService.error(_message); configService.setConfig({ active: false }); } }; _proto.setUserContext = function setUserContext(userContext) { var configService = this.serviceFactory.getService(_apmRumCore.CONFIG_SERVICE); configService.setUserContext(userContext); }; _proto.setCustomContext = function setCustomContext(customContext) { var configService = this.serviceFactory.getService(_apmRumCore.CONFIG_SERVICE); configService.setCustomContext(customContext); }; _proto.addLabels = function addLabels(labels) { var configService = this.serviceFactory.getService(_apmRumCore.CONFIG_SERVICE); configService.addLabels(labels); }; _proto.setInitialPageLoadName = function setInitialPageLoadName(name) { var configService = this.serviceFactory.getService(_apmRumCore.CONFIG_SERVICE); configService.setConfig({ pageLoadTransactionName: name }); }; _proto.startTransaction = function startTransaction(name, type, options) { if (this.isEnabled()) { var transactionService = this.serviceFactory.getService(_apmRumCore.TRANSACTION_SERVICE); return transactionService.startTransaction(name, type, options); } }; _proto.startSpan = function startSpan(name, type, options) { if (this.isEnabled()) { var transactionService = this.serviceFactory.getService(_apmRumCore.TRANSACTION_SERVICE); return transactionService.startSpan(name, type, options); } }; _proto.getCurrentTransaction = function getCurrentTransaction() { if (this.isEnabled()) { var transactionService = this.serviceFactory.getService(_apmRumCore.TRANSACTION_SERVICE); return transactionService.getCurrentTransaction(); } }; _proto.captureError = function captureError(error, opts) { if (this.isEnabled()) { var errorLogging = this.serviceFactory.getService(_apmRumCore.ERROR_LOGGING); return errorLogging.logError(error, opts); } }; _proto.addFilter = function addFilter(fn) { var configService = this.serviceFactory.getService(_apmRumCore.CONFIG_SERVICE); configService.addFilter(fn); }; return ApmBase; }(); exports.default = ApmBase;