WSGI中间件,用于statsd计时
项目描述
wsgi-statsd 文档
wsgi_statsd是一个WSGI中间件,提供了一种轻松计时所有请求的方法。集成就像包装您现有的WSGI应用程序一样简单。
使用
在您的wsgi.py文件中,按照以下方式包装您的WSGI应用程序
import statsd
from wsgi_statsd import StatsdTimingMiddleware
def application(environ, start_response):
response_body = 'The request method was %s' % environ['REQUEST_METHOD']
status = '200 OK'
response_headers = [('Content-Type', 'text/plain'),
('Content-Length', str(len(response_body)))]
start_response(status, response_headers)
return [response_body]
client = statsd.StatsClient(
prefix='your_prefix',
host='your_host',
port=8125
)
application = StatsdTimingMiddleware(application, client)
application = StatsdTimingMiddleware(application, client, time_exceptions=True)
它做什么
wsgi-statsd 使用 statsd 定时器函数,生成一个键并将请求所花费的时间作为值。键的构建方式如下:
<statsd-client-prefix>.<separator-joined-path>.<request-method>.<response-code>.<exception-name>
使用 foo 前缀,在你的 statsd 客户端中,并调用 www.spam.com/bar/test/ 页面,将导致 foo .bar_test.GET.200 的值等于处理请求所需的时间。
如果你传递了 time_exceptions=True 并且在响应过程中发生任何异常,则键名将在异常类名后附加: foo.bar_test.GET.500.ValueError。
根据您的需求进行自定义
可以自定义 wsgi_statsd 生成键和/或时间的方式。 StatsdTimingMiddleware 有 send_stats 和 get_key_name,你可以覆盖这些方法
class CustomStatsdMiddleware(StatsdTimingMiddleware):
def get_key_name(self, environ, response_interception):
return super(self, CustomStatsdMiddleware).get_key_name(environ, response_interception) + '.' + environ['Transfer-Encoding']
def send_stats(self, start, environ, response_interception):
super(self, CustomStatsdMiddleware).send_stats(start + 10, environ, response_interception)
wsgi-statsd 使用下划线作为键中路径部分的分隔符,因为这样可以很容易地从 graphite 中检索数据。你可以通过将分隔符值传递给中间件构造函数来覆盖此默认设置
StatsdTimingMiddleware(application, client, separator='.')
联系
如果你有任何问题、错误报告、建议等,请在该 GitHub 项目页面 上创建一个问题。
许可证
本软件受 MIT 许可证 许可。
请参阅 许可证文件。
© 2015 Wouter Lansu,Paylogic International 及他人。
变更日志
0.3.1
将 clean 目标添加到 Makefile。
添加了 0.2.6 版本的变化。
0.2.6
更新了文档。
0.2.5
修复了分隔符参数的问题。它现在按预期工作并已记录。
0.2.4
将分隔符参数添加到 StatsdTimingMiddleware.__init__()。
0.2.3
在发生异常时将异常类名添加到键中(bubenkoff)
0.2.1
确保从响应中正确调用 close()(bubenkoff,GrahamDumpleton)
可选地计时异常(bubenkoff)
代码可读性改进(bubenkoff)
0.2.0
修复了版本号
添加了对返回生成器的 WSGI 应用程序的支持
添加了一个测试,以验证在通过中间件后响应体保持不变
0.1.0
首次公开发布
项目详情
wsgi-statsd-0.3.1.tar.gz 的哈希
算法 | 哈希摘要 | |
---|---|---|
SHA256 | 57216790da03261b9ad77c5429d039a643e24383ab145d5f9e3e1cc4d0292458 |
|
MD5 | 5d74f90e27ca7e6c9432584cd6df3067 |
|
BLAKE2b-256 | 909012ab5fd2b86deb018c7afe570ca5d652d22429f69ca1e9bcaaf60145c7f9 |