StatsD
StatsD is a network daemon that runs on the Node.js platform and listens for statistics, like counters and timers, sent over UDP and sends aggregates to one or more pluggable backend services (e.g., Graphite).
One of the useful things StatsD can do is to sample data; by sending only a percentage of the requests you get a lot less data sent while still capturing an accurate view of the system.
https://github.com/etsy/statsd
**Key Words:**Node.js buckets values flush
Buckets:
Each stat is in its own “bucket”. They are not predefined anywhere. Buckets can be named anything that will translate to Graphite (periods make folders, etc)
Values:
Each stat will have a value. How it is interpreted depends on modifiers. In general values should be integer.
每个 stat 都有一个 value,该值的解释方式依赖于 modifier。通常,values 应该是整数。
Flush:
After the flush interval timeout (defined by config.flushInterval, default 10 seconds), stats are aggregated and sent to an upstream backend service.
在 flush interval (冲洗间隔,通常为10秒)超时之后,stats 会聚集起来,传送到上游的后端服务。
Stat Type
counting
计数器很简单。它会给 bucket 加 value,并存储在内存中,直到 flush interval 超时。
···
for (key in counters) {
var value = counters[key];
var valuePerSecond = value / (flushInterval / 1000); // calculate “per second” rate
statString += ‘stats.’+ key + ’ ’ + valuePerSecond + ’ ’ + ts + “\n”;
statString += ‘stats_counts.’ + key + ’ ’ + value + ’ ’ + ts + “\n”;
numStats += 1;
}
···
timing
计时器用于收集数字。他们不必要包含时间值。你可以收集某个存储器中的字节数、对象数或任意数字。计时器的一大好处在于,你可以得到平均值、总值、计数值和上下限值。给 StatsD 设置一个计时器,就能在数据传送给 Graphite 之前自动计算这些量。
Gauges
一个 guage 代表着时间段内某点的任意 vaule,是 StatsD 中最简单的类型。你可以给它传任意值,它会传给后端。
Gauge stats 的源码只有短短四行。
···
for (key in gauges) {
statString += ‘stats.gauges.’ + key + ’ ’ + gauges[key] + ’ ’ + ts + “\n”;
numStats += 1;
}
···
给StatsD 传一个数字,它会不经处理地将该数字传到后端。值得注意的是,在一个 flush interval 内,只有 gauge 最后的值会传送到后端。因此,如果你在一个 flush interval 内,将下面的 gauge 值传给 StatsD:
643
754
583
会传到后端的值只有583而已。该 gauge 的值会一直存储在内存中,直到 flush interval 结束才传值。
参考:http://segmentfault.com/a/1190000003800788
http://pgxn.org/dist/statsd/0.2.0/