Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(promethues): add ngx.shared.DICT status #7412

Merged
merged 20 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions apisix/plugins/prometheus/exporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ local base_prometheus = require("prometheus")
local core = require("apisix.core")
local plugin = require("apisix.plugin")
local ipairs = ipairs
local pairs = pairs
local ngx = ngx
local re_gmatch = ngx.re.gmatch
local ffi = require("ffi")
Expand Down Expand Up @@ -122,6 +123,14 @@ function _M.http_init(prometheus_enabled_in_stream)
"Etcd modify index for APISIX keys",
{"key"})

metrics.shared_dict_capacity_bytes = prometheus:gauge("shared_dict_capacity_bytes",
"The capacity of each nginx shared DICT since APISIX start",
{"name"})

metrics.shared_dict_free_space_bytes = prometheus:gauge("shared_dict_free_space_bytes",
"The free space of each nginx shared DICT since APISIX start",
{"name"})

-- per service

-- The consumer label indicates the name of consumer corresponds to the
Expand Down Expand Up @@ -352,13 +361,26 @@ local function etcd_modify_index()
end


local function shared_dict_status()
local name = {}
for shared_dict_name, shared_dict in pairs(ngx.shared) do
name[1] = shared_dict_name
metrics.shared_dict_capacity_bytes:set(shared_dict:capacity(), name)
metrics.shared_dict_free_space_bytes:set(shared_dict:free_space(), name)
end
end


local function collect(ctx, stream_only)
if not prometheus or not metrics then
core.log.error("prometheus: plugin is not initialized, please make sure ",
" 'prometheus_metrics' shared dict is present in nginx template")
return 500, {message = "An unexpected error occurred"}
end

-- collect ngx.shared.DICT status
shared_dict_status()

-- across all services
nginx_status()

Expand Down
19 changes: 19 additions & 0 deletions docs/en/latest/plugins/prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ The following metrics are exported by the `prometheus` Plugin:
| node | IP address of the Upstream node. |

- Info: Information about the APISIX node.
- Shared dict: The capacity and free space of all nginx.shared.DICT in APISIX.

Here are the original metrics from APISIX:

Expand Down Expand Up @@ -272,6 +273,24 @@ apisix_http_latency_bucket{type="upstream",route="1",service="",consumer="",node
# HELP apisix_node_info Info of APISIX node
# TYPE apisix_node_info gauge
apisix_node_info{hostname="desktop-2022q8f-wsl"} 1
# HELP apisix_shared_dict_capacity_bytes The capacity of each nginx shared DICT since APISIX start
# TYPE apisix_shared_dict_capacity_bytes gauge
apisix_shared_dict_capacity_bytes{name="access-tokens"} 1048576
apisix_shared_dict_capacity_bytes{name="balancer-ewma"} 10485760
ccxhwmy marked this conversation as resolved.
Show resolved Hide resolved
apisix_shared_dict_capacity_bytes{name="balancer-ewma-last-touched-at"} 10485760
apisix_shared_dict_capacity_bytes{name="balancer-ewma-locks"} 10485760
apisix_shared_dict_capacity_bytes{name="discovery"} 1048576
apisix_shared_dict_capacity_bytes{name="etcd-cluster-health-check"} 10485760
...
# HELP apisix_shared_dict_free_space_bytes The free space of each nginx shared DICT since APISIX start
# TYPE apisix_shared_dict_free_space_bytes gauge
apisix_shared_dict_free_space_bytes{name="access-tokens"} 1032192
apisix_shared_dict_free_space_bytes{name="balancer-ewma"} 10412032
apisix_shared_dict_free_space_bytes{name="balancer-ewma-last-touched-at"} 10412032
apisix_shared_dict_free_space_bytes{name="balancer-ewma-locks"} 10412032
apisix_shared_dict_free_space_bytes{name="discovery"} 1032192
apisix_shared_dict_free_space_bytes{name="etcd-cluster-health-check"} 10412032
...
```

## Disable Plugin
Expand Down
21 changes: 20 additions & 1 deletion docs/zh/latest/plugins/prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,15 @@ scrape_configs:
| node | 上游节点的 IP 地址。 |

- Info: 当前 APISIX 节点信息。
- Shared dict: APISIX 中所有共享内存的容量以及剩余可用空间。

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

```shell
curl http://127.0.0.1:9091/apisix/prometheus/metrics
```

```
```shell
# HELP apisix_bandwidth Total bandwidth in bytes consumed per service in Apisix
# TYPE apisix_bandwidth counter
apisix_bandwidth{type="egress",route="",service="",consumer="",node=""} 8417
Expand Down Expand Up @@ -269,6 +270,24 @@ apisix_http_latency_bucket{type="upstream",route="1",service="",consumer="",node
# HELP apisix_node_info Info of APISIX node
# TYPE apisix_node_info gauge
apisix_node_info{hostname="APISIX"} 1
# HELP apisix_shared_dict_capacity_bytes The capacity of each nginx shared DICT since APISIX start
# TYPE apisix_shared_dict_capacity_bytes gauge
apisix_shared_dict_capacity_bytes{name="access-tokens"} 1048576
apisix_shared_dict_capacity_bytes{name="balancer-ewma"} 10485760
apisix_shared_dict_capacity_bytes{name="balancer-ewma-last-touched-at"} 10485760
apisix_shared_dict_capacity_bytes{name="balancer-ewma-locks"} 10485760
apisix_shared_dict_capacity_bytes{name="discovery"} 1048576
apisix_shared_dict_capacity_bytes{name="etcd-cluster-health-check"} 10485760
...
# HELP apisix_shared_dict_free_space_bytes The free space of each nginx shared DICT since APISIX start
# TYPE apisix_shared_dict_free_space_bytes gauge
apisix_shared_dict_free_space_bytes{name="access-tokens"} 1032192
apisix_shared_dict_free_space_bytes{name="balancer-ewma"} 10412032
apisix_shared_dict_free_space_bytes{name="balancer-ewma-last-touched-at"} 10412032
apisix_shared_dict_free_space_bytes{name="balancer-ewma-locks"} 10412032
apisix_shared_dict_free_space_bytes{name="discovery"} 1032192
apisix_shared_dict_free_space_bytes{name="etcd-cluster-health-check"} 10412032
...
```

## 禁用插件
Expand Down
11 changes: 11 additions & 0 deletions t/plugin/prometheus.t
Original file line number Diff line number Diff line change
Expand Up @@ -632,3 +632,14 @@ GET /apisix/prometheus/metrics
qr/apisix_/
--- response_body_unlike eval
qr/etcd/



=== TEST 42: fetch the prometheus shared dict internal-status data
--- http_config
lua_shared_dict test-shared-dict 10m;
--- request
GET /apisix/prometheus/metrics
--- response_body_like
.*apisix_shared_dict_capacity_bytes{name="test-shared-dict"} 10485760(?:.|\n)*
apisix_shared_dict_free_space_bytes{name="test-shared-dict"} \d+.*