vulcain-corejs
Version:
Vulcain micro-service framework
53 lines (51 loc) • 2.09 kB
JavaScript
"use strict";
const conventions_1 = require("../utils/conventions");
const Statsd = require("statsd-client");
const system_1 = require("./../configurations/globals/system");
/**
* Default metrics adapter
* Emit metrics on statsd
*
* @export
* @class StatsdMetrics
*/
class StatsdMetrics {
constructor(address) {
this.customTags = "";
if (!system_1.System.isDevelopment) {
let host = system_1.System.resolveAlias(address || conventions_1.Conventions.instance.defaultStatsdAddress);
host = host || address || conventions_1.Conventions.instance.defaultStatsdAddress;
this.statsd = new Statsd({ host: host, socketTimeout: conventions_1.Conventions.instance.defaultStatsdDelayInMs });
this.tags = ",service=" + system_1.System.serviceName + ',version=' + system_1.System.serviceVersion;
this.tags = this.tags.replace(':', ' ');
system_1.System.log.info(null, "Initialize statsd metrics adapter on '" + host + "' with initial tags : " + this.tags);
}
}
/**
* Add tags as an array of string like <tag-name>=<tag-value>
*
* @param {...Array<string>} tags
*
* @memberOf Metrics
*/
setTags(...tags) {
this.customTags = "," + tags.join(',').replace(':', ' ');
}
increment(metric, delta) {
this.statsd && this.statsd.increment(metric.toLowerCase() + this.tags + this.customTags, delta);
}
decrement(metric, delta) {
this.statsd && this.statsd.decrement(metric.toLowerCase() + this.tags + this.customTags, delta);
}
counter(metric, delta) {
this.statsd && this.statsd.counter(metric.toLowerCase() + this.tags + this.customTags, delta);
}
gauge(metric, value) {
this.statsd && this.statsd.gauge(metric.toLowerCase() + this.tags + this.customTags, value);
}
timing(metric, duration) {
this.statsd && this.statsd.timing(metric.toLowerCase() + this.tags + this.customTags, duration);
}
}
exports.StatsdMetrics = StatsdMetrics;
//# sourceMappingURL=statsdMetrics.js.map