UNPKG

@splitsoftware/splitio-commons

Version:
210 lines (209 loc) 8.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.TelemetryCacheInMemory = exports.shouldRecordTelemetry = exports.newBuckets = exports.MAX_LATENCY_BUCKET_COUNT = void 0; var constants_1 = require("../../utils/constants"); var key_1 = require("../../utils/key"); var findLatencyIndex_1 = require("../findLatencyIndex"); var MAX_STREAMING_EVENTS = 20; var MAX_TAGS = 10; exports.MAX_LATENCY_BUCKET_COUNT = 23; function newBuckets() { // MAX_LATENCY_BUCKET_COUNT (length) is 23 // Not using Array.fill for old browsers compatibility return [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; } exports.newBuckets = newBuckets; var ACCEPTANCE_RANGE = 0.001; /** * Record telemetry if mode is not localhost. * All factory instances track telemetry on server-side, and 0.1% on client-side. */ function shouldRecordTelemetry(_a) { var settings = _a.settings, disableTelemetry = _a.disableTelemetry; return !disableTelemetry && settings.mode !== constants_1.LOCALHOST_MODE && ((0, key_1.checkIfServerSide)(settings) || Math.random() <= ACCEPTANCE_RANGE); } exports.shouldRecordTelemetry = shouldRecordTelemetry; var TelemetryCacheInMemory = /** @class */ (function () { function TelemetryCacheInMemory(definitions, segments, largeSegments) { this.definitions = definitions; this.segments = segments; this.largeSegments = largeSegments; this.name = 'telemetry stats'; // isEmpty flag this.e = true; this.notReadyUsage = 0; /** Usage stats */ this.impressionStats = [0, 0, 0]; this.eventStats = [0, 0]; this.lastSync = {}; this.httpErrors = {}; this.httpLatencies = {}; this.authRejections = 0; this.tokenRefreshes = 0; this.streamingEvents = []; this.tags = []; this.exceptions = {}; this.latencies = {}; this.updatesFromSSE = {}; } TelemetryCacheInMemory.prototype.isEmpty = function () { return this.e; }; TelemetryCacheInMemory.prototype.clear = function () { }; TelemetryCacheInMemory.prototype.pop = function () { this.e = true; return { lS: this.getLastSynchronization(), mL: this.popLatencies(), mE: this.popExceptions(), hE: this.popHttpErrors(), hL: this.popHttpLatencies(), tR: this.popTokenRefreshes(), aR: this.popAuthRejections(), iQ: this.getImpressionStats(constants_1.QUEUED), iDe: this.getImpressionStats(constants_1.DEDUPED), iDr: this.getImpressionStats(constants_1.DROPPED), spC: this.definitions && this.definitions.getNames().length, seC: this.segments && this.segments.getRegisteredSegments().length, skC: this.segments && this.segments.getKeysCount(), lsC: this.largeSegments && this.largeSegments.getRegisteredSegments().length, lskC: this.largeSegments && this.largeSegments.getKeysCount(), sL: this.getSessionLength(), eQ: this.getEventStats(constants_1.QUEUED), eD: this.getEventStats(constants_1.DROPPED), sE: this.popStreamingEvents(), t: this.popTags(), ufs: this.popUpdatesFromSSE(), }; }; TelemetryCacheInMemory.prototype.getTimeUntilReady = function () { return this.timeUntilReady; }; TelemetryCacheInMemory.prototype.recordTimeUntilReady = function (ms) { this.timeUntilReady = ms; }; TelemetryCacheInMemory.prototype.getTimeUntilReadyFromCache = function () { return this.timeUntilReadyFromCache; }; TelemetryCacheInMemory.prototype.recordTimeUntilReadyFromCache = function (ms) { this.timeUntilReadyFromCache = ms; }; TelemetryCacheInMemory.prototype.getNonReadyUsage = function () { return this.notReadyUsage; }; TelemetryCacheInMemory.prototype.recordNonReadyUsage = function () { this.notReadyUsage++; }; TelemetryCacheInMemory.prototype.getImpressionStats = function (type) { return this.impressionStats[type]; }; TelemetryCacheInMemory.prototype.recordImpressionStats = function (type, count) { this.impressionStats[type] += count; this.e = false; }; TelemetryCacheInMemory.prototype.getEventStats = function (type) { return this.eventStats[type]; }; TelemetryCacheInMemory.prototype.recordEventStats = function (type, count) { this.eventStats[type] += count; this.e = false; }; TelemetryCacheInMemory.prototype.getLastSynchronization = function () { return this.lastSync; }; TelemetryCacheInMemory.prototype.recordSuccessfulSync = function (resource, timeMs) { this.lastSync[resource] = timeMs; this.e = false; }; TelemetryCacheInMemory.prototype.popHttpErrors = function () { var result = this.httpErrors; this.httpErrors = {}; return result; }; TelemetryCacheInMemory.prototype.recordHttpError = function (resource, status) { var statusErrors = (this.httpErrors[resource] = this.httpErrors[resource] || {}); statusErrors[status] = (statusErrors[status] || 0) + 1; this.e = false; }; TelemetryCacheInMemory.prototype.popHttpLatencies = function () { var result = this.httpLatencies; this.httpLatencies = {}; return result; }; TelemetryCacheInMemory.prototype.recordHttpLatency = function (resource, latencyMs) { var latencyBuckets = (this.httpLatencies[resource] = this.httpLatencies[resource] || newBuckets()); latencyBuckets[(0, findLatencyIndex_1.findLatencyIndex)(latencyMs)]++; this.e = false; }; TelemetryCacheInMemory.prototype.popAuthRejections = function () { var result = this.authRejections; this.authRejections = 0; return result; }; TelemetryCacheInMemory.prototype.recordAuthRejections = function () { this.authRejections++; this.e = false; }; TelemetryCacheInMemory.prototype.popTokenRefreshes = function () { var result = this.tokenRefreshes; this.tokenRefreshes = 0; return result; }; TelemetryCacheInMemory.prototype.recordTokenRefreshes = function () { this.tokenRefreshes++; this.e = false; }; TelemetryCacheInMemory.prototype.popStreamingEvents = function () { return this.streamingEvents.splice(0); }; TelemetryCacheInMemory.prototype.recordStreamingEvents = function (streamingEvent) { if (this.streamingEvents.length < MAX_STREAMING_EVENTS) { this.streamingEvents.push(streamingEvent); } this.e = false; }; TelemetryCacheInMemory.prototype.popTags = function () { return this.tags.splice(0); }; TelemetryCacheInMemory.prototype.addTag = function (tag) { if (this.tags.length < MAX_TAGS) { this.tags.push(tag); } this.e = false; }; TelemetryCacheInMemory.prototype.getSessionLength = function () { return this.sessionLength; }; TelemetryCacheInMemory.prototype.recordSessionLength = function (ms) { this.sessionLength = ms; this.e = false; }; TelemetryCacheInMemory.prototype.popExceptions = function () { var result = this.exceptions; this.exceptions = {}; return result; }; TelemetryCacheInMemory.prototype.recordException = function (method) { this.exceptions[method] = (this.exceptions[method] || 0) + 1; this.e = false; }; TelemetryCacheInMemory.prototype.popLatencies = function () { var result = this.latencies; this.latencies = {}; return result; }; TelemetryCacheInMemory.prototype.recordLatency = function (method, latencyMs) { var latencyBuckets = (this.latencies[method] = this.latencies[method] || newBuckets()); latencyBuckets[(0, findLatencyIndex_1.findLatencyIndex)(latencyMs)]++; this.e = false; }; TelemetryCacheInMemory.prototype.popUpdatesFromSSE = function () { var result = this.updatesFromSSE; this.updatesFromSSE = {}; return result; }; TelemetryCacheInMemory.prototype.recordUpdatesFromSSE = function (type) { this.updatesFromSSE[type] = (this.updatesFromSSE[type] || 0) + 1; this.e = false; }; return TelemetryCacheInMemory; }()); exports.TelemetryCacheInMemory = TelemetryCacheInMemory;