使用StatsD的Pyramid性能指标
项目描述
使用StatsD为Pyramid提供性能指标。该项目旨在以最不侵入的方式提供对Pyramid应用程序的监控方法。
安装
使用setuptools安装,例如(在虚拟环境内)
$ pip install pyramid_metrics
设置
一旦安装了 pyramid_metrics,您必须使用 config.include 机制将其包含到Pyramid项目的配置中。在您的Pyramid项目的 __init__.py
config = Configurator(.....)
config.include('pyramid_metrics')
或者您可以在 .ini 文件中使用 pyramid.includes 配置值
[app:myapp]
pyramid.includes = pyramid_metrics
用法
Pyramid_metrics配置(值是默认值)
[app:myapp]
metrics.host = localhost
metrics.port = 8125
metrics.prefix = application.stage
metrics.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 1
request.metrics.incr('cache.hit')
# Increment by N
request.metrics.incr(('cache.hit.read.total', count=len(cacheresult)))
# Stat names can be composed from list or tuple
request.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 8
request.metrics.gauge('sql.connections', 8)
# Increase the value of the metrics by some amount
request.metrics.gauge('network.egress', 34118, delta=True)
计时器
StatsD 类型: https://github.com/etsy/statsd/blob/master/docs/metric_types.md#timing
# Simple timing
time_in_ms = requests.get('http://example.net').elapsed.microseconds/1000
request.metrics.timing('net.example.responsetime', time_in_ms)
# Using the time marker mechanism
request.metrics.marker_start('something_slow')
httpclient.get('http://example.net')
request.metrics.marker_stop('something_slow')
# Measure different outcome
request.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 manager
with request.metrics.timer(['longprocess', processname]):
run_longprocess(processname)
# Send measure to 'longprocess.foobar' or 'longprocess.foobar.exc'
目前实现
将收集工具作为请求方法
能够按 Pyramid 路由发送指标
简单的时间标记机制
简单的计数器
时间度量类型的上下文管理器
待办事项
完整的 StatsD 度量类型
自动度量扩展(SQLAlchemy, MongoDB, Requests…)
指标的白名单/黑名单
每个子系统的时分配(使用时间标记机制)
注意事项
一般的错误策略是:始终安全。Pyramid_metrics 应该永远不会破坏你的应用程序。
域名解析是在配置期间完成的,以避免重复的延迟。
开发
运行测试
测试由 nose 运行,所有依赖项都在 requirements-test.txt 中。
$ pip install -r requirements-test
...
$ nosetests
...
使用 tox 运行测试
$ pip install tox
...
$ tox # Run on python 2.7 and python 3.4
...
$ tox -e py34 # Run on python 3.4 only
贡献者
Pior Bastida (@pior)
Philippe Gauthier (@deuxpi)
Hadrien David (@hadrien)
Jay R. Wren (@jrwren)
项目详情
下载文件
为您的平台下载文件。如果您不确定要选择哪一个,请了解有关 安装包 的更多信息。