prometheus

描述

prometheus 插件以 Prometheus 文档规定的格式上报指标到 Prometheus 中。

属性

名称类型必选项默认值描述
prefer_namebooleanfalse当设置为 true 时,将使用路由或服务的 name 标识请求所命中的路由或服务,否则使用其 id
prometheus - 图1note

多个路由或服务可以设置为相同的名称,所以当设置 prefer_nametrue 时,请规范路由和服务的命名,否则容易引起误解。

如何修改暴露指标的 uri

你可以在配置文件 ./conf/config.yamlplugin_attr 列表下修改默认的 URI。

名称类型默认值描述
export_uristring“/apisix/prometheus/metrics”暴露 Prometheus 指标的 URI。

配置示例如下:

./conf/config.yaml

  1. plugin_attr:
  2. prometheus:
  3. export_uri: /apisix/metrics

API

prometheus 插件会增加 /apisix/prometheus/metrics 接口或者你自定义的 URI 来暴露其指标信息。

这些指标由独立的 Prometheus 服务器地址公开。默认情况下,地址为 127.0.0.1:9091。你可以在配置文件(./conf/config.yaml)中修改,示例如下:

./conf/config.yaml

  1. plugin_attr:
  2. prometheus:
  3. export_addr:
  4. ip: ${{INTRANET_IP}}
  5. port: 9092

假设环境变量 INTRANET_IP172.1.1.1,那么 APISIX 将会在 172.1.1.1:9092 上暴露指标。

如果你仍然想要让指标暴露在数据面的端口(默认:9080)上,可参考如下配置:

./conf/config.yaml

  1. plugin_attr:
  2. prometheus:
  3. enable_export_server: false

你可以使用 public-api 插件来暴露该 URI。

启用插件

prometheus 插件可以使用空表 {} 开启。

你可以通过如下命令在指定路由上启用 prometheus 插件:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  3. {
  4. "uri": "/hello",
  5. "plugins": {
  6. "prometheus":{}
  7. },
  8. "upstream": {
  9. "type": "roundrobin",
  10. "nodes": {
  11. "127.0.0.1:1980": 1
  12. }
  13. }
  14. }'

提取指标

你可以从指定的 URL(默认:/apisix/prometheus/metrics)中提取指标数据:

  1. curl -i http://127.0.0.1:9091/apisix/prometheus/metrics

你可以将该 URI 地址添加到 Prometheus 中来提取指标数据,配置示例如下:

./prometheus.yml

  1. scrape_configs:
  2. - job_name: "apisix"
  3. scrape_interval: 15s # 该值会跟 Prometheus QL 中 rate 函数的时间范围有关系,rate 函数中的时间范围应该至少两倍于该值。
  4. metrics_path: "/apisix/prometheus/metrics"
  5. static_configs:
  6. - targets: ["127.0.0.1:9091"]

现在你可以在 Prometheus 控制台中检查状态:

checking status on prometheus dashboard

prometheus apisix in-depth metric view

使用 Grafana 绘制指标

prometheus 插件导出的指标可以在 Grafana 进行图形化绘制显示。

如果需要进行设置,请下载 APISIX’s Grafana dashboard 元数据 并导入到 Grafana 中。

你可以到 Grafana 官方 下载 Grafana 元数据。

Grafana chart-1

Grafana chart-2

Grafana chart-3

Grafana chart-4

可用的 HTTP 指标

prometheus 插件可以导出以下指标:

  • Status codes: 上游服务返回的 HTTP 状态码,可以统计到每个服务或所有服务的响应状态码的次数总和。属性如下所示:

    名称描述
    code上游服务返回的 HTTP 状态码。
    route与请求匹配的路由的 route_id,如果未匹配,则默认为空字符串。
    matched_uri与请求匹配的路由的 uri,如果未匹配,则默认为空字符串。
    matched_host与请求匹配的路由的 host,如果未匹配,则默认为空字符串。
    service与请求匹配的路由的 service_id。当路由缺少 service_id 时,则默认为 $host
    consumer与请求匹配的消费者的 consumer_name。如果未匹配,则默认为空字符串。
    node上游节点 IP 地址。
  • Bandwidth: 经过 APISIX 的总带宽(出口带宽和入口带宽),可以统计到每个服务的带宽总和。属性如下所示:

    名称描述
    type带宽的类型 (ingressegress)。
    route与请求匹配的路由的 route_id,如果未匹配,则默认为空字符串。
    service与请求匹配的路由的 service_id。当路由缺少 service_id 时,则默认为 $host
    consumer与请求匹配的消费者的 consumer_name。如果未匹配,则默认为空字符串。
    node消费者节点 IP 地址。
  • etcd reachability: APISIX 连接 etcd 的可用性,用 0 和 1 来表示,1 表示可用,0 表示不可用。

  • Connections: 各种的 NGINX 连接指标,如 active(正处理的活动连接数),reading(NGINX 读取到客户端的 Header 信息数),writing(NGINX 返回给客户端的 Header 信息数),已建立的连接数。

  • Batch process entries: 批处理未发送数据计数器,当你使用了批处理发送插件,比如:syslog, http-logger, tcp-logger, udp-logger, and zipkin,那么你将会在此指标中看到批处理当前尚未发送的数据的数量。

  • Latency: 每个服务的请求用时和 APISIX 处理耗时的直方图。属性如下所示:

    名称描述
    type该值可以是 apisixupstreamrequest,分别表示耗时的来源是 APISIX、上游以及两者总和。
    service与请求匹配的路由 的 service_id。当路由缺少 service_id 时,则默认为 $host
    consumer与请求匹配的消费者的 consumer_name。未匹配,则默认为空字符串。
    node上游节点的 IP 地址。
  • Info: 当前 APISIX 节点信息。

  • Shared dict: APISIX 中所有共享内存的容量以及剩余可用空间。

