@elastic/apm-rum
Version:
Elastic APM JavaScript agent
226 lines (187 loc) • 7.37 kB
JavaScript
import { getInstrumentationFlags, PAGE_LOAD, ERROR } from '@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.init = function init(config) {
var _this = this;
if (this.isEnabled() && !this._initialized) {
this._initialized = true;
var configService = this.serviceFactory.getService('ConfigService');
configService.setVersion('4.5.1');
this.config(config);
var loggingService = this.serviceFactory.getService('LoggingService');
if (configService.isActive()) {
this.serviceFactory.init();
var flags = getInstrumentationFlags(configService.get('instrument'), configService.get('disableInstrumentations'));
var performanceMonitoring = this.serviceFactory.getService('PerformanceMonitoring');
performanceMonitoring.init(flags);
if (flags[ERROR]) {
var errorLogging = this.serviceFactory.getService('ErrorLogging');
errorLogging.registerListeners();
}
var sendPageLoad = function sendPageLoad() {
return flags[PAGE_LOAD] && _this._sendPageLoadMetrics();
};
if (configService.get('centralConfig')) {
this.fetchCentralConfig().then(sendPageLoad);
} else {
sendPageLoad();
}
} else {
loggingService.info('RUM agent is inactive');
}
}
return this;
};
_proto.fetchCentralConfig = function fetchCentralConfig() {
var apmServer = this.serviceFactory.getService('ApmServer');
var loggingService = this.serviceFactory.getService('LoggingService');
var configService = this.serviceFactory.getService('ConfigService');
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 transactionService = this.serviceFactory.getService('TransactionService');
var tr = transactionService.startTransaction(undefined, PAGE_LOAD, {
managed: true,
canReuse: true
});
if (tr) {
tr.addTask(PAGE_LOAD);
}
var sendPageLoadMetrics = function sendPageLoadMetrics() {
setTimeout(function () {
if (tr) {
tr.removeTask(PAGE_LOAD);
}
});
};
if (document.readyState === 'complete') {
sendPageLoadMetrics();
} else {
window.addEventListener('load', sendPageLoadMetrics);
}
};
_proto.isEnabled = function isEnabled() {
return !this._disable;
};
_proto.observe = function observe(name, fn) {
var configService = this.serviceFactory.getService('ConfigService');
configService.events.observe(name, fn);
};
_proto.config = function config(_config) {
var configService = this.serviceFactory.getService('ConfigService');
var _configService$valida2 = configService.validate(_config),
missing = _configService$valida2.missing,
invalid = _configService$valida2.invalid;
if (missing.length === 0 && invalid.length === 0) {
configService.setConfig(_config);
} else {
var loggingService = this.serviceFactory.getService('LoggingService');
var separator = ', ';
var message = "RUM Agent isn't correctly configured: ";
if (missing.length > 0) {
message += 'Missing config - ' + missing.join(separator);
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('ConfigService');
configService.setUserContext(userContext);
};
_proto.setCustomContext = function setCustomContext(customContext) {
var configService = this.serviceFactory.getService('ConfigService');
configService.setCustomContext(customContext);
};
_proto.addTags = function addTags(tags) {
var loggingService = this.serviceFactory.getService('LoggingService');
loggingService.warn('addTags deprecated, please use addLabels');
this.addLabels(tags);
};
_proto.addLabels = function addLabels(labels) {
var configService = this.serviceFactory.getService('ConfigService');
configService.addLabels(labels);
};
_proto.setInitialPageLoadName = function setInitialPageLoadName(name) {
if (this.isEnabled()) {
var configService = this.serviceFactory.getService('ConfigService');
configService.setConfig({
pageLoadTransactionName: name
});
}
};
_proto.startTransaction = function startTransaction(name, type, options) {
if (this.isEnabled()) {
var transactionService = this.serviceFactory.getService('TransactionService');
return transactionService.startTransaction(name, type, options);
}
};
_proto.startSpan = function startSpan(name, type) {
if (this.isEnabled()) {
var transactionService = this.serviceFactory.getService('TransactionService');
return transactionService.startSpan(name, type);
}
};
_proto.getCurrentTransaction = function getCurrentTransaction() {
if (this.isEnabled()) {
var transactionService = this.serviceFactory.getService('TransactionService');
return transactionService.getCurrentTransaction();
}
};
_proto.getTransactionService = function getTransactionService() {
if (this.isEnabled()) {
var transactionService = this.serviceFactory.getService('TransactionService');
return transactionService;
}
};
_proto.captureError = function captureError(error) {
if (this.isEnabled()) {
var errorLogging = this.serviceFactory.getService('ErrorLogging');
return errorLogging.logError(error);
}
};
_proto.addFilter = function addFilter(fn) {
var configService = this.serviceFactory.getService('ConfigService');
configService.addFilter(fn);
};
return ApmBase;
}();
export default ApmBase;