[英]Prometheus statsd-exporter - how to tag status code in request duration metric (histogram)
I have setup statsd-exporter to scrape metric from gunicorn web server.我已经设置了 statsd-exporter 来从 gunicorn Web 服务器抓取指标。 My goal is to filter request duration metric only for successful request(non 5xx), however in statsd-exporter there is no way to tag status code in duration metric.
我的目标是仅针对成功请求(非 5xx)过滤请求持续时间指标,但是在 statsd-exporter 中,无法在持续时间指标中标记状态代码。 Can anyone suggest a way to add status code in request duration metric or a way to filter only successful request duration in prometheus.
任何人都可以建议一种在请求持续时间指标中添加状态代码的方法,或者一种在普罗米修斯中仅过滤成功请求持续时间的方法。
In particular I want to extract successful request duration hitogram from statsd-exporter to prometheus.特别是我想从 statsd-exporter 到 prometheus 提取成功的请求持续时间hitogram。
To export successful request duration histogram metrics from gunicorn web server to prometheus you would need to add this functionality in gunicorn sorcecode.要将成功的请求持续时间直方图指标从 gunicorn Web 服务器导出到 prometheus,您需要在 gunicorn 源代码中添加此功能。
First take a look at the code that exports statsd metrics here .首先看一下这里导出 statsd 指标的代码。 You should see this peace of code:
你应该看到这种代码的平静:
status = resp.status
...
self.histogram("gunicorn.request.duration", duration_in_ms)
By changing the code to sth like this:通过将代码更改为这样:
self.histogram("gunicorn.request.duration.%d" % status, duration_in_ms)
from this moment you will have metrics names exported with status codes like gunicorn_request_duration_200
or gunicorn_request_duration_404
etc.从这一刻起,您将导出带有状态代码的指标名称,例如
gunicorn_request_duration_200
或gunicorn_request_duration_404
等。
You can also modify it a little bit and move status codes to label by adding a configuration like below to your statsd_exporter
:您还可以通过将如下配置添加到您的
statsd_exporter
来稍微修改它并将状态代码移动到标签:
mappings:
- match: gunicorn.request.duration.*
name: "gunicorn_http_request_duration"
labels:
status: "$1"
job: "gunicorn_request_duration"
So your metrics will now look like this:因此,您的指标现在将如下所示:
# HELP gunicorn_http_request_duration Metric autogenerated by statsd_exporter.
# TYPE gunicorn_http_request_duration summary
gunicorn_http_request_duration{job="gunicorn_request_duration",status="200",quantile="0.5"} 2.4610000000000002e-06
gunicorn_http_request_duration{job="gunicorn_request_duration",status="200",quantile="0.9"} 2.4610000000000002e-06
gunicorn_http_request_duration{job="gunicorn_request_duration",status="200",quantile="0.99"} 2.4610000000000002e-06
gunicorn_http_request_duration_sum{job="gunicorn_request_duration",status="200"} 2.4610000000000002e-06
gunicorn_http_request_duration_count{job="gunicorn_request_duration",status="200"} 1
gunicorn_http_request_duration{job="gunicorn_request_duration",status="404",quantile="0.5"} 3.056e-06
gunicorn_http_request_duration{job="gunicorn_request_duration",status="404",quantile="0.9"} 3.056e-06
gunicorn_http_request_duration{job="gunicorn_request_duration",status="404",quantile="0.99"} 3.056e-06
gunicorn_http_request_duration_sum{job="gunicorn_request_duration",status="404"} 3.056e-06
gunicorn_http_request_duration_count{job="gunicorn_request_duration",status="404"} 1
And now to query all metrics except these with 5xx status in prometheus you can run:现在要在 prometheus 中查询除具有 5xx 状态的指标以外的所有指标,您可以运行:
gunicorn_http_request_duration{status=~"[^5].*"}
Let me know if it was helpful.让我知道它是否有帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.