vulcain-corejs
Version:
Vulcain micro-service framework
50 lines (48 loc) • 2.29 kB
JavaScript
"use strict";
const conventions_1 = require("../utils/conventions");
const Statsd = require("statsd-client");
const system_1 = require("./../configurations/globals/system");
const dynamicConfiguration_1 = require("../configurations/dynamicConfiguration");
/**
* Default metrics adapter
* Emit metrics on statsd
*
* @export
* @class StatsdMetrics
*/
class StatsdMetrics {
constructor(address) {
if (!system_1.System.isDevelopment) {
let host = dynamicConfiguration_1.DynamicConfiguration.getPropertyValue("statsdAgent") || system_1.System.resolveAlias(address) || address;
if (host) {
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(/:/g, '-');
system_1.System.log.info(null, "Initialize statsd metrics adapter on '" + host + "' with initial tags : " + this.tags);
}
}
}
encodeTags(...tags) {
if (!tags || tags.length === 0)
return StatsdMetrics.EmptyString;
return "," + tags.map(t => t.replace(/[:|,]/g, '-')).join(',');
}
increment(metric, customTags, delta) {
this.statsd && this.statsd.increment(metric.toLowerCase() + this.tags + (customTags || StatsdMetrics.EmptyString), delta);
}
decrement(metric, customTags, delta) {
this.statsd && this.statsd.decrement(metric.toLowerCase() + this.tags + (customTags || StatsdMetrics.EmptyString), delta);
}
counter(metric, delta, customTags) {
this.statsd && this.statsd.counter(metric.toLowerCase() + this.tags + (customTags || StatsdMetrics.EmptyString), delta);
}
gauge(metric, value, customTags) {
this.statsd && this.statsd.gauge(metric.toLowerCase() + this.tags + (customTags || StatsdMetrics.EmptyString), value);
}
timing(metric, duration, customTags) {
this.statsd && this.statsd.timing(metric.toLowerCase() + this.tags + (customTags || StatsdMetrics.EmptyString), duration);
}
}
StatsdMetrics.EmptyString = "";
exports.StatsdMetrics = StatsdMetrics;
//# sourceMappingURL=statsdMetrics.js.map