vulcain-corejs
Version:
Vulcain micro-service framework
52 lines (50 loc) • 1.95 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;
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(',');
}
increment(metric, delta) {
this.statsd && this.statsd.increment(metric + this.tags + this.customTags, delta);
}
decrement(metric, delta) {
this.statsd && this.statsd.decrement(metric + this.tags + this.customTags, delta);
}
counter(metric, delta) {
this.statsd && this.statsd.counter(metric + this.tags + this.customTags, delta);
}
gauge(metric, value) {
this.statsd && this.statsd.gauge(metric + this.tags + this.customTags, value);
}
timing(metric, duration) {
this.statsd && this.statsd.timing(metric + this.tags + this.customTags, duration);
}
}
exports.StatsdMetrics = StatsdMetrics;
//# sourceMappingURL=statsdMetrics.js.map