以下是 APISIX 的原始的指标数据集:

  1. curl http://127.0.0.1:9091/apisix/prometheus/metrics
  1. # HELP apisix_bandwidth Total bandwidth in bytes consumed per service in Apisix
  2. # TYPE apisix_bandwidth counter
  3. apisix_bandwidth{type="egress",route="",service="",consumer="",node=""} 8417
  4. apisix_bandwidth{type="egress",route="1",service="",consumer="",node="127.0.0.1"} 1420
  5. apisix_bandwidth{type="egress",route="2",service="",consumer="",node="127.0.0.1"} 1420
  6. apisix_bandwidth{type="ingress",route="",service="",consumer="",node=""} 189
  7. apisix_bandwidth{type="ingress",route="1",service="",consumer="",node="127.0.0.1"} 332
  8. apisix_bandwidth{type="ingress",route="2",service="",consumer="",node="127.0.0.1"} 332
  9. # HELP apisix_etcd_modify_indexes Etcd modify index for APISIX keys
  10. # TYPE apisix_etcd_modify_indexes gauge
  11. apisix_etcd_modify_indexes{key="consumers"} 0
  12. apisix_etcd_modify_indexes{key="global_rules"} 0
  13. apisix_etcd_modify_indexes{key="max_modify_index"} 222
  14. apisix_etcd_modify_indexes{key="prev_index"} 35
  15. apisix_etcd_modify_indexes{key="protos"} 0
  16. apisix_etcd_modify_indexes{key="routes"} 222
  17. apisix_etcd_modify_indexes{key="services"} 0
  18. apisix_etcd_modify_indexes{key="ssls"} 0
  19. apisix_etcd_modify_indexes{key="stream_routes"} 0
  20. apisix_etcd_modify_indexes{key="upstreams"} 0
  21. apisix_etcd_modify_indexes{key="x_etcd_index"} 223
  22. # HELP apisix_batch_process_entries batch process remaining entries
  23. # TYPE apisix_batch_process_entries gauge
  24. apisix_batch_process_entries{name="http-logger",route_id="9",server_addr="127.0.0.1"} 1
  25. apisix_batch_process_entries{name="sls-logger",route_id="9",server_addr="127.0.0.1"} 1
  26. apisix_batch_process_entries{name="tcp-logger",route_id="9",server_addr="127.0.0.1"} 1
  27. apisix_batch_process_entries{name="udp-logger",route_id="9",server_addr="127.0.0.1"} 1
  28. apisix_batch_process_entries{name="sys-logger",route_id="9",server_addr="127.0.0.1"} 1
  29. apisix_batch_process_entries{name="zipkin_report",route_id="9",server_addr="127.0.0.1"} 1
  30. # HELP apisix_etcd_reachable Config server etcd reachable from Apisix, 0 is unreachable
  31. # TYPE apisix_etcd_reachable gauge
  32. apisix_etcd_reachable 1
  33. # HELP apisix_http_status HTTP status codes per service in Apisix
  34. # TYPE apisix_http_status counter
  35. apisix_http_status{code="200",route="1",matched_uri="/hello",matched_host="",service="",consumer="",node="127.0.0.1"} 4
  36. apisix_http_status{code="200",route="2",matched_uri="/world",matched_host="",service="",consumer="",node="127.0.0.1"} 4
  37. apisix_http_status{code="404",route="",matched_uri="",matched_host="",service="",consumer="",node=""} 1
  38. # HELP apisix_http_requests_total The total number of client requests
  39. # TYPE apisix_http_requests_total gauge
  40. apisix_http_requests_total 1191780
  41. # HELP apisix_nginx_http_current_connections Number of HTTP connections
  42. # TYPE apisix_nginx_http_current_connections gauge
  43. apisix_nginx_http_current_connections{state="accepted"} 11994
  44. apisix_nginx_http_current_connections{state="active"} 2
  45. apisix_nginx_http_current_connections{state="handled"} 11994
  46. apisix_nginx_http_current_connections{state="reading"} 0
  47. apisix_nginx_http_current_connections{state="waiting"} 1
  48. apisix_nginx_http_current_connections{state="writing"} 1
  49. # HELP apisix_nginx_metric_errors_total Number of nginx-lua-prometheus errors
  50. # TYPE apisix_nginx_metric_errors_total counter
  51. apisix_nginx_metric_errors_total 0
  52. # HELP apisix_http_latency HTTP request latency in milliseconds per service in APISIX
  53. # TYPE apisix_http_latency histogram
  54. apisix_http_latency_bucket{type="apisix",route="1",service="",consumer="",node="127.0.0.1",le="1"} 1
  55. apisix_http_latency_bucket{type="apisix",route="1",service="",consumer="",node="127.0.0.1",le="2"} 1
  56. apisix_http_latency_bucket{type="request",route="1",service="",consumer="",node="127.0.0.1",le="1"} 1
  57. apisix_http_latency_bucket{type="request",route="1",service="",consumer="",node="127.0.0.1",le="2"} 1
  58. apisix_http_latency_bucket{type="upstream",route="1",service="",consumer="",node="127.0.0.1",le="1"} 1
  59. apisix_http_latency_bucket{type="upstream",route="1",service="",consumer="",node="127.0.0.1",le="2"} 1
  60. ...
  61. # HELP apisix_node_info Info of APISIX node
  62. # TYPE apisix_node_info gauge
  63. apisix_node_info{hostname="APISIX"} 1
  64. # HELP apisix_shared_dict_capacity_bytes The capacity of each nginx shared DICT since APISIX start
  65. # TYPE apisix_shared_dict_capacity_bytes gauge
  66. apisix_shared_dict_capacity_bytes{name="access-tokens"} 1048576
  67. apisix_shared_dict_capacity_bytes{name="balancer-ewma"} 10485760
  68. apisix_shared_dict_capacity_bytes{name="balancer-ewma-last-touched-at"} 10485760
  69. apisix_shared_dict_capacity_bytes{name="balancer-ewma-locks"} 10485760
  70. apisix_shared_dict_capacity_bytes{name="discovery"} 1048576
  71. apisix_shared_dict_capacity_bytes{name="etcd-cluster-health-check"} 10485760
  72. ...
  73. # HELP apisix_shared_dict_free_space_bytes The free space of each nginx shared DICT since APISIX start
  74. # TYPE apisix_shared_dict_free_space_bytes gauge
  75. apisix_shared_dict_free_space_bytes{name="access-tokens"} 1032192
  76. apisix_shared_dict_free_space_bytes{name="balancer-ewma"} 10412032
  77. apisix_shared_dict_free_space_bytes{name="balancer-ewma-last-touched-at"} 10412032
  78. apisix_shared_dict_free_space_bytes{name="balancer-ewma-locks"} 10412032
  79. apisix_shared_dict_free_space_bytes{name="discovery"} 1032192
  80. apisix_shared_dict_free_space_bytes{name="etcd-cluster-health-check"} 10412032
  81. ...

