python3兼容库,用于将数据发送到Graphite metrics服务器(Carbon)
graphyte的Python项目详细描述
graphine是一个小Python库,它将数据发送到Graphite度量 服务器(碳纤维)。我们编写它是因为现有的graphitesend库 不支持Python3,它还需要gevent来异步使用。 graphine与python3.4+和python2.7兼容,并使用 用于异步使用的标准库threading模块。在
库是on the Python Package Index (PyPI),所以要安装它,请启动 在命令提示符下,如果您正在使用virtualenv,请激活它,然后键入:
pip install graphyte
使用graphine很简单–只需调用init()来初始化默认值 sender,然后send()发送消息。例如,发送 system.sync.foo.bar 42 {timestamp}\n到graphite.example.com:2003年 同步:
^{pr2}$如果要在后台线程上异步发送(例如,在 web服务器上下文),只需指定一个发送间隔。例如,这将 设置后台线程每隔10秒发送一次:
graphyte.init('graphite.example.com',prefix='system.async',interval=10)graphyte.send('foo.bar',42)
如果要发送标记的度量,其用法如下:
graphite.send('foo.bar',42,tags={'ding':'dong'})
对于更高级的用法,例如,如果要发送到多个服务器 或者,如果要子类Sender,可以实例化 Sender直接。例如,实例化发送到 不同的服务器(一个同步,一个使用后台线程发送 间隔10秒),使用如下方法:
sender1=graphyte.Sender('graphite1.example.com',prefix='system.one')sender2=graphyte.Sender('graphite2.example.com',prefix='system.two',interval=10)sender1.send('foo.bar1',42)sender2.send('foo.bar2',43)
如果您想通过UDP而不是TCP发送,只需将protocol='udp'添加到 init()或Sender()调用。在
或者,为了自定义消息如何被记录或发送到套接字,子类 Sender并重写send_message(或者甚至send_socket,如果您 要重写日志记录和异常处理):
classCustomSender(graphyte.Sender):defsend_message(self,message):print('Sending bytes in some custom way: {!r}'.format(message))
使用Python日志系统(使用 记录器名称“graphyte”)。如果发送方初始化为 log_sends=True,所有发送都记录在信息级别。在
也可以使用graphine直接从命令行发送度量:
python -m graphyte foo.bar 42
有命令行参数来指定服务器、端口和其他 配置。键入python -m graphyte --help以获取帮助。在
请阅读graphyte.py中的代码以了解更多详细信息–它非常小!在
graphine是由Ben Hoyt为Jetsetter编写的,并获得了 MIT许可证(见LICENSE.txt)。在
- 项目
标签: