用法:

http://pypi.python.org/pypi/python-statsd/1.5.2与我安装的不是很一样,稍作修改再用(使用help(statsd)查看用法)

一 概念说明

这个是使用说明跟下面描述的statsd的用法不是很一样。可能是不同版本所致。



Concepts

  • bucketsEach stat is in its own "bucket". They are not predefined anywhere. Bucketscan be named anything that will translate to Graphite (periods make folders,etc)
  • valuesEach stat will have a value. How it is interpreted depends on modifiers. Ingeneral values should be integer.
  • flushAfter the flush interval timeout (defined by config.flushInterval,default 10 seconds), stats are aggregated and sent to an upstream backend service.

Counting

gorets:1|c


This is a simple counter. Add 1 to the "gorets" bucket.At each flush the current count is sent and reset to 0.If the count at flush is 0 then you can opt to send no metric at all forthis counter, by setting​​config.deleteCounters​​ (applies only to graphitebackend). Statsd will send both the rate as well as the count at each flush.

Sampling

gorets:1|c|@0.1


Tells StatsD that this counter is being sent sampled every 1/10th of the time.

Timing

glork:320|ms


The glork took 320ms to complete this time. StatsD figures out percentiles,average (mean), standard deviation, sum, lower and upper bounds for the flush interval.The percentile threshold can be tweaked with​​config.percentThreshold​​.

The percentile threshold can be a single value, or a list of values, and willgenerate the following list of stats for each threshold:

stats.timers.$KEY.mean_$PCT
stats.timers.$KEY.upper_$PCT
stats.timers.$KEY.sum_$PCT


Where ​​$KEY​​ is the stats key you specify when sending to statsd, and​​$PCT​​ isthe percentile threshold.

Gauges

StatsD now also supports gauges, arbitrary values, which can be recorded.

gaugor:333|g


Sets

StatsD supports counting unique occurences of events between flushes,using a Set to store all occuring events.

uniques:765|s




Multi-Metric Packets

StatsD supports receiving multiple metrics in a single packet by separating themwith a newline.

gorets:1|c\nglork:320|ms\ngaugor:333|g\nuniques:765|s


二 具体使用

1

下面是python版本的statsd的用法:

import python

import statsd

使用help(statsd)观察statsd的用法

ccc=statsd.client.StatsClient(host='127.0.0.1',port=8125)#建立链接

ccc.incr("hello",5)#把hello这个值增加5


2变量的路径

你使用statsd发送的数据,例如上面的变量hello是在stats 和stats_count路径下面。

假如你定义了​​local.random.diceroll这样一个变量,并且不使用statsd发送,那么它会产生再根目录下。echo "local.random.diceroll 4 `date +%s`" | nc ${SERVER} ${PORT};​






所有函数的定义:

 class StatsClient(__builtin__.object)

     |  A client for statsd.

     |  

     |  Methods defined here:

     |  

     |  __init__(self, host='localhost', port=8125, prefix=None)

     |      Create a new client.

     |  

     |  decr(self, stat, count=1, rate=1) #减少一个值stat的大小count (rate是抽样的比例)

     |      Decrement a stat by `count`.

     |  

     |  gauge(self, stat, value, rate=1)#设置一个值stat的大小

     |      Set a gauge value.

     |  

     |  incr(self, stat, count=1, rate=1)#增加一个值stat的大小count (rate是抽样的比例)

     |      Increment a stat by `count`.

     |  

     |  timer(self, stat, rate=1)#发送一个时间值stat

     |  

     |  timing(self, stat, delta, rate=1) #微秒

     |      Send new timing information. `delta` is in milliseconds.

     |  



网上还有其它语言版本的statsd,似乎使用起来更加方便