使用statsd的金字塔性能度量
pyramid_metrics的Python项目详细描述
使用statsd的金字塔的性能度量。该项目旨在提供 以最少侵入的方式插入金字塔应用程序的方法。
安装
使用setuptools安装,例如(在virtualenv中):
$ pip install pyramid_metrics
设置
安装pyramid_metrics后,必须使用config.include 将其包含到金字塔项目配置中的机制。在你的 金字塔项目的__init__.py:
config=Configurator(.....)config.include('pyramid_metrics')
或者,您可以在 .ini文件:
[app:myapp]pyramid.includes=pyramid_metrics
用法
金字塔度量配置(默认值):
[app:myapp]metrics.host=localhostmetrics.port=8125metrics.prefix=application.stagemetrics.route_performance=true
路线性能
如果启用,路由性能功能将计时请求处理。 通过使用statsd计时器类型度量,预聚合将提供信息 关于延迟、速率和总数。信息发送两次:每条路线 在全球范围内。
密钥名由路由名组成, http方法和结果(作为http状态代码或“exc”表示异常)。
- 全局键request.<HTTP_METHOD>.<STATUS_CODE_OR_EXC>
- 每个路由密钥route.<ROUTE_NAME>.request.<HTTP_METHOD>.<STATUS_CODE_OR_EXC>
API
计数器
STATSD类型: https://github.com/etsy/statsd/blob/master/docs/metric_types.md#counting
# Increment a counter named cache.hit by 1request.metrics.incr('cache.hit')# Increment by Nrequest.metrics.incr(('cache.hit.read.total',count=len(cacheresult)))# Stat names can be composed from list or tuplerequest.metrics.incr(('cache',cache_action))
仪表
STATSD类型: https://github.com/etsy/statsd/blob/master/docs/metric_types.md#gauges
# Set the number of SQL connections to 8request.metrics.gauge('sql.connections',8)# Increase the value of the metrics by some amountrequest.metrics.gauge('network.egress',34118,delta=True)
计时器
STATSD类型: https://github.com/etsy/statsd/blob/master/docs/metric_types.md#timing
# Simple timingtime_in_ms=requests.get('http://example.net').elapsed.microseconds/1000request.metrics.timing('net.example.responsetime',time_in_ms)# Using the time marker mechanismrequest.metrics.marker_start('something_slow')httpclient.get('http://example.net')request.metrics.marker_stop('something_slow')# Measure different outcomerequest.metrics.marker_start('something_slow')try:httpclient.get('http://example.net').raise_for_status()except:# Send measure to key 'something_slow.error'request.metrics.marker_stop('something_slow',suffix='error')else:# Send measure to key 'something_slow.ok'request.metrics.marker_stop('something_slow',suffix='ok')# Using the context managerwithrequest.metrics.timer(['longprocess',processname]):run_longprocess(processname)# Send measure to 'longprocess.foobar' or 'longprocess.foobar.exc'
当前实施
- 收集实用程序作为请求方法
- 能够按金字塔路由发送度量值
- 简单的时间标记机制
- 简单计数器
- 计时度量类型的上下文管理器
待办事项
- 全statsd度量类型
- 自动计量扩展(sqlalchemy、mongodb、requests…)
- 白名单/指标黑名单
- 每个子系统的时间分配(使用时间标记机制)
注意事项
- 一般错误策略是:总是故障保护。金字塔度量永远不应该 打破你的申请。
- DNS解析是在配置期间完成的,以避免重复的延迟。
开发
运行测试
测试由nose运行,所有依赖项都在requirements-test.txt中。
$ pip install -r requirements-test ... $ nosetests ...
进行毒性
$ pip install tox ... $ tox # Run on python 2.7 and python 3.4 ... $ tox -e py34 # Run on python 3.4 only
贡献者
- 皮奥巴斯蒂达(@pior)
- 菲利普·高希尔(@deuxpi)
- 哈德里安·大卫(@hadrien)
- 杰·R·雷恩(@jrrren)