UNPKG

backtrace-node

Version:
147 lines 6.86 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var tslib_1 = require("tslib"); var utils_1 = require("../utils"); /** * Handles Backtrace Metrics. */ var BacktraceMetrics = /** @class */ (function () { function BacktraceMetrics(configuration, attributeProvider, _logger) { var _a; this.attributeProvider = attributeProvider; this._logger = _logger; this.sessionId = utils_1.uuid(); if (!configuration.endpoint) { throw new Error("Backtrace: missing 'endpoint' option."); } var endpointParameters = utils_1.getEndpointParams(configuration.endpoint, configuration.token); if (!endpointParameters) { throw new Error("Invalid Backtrace submission parameters. Cannot create a submission URL to metrics support"); } var universe = endpointParameters.universe, token = endpointParameters.token; if (!universe) { throw new Error("Backtrace: 'universe' could not be parsed from the endpoint."); } if (!token) { throw new Error("Backtrace: missing 'token' option or it could not be parsed from the endpoint."); } this.universe = universe; this.token = token; this.hostname = (_a = configuration.metricsSubmissionUrl, (_a !== null && _a !== void 0 ? _a : 'https://events.backtrace.io')); this.summedEndpoint = this.hostname + "/api/summed-events/submit?universe=" + this.universe + "&token=" + this.token; this.uniqueEndpoint = this.hostname + "/api/unique-events/submit?universe=" + this.universe + "&token=" + this.token; this.handleSession(); } /** * Handle persisting of session. When called, will create a new session. */ BacktraceMetrics.prototype.handleSession = function () { // If sessionId is not set, create new session. Send unique and app launch events. this.sendUniqueEvent(); this.sendSummedEvent('Application Launches'); }; /** * Send POST to unique-events API endpoint */ BacktraceMetrics.prototype.sendUniqueEvent = function () { var _a, _b; return tslib_1.__awaiter(this, void 0, void 0, function () { var attributes, payload, e_1; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: attributes = this.getEventAttributes(); payload = { application: attributes.application, appversion: attributes['application.version'], metadata: { dropped_events: 0, }, unique_events: [ { timestamp: utils_1.currentTimestamp(), unique: ['guid'], attributes: this.getEventAttributes(), }, ], }; _c.label = 1; case 1: _c.trys.push([1, 3, , 4]); return [4 /*yield*/, utils_1.post(this.uniqueEndpoint, payload)]; case 2: _c.sent(); return [3 /*break*/, 4]; case 3: e_1 = _c.sent(); (_a = this._logger) === null || _a === void 0 ? void 0 : _a.error("Encountered error sending unique event: " + ((_b = e_1) === null || _b === void 0 ? void 0 : _b.message)); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; /** * Send POST to summed-events API endpoint */ BacktraceMetrics.prototype.sendSummedEvent = function (metricGroup) { var _a, _b; return tslib_1.__awaiter(this, void 0, void 0, function () { var attributes, payload, e_2; return tslib_1.__generator(this, function (_c) { switch (_c.label) { case 0: attributes = this.getEventAttributes(); payload = { application: attributes.application, appversion: attributes['application.version'], metadata: { dropped_events: 0, }, summed_events: [ { timestamp: utils_1.currentTimestamp(), metric_group: metricGroup, attributes: this.getEventAttributes(), }, ], }; _c.label = 1; case 1: _c.trys.push([1, 3, , 4]); return [4 /*yield*/, utils_1.post(this.summedEndpoint, payload)]; case 2: _c.sent(); return [3 /*break*/, 4]; case 3: e_2 = _c.sent(); (_a = this._logger) === null || _a === void 0 ? void 0 : _a.error("Encountered error sending summed event: " + ((_b = e_2) === null || _b === void 0 ? void 0 : _b.message)); return [3 /*break*/, 4]; case 4: return [2 /*return*/]; } }); }); }; BacktraceMetrics.prototype.getEventAttributes = function () { var clientAttributes = this.attributeProvider(); var result = { 'application.session': this.sessionId, }; for (var attributeName in clientAttributes) { if (Object.prototype.hasOwnProperty.call(clientAttributes, attributeName)) { var element = clientAttributes[attributeName]; var elementType = typeof element; if (elementType === 'string' || elementType === 'boolean' || elementType === 'number') { var attributeValue = element.toString(); if (attributeValue) { result[attributeName] = attributeValue; } } } } return result; }; return BacktraceMetrics; }()); exports.BacktraceMetrics = BacktraceMetrics; //# sourceMappingURL=backtraceMetrics.js.map