@allspark-js/core
Version:
Core library to create js applications.
62 lines (61 loc) • 2.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
var PrometheusRta = /** @class */ (function () {
function PrometheusRta(deps) {
this.deps = deps;
this.metrics = {
counter: {},
histogram: {},
};
}
PrometheusRta.prototype.createPromMetric = function (_a) {
var type = _a.type, metricData = _a.metricData;
var promClient = this.deps.promClient;
if (type === 'counter') {
return new promClient.Counter(metricData);
}
return new promClient.Histogram(metricData);
};
PrometheusRta.prototype.getMetric = function (_a) {
var type = _a.type, name = _a.name, _b = _a.labels, labels = _b === void 0 ? {} : _b;
var configManager = this.deps.configManager;
var prefix = configManager.get('rta:metricPrefix');
var metric = this.metrics[type];
var metricName = "".concat(prefix).concat(name);
var labelNames = Object.keys(labels);
var metricData = {
name: metricName,
help: metricName,
};
if (labels) {
metricData.labelNames = labelNames;
}
if (!metric[name]) {
metric[name] = {
instance: this.createPromMetric({ type: type, metricData: metricData }),
labelNames: labelNames,
};
}
return metric[name].instance;
};
PrometheusRta.prototype.inc = function (_a) {
var metric = _a.metric, labels = _a.labels, _b = _a.value, value = _b === void 0 ? 1 : _b;
var promMetric = this.getMetric({
type: 'counter',
name: metric,
labels: labels,
});
promMetric.inc(value);
};
PrometheusRta.prototype.record = function (_a) {
var metric = _a.metric, labels = _a.labels, value = _a.value;
var promMetric = this.getMetric({
type: 'histogram',
name: metric,
labels: labels,
});
promMetric.observe(value);
};
return PrometheusRta;
}());
exports.default = PrometheusRta;