Statsd is a client for Etsy's node-JS statsd server.石墨堆栈收藏与石墨服务器的代理。
python-statsd的Python项目详细描述
statsd是etsy的statsd服务器的客户端,是 Graphite统计数据收集和绘图服务器。
链接
- The source: https://github.com/WoLpH/python-statsd
- Project page: https://pypi.python.org/pypi/python-statsd
- Reporting bugs: https://github.com/WoLpH/python-statsd/issues
- Documentation: http://python-statsd.readthedocs.io/en/latest/
- My blog: http://w.wol.ph/
- Statsd: https://github.com/etsy/statsd
- Graphite: http://graphite.wikidot.com
安装
要安装,只需执行python setup.py install。 如果要先运行测试,请运行python setup.py nosets
用法
要快速开始,请尝试以下方法:
基本用法
计时器
>>> import statsd >>> >>> timer = statsd.Timer('MyApplication') >>> >>> timer.start() >>> # do something here >>> timer.stop('SomeTimer')
计数器
>>> import statsd >>> >>> counter = statsd.Counter('MyApplication') >>> # do something here >>> counter += 1
仪表
>>> import statsd >>> >>> gauge = statsd.Gauge('MyApplication') >>> # do something here >>> gauge.send('SomeName', value)
未加工
原始字符串应为预先汇总的数据或其他将 直接传给碳。这可以作为一个时间和 带宽节省机制发送大量样本可能需要 带宽(在udp报头中使用的b/w比测量器中使用的数据多,因为 实例)
>>> import statsd >>> >>> raw = statsd.Raw('MyApplication', connection) >>> # do something here >>> raw.send('SomeName', value, timestamp)
原始类型希望有一个时间戳(从epoch开始,以秒为单位)。 标准的unix时间戳,例如“date+%s”的输出,但是如果您忽略它,或者 不提供它将提供当前时间作为消息的一部分
平均值
>>> import statsd >>> >>> average = statsd.Average('MyApplication', connection) >>> # do something here >>> average.send('SomeName', 'somekey:%d'.format(value))
连接设置
如果需要某些设置,而不是Connection的默认设置, 您可以使用Connection.set_defaults()。
>>> import statsd >>> statsd.Connection.set_defaults(host='localhost', port=8125, sample_rate=1, disabled=False)
在这些设置之后,每次与statsd的交互都将使用 指定,除非显式创建另一个Connection以使用 (如下所述)。
默认值:
- host='localhost'
- port=8125
- sample_rate=1
- disabled=False
高级用法
>>> import statsd >>> >>> # Open a connection to `server` on port `1234` with a `50%` sample rate >>> statsd_connection = statsd.Connection( ... host='server', ... port=1234, ... sample_rate=0.5, ... ) >>> >>> # Create a client for this application >>> statsd_client = statsd.Client(__name__, statsd_connection) >>> >>> class SomeClass(object): ... def __init__(self): ... # Create a client specific for this class ... self.statsd_client = statsd_client.get_client( ... self.__class__.__name__) ... ... def do_something(self): ... # Create a `timer` client ... timer = self.statsd_client.get_client(class_=statsd.Timer) ... ... # start the measurement ... timer.start() ... ... # do something ... timer.intermediate('intermediate_value') ... ... # do something else ... timer.stop('total')
如果需要关闭服务并避免发送udp消息, 可以通过启用disabled参数来禁用Connection类:
>>> statsd_connection = statsd.Connection( ... host='server', ... port=1234, ... sample_rate=0.5, ... disabled=True ... )
如果日志记录的级别设置为调试Connection对象,则它将通知 不再发送UDP消息