禁用插件

当你需要禁用 prometheus 插件时,可以通过以下命令删除相应的 JSON 配置,APISIX 将会自动重新加载相关配置,无需重启服务:

  1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "uri": "/hello",
  4. "plugins": {},
  5. "upstream": {
  6. "type": "roundrobin",
  7. "nodes": {
  8. "127.0.0.1:80": 1
  9. }
  10. }
  11. }'

如何启用 TCP/UDP 指标

prometheus - 图8IMPORTANT

该功能要求 APISIX 运行在 APISIX-Base 上。

我们也可以通过 prometheus 插件采集 TCP/UDP 指标。

首先,确保 prometheus 插件已经在你的配置文件(./conf/config.yaml)中启用:

conf/config.yaml

  1. stream_plugins:
  2. - ...
  3. - prometheus

接着你需要在 stream 路由中配置该插件:

  1. curl http://127.0.0.1:9080/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
  2. {
  3. "plugins": {
  4. "prometheus":{}
  5. },
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:80": 1
  10. }
  11. }
  12. }'

可用的 TCP/UDP 指标

以下是将 APISIX 作为 L4 代理时可用的指标:

  • Stream Connections: 路由级别的已处理连接数。具有的维度:

    名称描述
    route匹配的 stream 路由 ID。
  • Connections: 各种的 NGINX 连接指标,如 activereadingwriting 等已建立的连接数。

  • Info: 当前 APISIX 节点信息。

以下是 APISIX 指标的示例:

  1. curl http://127.0.0.1:9091/apisix/prometheus/metrics
  1. ...
  2. # HELP apisix_node_info Info of APISIX node
  3. # TYPE apisix_node_info gauge
  4. apisix_node_info{hostname="desktop-2022q8f-wsl"} 1
  5. # HELP apisix_stream_connection_total Total number of connections handled per stream route in APISIX
  6. # TYPE apisix_stream_connection_total counter
  7. apisix_stream_connection_total{route="1"} 1