简体   繁体   English

Prometheus statsd-exporter - 如何在请求持续时间指标(直方图)中标记状态代码

[英]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_200gunicorn_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.

相关问题 在kubernetes上将statsd-exporter设置为守护程序,并从pod中向其发送指标 - Setup statsd-exporter as daemon on Kubernetes and send metrics to it from pods Kubernetes 中的 apiserver_request_duration_seconds prometheus 指标是什么意思? - What does apiserver_request_duration_seconds prometheus metric in Kubernetes mean? 使用 Prometheus 的平均请求持续时间 - Average request duration using Prometheus 如何在 Kubernetes 中安装用于 Prometheus 监控的 MongoDb 导出器 - How to Install MongoDb Exporter for Prometheus Monitoring in Kubernetes 如何在普罗米修斯度量标准中复制标签 - How replicate label in prometheus metric 如何获取 JSON 格式的 Prometheus Node Exporter 指标 - How to get Prometheus Node Exporter metrics with JSON format 如何在 Prometheus/Grafana 中配置 Rabbitmq Metric Kubernetes - How to configure Rabbitmq Metric in Prometheus/Grafana Kubernetes 适用于Prometheus的Docker Exporter - Docker Exporter for Prometheus 如何在 helm 托管环境中安装 prometheus exporter 插件到 openSearch - How to install prometheus exporter plugin to openSearch in helm managed environment 如何减少 Prometheus(Federation) 抓取持续时间 - How To Reduce Prometheus(Federation) Scrape Duration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM