vulcain-corejs
Version:
Vulcain micro-service framework
119 lines (117 loc) • 4.96 kB
JavaScript
const rollingNumber_1 = require("./rollingNumber");
const rollingPercentile_1 = require("./rollingPercentile");
const rollingNumberEvent_1 = require("./rollingNumberEvent");
const actualTime_1 = require("../../utils/actualTime");
class CommandMetrics {
constructor(properties) {
this.properties = properties;
if (!properties)
throw new Error("properties argument is required");
this.currentExecutionCount = 0;
this.commandName = properties.commandName;
this.commandGroup = properties.commandGroup;
this.lastHealthCountsSnapshot = actualTime_1.default.getCurrentTime();
this.metricsRollingStatisticalWindowInMilliseconds = properties.metricsRollingStatisticalWindowInMilliseconds.value;
this.rollingCount = new rollingNumber_1.RollingNumber(properties.metricsRollingStatisticalWindowInMilliseconds.value, properties.metricsRollingStatisticalWindowBuckets.value);
this.percentileCount = new rollingPercentile_1.RollingPercentile(properties.metricsRollingPercentileWindowInMilliseconds.value, properties.metricsRollingPercentileWindowBuckets.value);
}
markFallbackSuccess() {
this.rollingCount.increment(rollingNumberEvent_1.default.FALLBACK_SUCCESS);
}
markFallbackFailure() {
this.rollingCount.increment(rollingNumberEvent_1.default.FALLBACK_FAILURE);
}
markFallbackRejection() {
this.rollingCount.increment(rollingNumberEvent_1.default.FALLBACK_REJECTION);
}
markExceptionThrown() {
this.rollingCount.increment(rollingNumberEvent_1.default.EXCEPTION_THROWN);
}
markBadRequest(duration) {
this.rollingCount.increment(rollingNumberEvent_1.default.BAD_REQUEST);
}
markResponseFromCache() {
this.rollingCount.increment(rollingNumberEvent_1.default.RESPONSE_FROM_CACHE);
}
markSuccess() {
this.rollingCount.increment(rollingNumberEvent_1.default.SUCCESS);
}
markRejected() {
this.rollingCount.increment(rollingNumberEvent_1.default.REJECTED);
}
markFailure() {
this.rollingCount.increment(rollingNumberEvent_1.default.FAILURE);
}
markTimeout() {
this.rollingCount.increment(rollingNumberEvent_1.default.TIMEOUT);
}
markShortCircuited() {
this.rollingCount.increment(rollingNumberEvent_1.default.SHORT_CIRCUITED);
}
incrementExecutionCount() {
++this.currentExecutionCount;
}
decrementExecutionCount() {
--this.currentExecutionCount;
}
getCurrentExecutionCount() {
return this.currentExecutionCount;
}
addExecutionTime(time) {
this.percentileCount.addValue(time);
}
getRollingCount(type) {
return this.rollingCount.getRollingSum(type);
}
getExecutionTime(percentile) {
return this.percentileCount.getPercentile(percentile);
}
getHealthCounts() {
//TODO restrict calculation by time to avoid too frequent calls
let success = this.rollingCount.getRollingSum(rollingNumberEvent_1.default.SUCCESS);
let error = this.rollingCount.getRollingSum(rollingNumberEvent_1.default.FAILURE);
let timeout = this.rollingCount.getRollingSum(rollingNumberEvent_1.default.TIMEOUT);
let shortCircuited = this.rollingCount.getRollingSum(rollingNumberEvent_1.default.SHORT_CIRCUITED);
let semaphoreRejected = this.rollingCount.getRollingSum(rollingNumberEvent_1.default.REJECTED);
let totalCount = success + error + timeout + shortCircuited + semaphoreRejected;
let errorCount = error + timeout + shortCircuited + semaphoreRejected;
let errorPercentage = 0;
if (totalCount > 0) {
errorPercentage = errorCount / totalCount * 100;
}
return {
totalCount: totalCount,
errorCount: errorCount,
errorPercentage: errorPercentage
};
}
reset() {
this.rollingCount.reset();
this.lastHealthCountsSnapshot = actualTime_1.default.getCurrentTime();
}
}
exports.CommandMetrics = CommandMetrics;
class CommandMetricsFactory {
static getOrCreate(options) {
let previouslyCached = CommandMetricsFactory.metricsByCommand.get(options.commandName);
if (previouslyCached) {
return previouslyCached;
}
let metrics = new CommandMetrics(options);
CommandMetricsFactory.metricsByCommand.set(options.commandName, metrics);
return metrics;
}
static get(commandName) {
return CommandMetricsFactory.metricsByCommand.get(commandName);
}
static resetCache() {
CommandMetricsFactory.metricsByCommand.clear();
}
static getAllMetrics() {
return CommandMetricsFactory.metricsByCommand.values();
}
}
CommandMetricsFactory.metricsByCommand = new Map();
exports.CommandMetricsFactory = CommandMetricsFactory;
//# sourceMappingURL=commandMetrics.js.map
;