apisix 开启 prometheus

安装完了 apisix 后,想对流量做一个监控,发现 apisix 已经有了该项功能,于是记录一下。

本次环境为k8s,采用的是helm 安装的 apisix + apisix-ingress-controller.

开启 prometheus

默认情况下,apisix是已经开启了prometheus。如下:

1
2
3
plugin_attr:
prometheus:
enable_export_server: false

这种方式是通过apisix-admin来暴露metrics的。但是访问的话还是需要通过public-api插件去访问。

添加ingress

下面是我的ingress 配置。记得将namespace配置成和apisix-ingress一致。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
apiVersion: apisix.apache.org/v2
kind: ApisixRoute
metadata:
name: prometheus-route
namespace: ingress-apisix
spec:
http:
- name: prometheus-metrics
match:
hosts:
- apisix-prometheus.cootekos.com
paths:
- /apisix/prometheus/metrics*
backends:
- serviceName: apisix-admin
servicePort: 9180
plugins:
- name: public-api
enable: true

访问

访问https://your-domain/apisix/prometheus/metrics 就可以看到指标了。但是缺少了很多关键性的指标。还需要在apisix-dashboard中开启prometheus插件。

prometheus插件

然后访问连接可以拿到指标:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
...
apisix_http_latency_bucket{type="upstream",route="ingress-apisix_apisix-dashboard_dashboard",service="",consumer="",node="10.19.74.97",le="10000"} 416
apisix_http_latency_bucket{type="upstream",route="ingress-apisix_apisix-dashboard_dashboard",service="",consumer="",node="10.19.74.97",le="30000"} 416
apisix_http_latency_bucket{type="upstream",route="ingress-apisix_apisix-dashboard_dashboard",service="",consumer="",node="10.19.74.97",le="60000"} 416
apisix_http_latency_bucket{type="upstream",route="ingress-apisix_apisix-dashboard_dashboard",service="",consumer="",node="10.19.74.97",le="+Inf"} 416
apisix_http_latency_count{type="apisix",route="721ea032",service="",consumer="",node="10.19.74.97"} 198
apisix_http_latency_count{type="apisix",route="a0eeefc1",service="",consumer="",node=""} 887
apisix_http_latency_count{type="apisix",route="ingress-apisix_apisix-dashboard_dashboard",service="",consumer="",node="10.19.74.97"} 416
apisix_http_latency_count{type="apisix",route="ingress-apisix_prometheus-route_public-api",service="",consumer="",node=""} 4835
apisix_http_latency_count{type="request",route="721ea032",service="",consumer="",node="10.19.74.97"} 198
apisix_http_latency_count{type="request",route="a0eeefc1",service="",consumer="",node=""} 887
apisix_http_latency_count{type="request",route="ingress-apisix_apisix-dashboard_dashboard",service="",consumer="",node="10.19.74.97"} 416
apisix_http_latency_count{type="request",route="ingress-apisix_prometheus-route_public-api",service="",consumer="",node=""} 4835
apisix_http_latency_count{type="upstream",route="721ea032",service="",consumer="",node="10.19.74.97"} 198
apisix_http_latency_count{type="upstream",route="ingress-apisix_apisix-dashboard_dashboard",service="",consumer="",node="10.19.74.97"} 416
apisix_http_latency_sum{type="apisix",route="721ea032",service="",consumer="",node="10.19.74.97"} 32.007696151733
apisix_http_latency_sum{type="apisix",route="a0eeefc1",service="",consumer="",node=""} 8282.9859256744
apisix_http_latency_sum{type="apisix",route="ingress-apisix_apisix-dashboard_dashboard",service="",consumer="",node="10.19.74.97"} 70.012517929077
apisix_http_latency_sum{type="apisix",route="ingress-apisix_prometheus-route_public-api",service="",consumer="",node=""} 43880.94496727
apisix_http_latency_sum{type="request",route="721ea032",service="",consumer="",node="10.19.74.97"} 615.00382423401
apisix_http_latency_sum{type="request",route="a0eeefc1",service="",consumer="",node=""} 8282.9859256744
apisix_http_latency_sum{type="request",route="ingress-apisix_apisix-dashboard_dashboard",service="",consumer="",node="10.19.74.97"} 1631.0000419617
apisix_http_latency_sum{type="request",route="ingress-apisix_prometheus-route_public-api",service="",consumer="",node=""} 43880.94496727
apisix_http_latency_sum{type="upstream",route="721ea032",service="",consumer="",node="10.19.74.97"} 630
apisix_http_latency_sum{type="upstream",route="ingress-apisix_apisix-dashboard_dashboard",service="",consumer="",node="10.19.74.97"} 1639
# HELP apisix_http_requests_total The total number of client requests since APISIX started
# TYPE apisix_http_requests_total gauge
apisix_http_requests_total 16232
# HELP apisix_http_status HTTP status codes per service in APISIX
# TYPE apisix_http_status counter
apisix_http_status{code="200",route="721ea032",matched_uri="/*",matched_host="apisix-dashboard.cootekos.com",service="",consumer="",node="10.19.74.97"} 22
apisix_http_status{code="200",route="a0eeefc1",matched_uri="/apisix/prometheus/metrics*",matched_host="apisix-prometheus.cootekos.com",service="",consumer="",node=""} 887
apisix_http_status{code="200",route="ingress-apisix_apisix-dashboard_dashboard",matched_uri="/*",matched_host="apisix-dashboard.cootekos.com",service="",consumer="",node="10.19.74.97"} 102
apisix_http_status{code="200",route="ingress-apisix_prometheus-route_public-api",matched_uri="/apisix/prometheus/metrics*",matched_host="apisix-prometheus.cootekos.com",service="",consumer="",node=""} 4835
apisix_http_status{code="304",route="721ea032",matched_uri="/*",matched_host="apisix-dashboard.cootekos.com",service="",consumer="",node="10.19.74.97"} 170
apisix_http_status{code="304",route="ingress-apisix_apisix-dashboard_dashboard",matched_uri="/*",matched_host="apisix-dashboard.cootekos.com",service="",consumer="",node="10.19.74.97"} 312
apisix_http_status{code="401",route="721ea032",matched_uri="/*",matched_host="apisix-dashboard.cootekos.com",service="",consumer="",node="10.19.74.97"} 6
apisix_http_status{code="401",route="ingress-apisix_apisix-dashboard_dashboard",matched_uri="/*",matched_host="apisix-dashboard.cootekos.com",service="",consumer="",node="10.19.74.97"} 2
# HELP apisix_nginx_http_current_connections Number of HTTP connections
# TYPE apisix_nginx_http_current_connections gauge
apisix_nginx_http_current_connections{state="accepted"} 185827
apisix_nginx_http_current_connections{state="active"} 5
apisix_nginx_http_current_connections{state="handled"} 185827
apisix_nginx_http_current_connections{state="reading"} 0
apisix_nginx_http_current_connections{state="waiting"} 4
apisix_nginx_http_current_connections{state="writing"} 1
# HELP apisix_nginx_metric_errors_total Number of nginx-lua-prometheus errors
# TYPE apisix_nginx_metric_errors_total counter
apisix_nginx_metric_errors_total 0
....

prefer_name 配置

根据文档中的说明: prefer_name配置开启,将会打印route/service的名字,而不是ID。我这边需要知道对应的service名字,所以开启了。

配置grafana

直接下载 grafana 面板的json文件。然后导入进去就行。

效果图如下:

grafana-apisix


apisix 开启 prometheus
https://randzz.cn/18cd4dbdf01d/apisix-开启-prometheus/
作者
Ezreal Rao
发布于
2023年3月2日
许可协议