@splitsoftware/splitio-commons
Version:
Split JavaScript SDK common components
76 lines (75 loc) • 3.21 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.telemetryTrackerFactory = void 0;
var labels_1 = require("../utils/labels");
var timer_1 = require("../utils/timeTracker/timer");
var constants_1 = require("../utils/constants");
function telemetryTrackerFactory(telemetryCache, now) {
if (telemetryCache && now) {
var startTime_1 = (0, timer_1.timer)(now);
return {
trackEval: function (method) {
var evalTime = (0, timer_1.timer)(now);
return function (label) {
switch (label) {
case labels_1.EXCEPTION:
telemetryCache.recordException(method);
return; // Don't track latency on exceptions
case labels_1.SDK_NOT_READY: // @ts-ignore ITelemetryCacheAsync doesn't implement the method
if (telemetryCache.recordNonReadyUsage)
telemetryCache.recordNonReadyUsage();
}
telemetryCache.recordLatency(method, evalTime());
};
},
trackHttp: function (operation) {
var httpTime = (0, timer_1.timer)(now);
return function (error) {
telemetryCache.recordHttpLatency(operation, httpTime());
if (error && error.statusCode)
telemetryCache.recordHttpError(operation, error.statusCode);
else
telemetryCache.recordSuccessfulSync(operation, Date.now());
};
},
sessionLength: function () {
if (telemetryCache.recordSessionLength)
telemetryCache.recordSessionLength(startTime_1());
},
streamingEvent: function (e, d) {
if (e === constants_1.AUTH_REJECTION) {
telemetryCache.recordAuthRejections();
}
else {
telemetryCache.recordStreamingEvents({
e: e,
d: d,
t: Date.now()
});
if (e === constants_1.TOKEN_REFRESH)
telemetryCache.recordTokenRefreshes();
}
},
addTag: function (tag) {
// @ts-ignore
if (telemetryCache.addTag)
telemetryCache.addTag(tag);
},
trackUpdatesFromSSE: function (type) {
telemetryCache.recordUpdatesFromSSE(type);
}
};
}
else { // If there is not `telemetryCache` or `now` time tracker, return a no-op telemetry tracker
var noopTrack = function () { return function () { }; };
return {
trackEval: noopTrack,
trackHttp: noopTrack,
sessionLength: function () { },
streamingEvent: function () { },
addTag: function () { },
trackUpdatesFromSSE: function () { },
};
}
}
exports.telemetryTrackerFactory = telemetryTrackerFactory;
;