Skip to content

Commit

Permalink
feat(prometheus): allow user configure DEFAULT_BUCKETS (#9673)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiangfucheng authored Jun 25, 2023
1 parent 3b31939 commit f3b549b
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 1 deletion.
7 changes: 6 additions & 1 deletion apisix/plugins/prometheus/exporter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,15 @@ function _M.http_init(prometheus_enabled_in_stream)
{"code", "route", "matched_uri", "matched_host", "service", "consumer", "node",
unpack(extra_labels("http_status"))})

local buckets = DEFAULT_BUCKETS
if attr and attr.default_buckets then
buckets = attr.default_buckets
end

metrics.latency = prometheus:histogram("http_latency",
"HTTP request latency in milliseconds per service in APISIX",
{"type", "route", "service", "consumer", "node", unpack(extra_labels("http_latency"))},
DEFAULT_BUCKETS)
buckets)

metrics.bandwidth = prometheus:counter("bandwidth",
"Total bandwidth in bytes consumed per service in APISIX",
Expand Down
6 changes: 6 additions & 0 deletions conf/config-default.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,12 @@ plugin_attr:
# bandwidth:
# extra_labels:
# - upstream_addr: $upstream_addr
# default_buckets:
# - 10
# - 50
# - 100
# - 200
# - 500
server-info:
report_ttl: 60 # live time for server info in etcd (unit: second)
dubbo-proxy:
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 @@ -77,6 +77,25 @@ plugin_attr:
- upstream_status: $upstream_status
```

### Specifying `default_buckets`

`DEFAULT_BUCKETS` is the default value for bucket array in `http_latency` metrics.

You can change the `DEFAULT_BUCKETS` by configuring `default_buckets` attribute in you configuration file.

Here is a configuration example:

```yaml title="conf/config.yaml"
plugin_attr:
prometheus:
default_buckets:
- 15
- 55
- 105
- 205
- 505
```

## API

This Plugin will add the API endpoint `/apisix/prometheus/metrics` or your custom export URI for exposing the metrics.
Expand Down
19 changes: 19 additions & 0 deletions docs/zh/latest/plugins/prometheus.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,25 @@ plugin_attr:
export_uri: /apisix/metrics
```
### 如何修改延迟指标中的 `default_buckets`

`DEFAULT_BUCKETS` 是 `http_latency` 指标中 bucket 数组的默认值。

你可以通过修改配置文件中的 `default_buckets` 来重新指定 `DEFAULT_BUCKETS`

配置示例如下:

```yaml title="conf/config.yaml"
plugin_attr:
prometheus:
default_buckets:
- 15
- 55
- 105
- 205
- 505
```

## API

`prometheus` 插件会增加 `/apisix/prometheus/metrics` 接口或者你自定义的 URI 来暴露其指标信息。
Expand Down
55 changes: 55 additions & 0 deletions t/plugin/prometheus4.t
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,58 @@ GET /hello
GET /apisix/prometheus/metrics
--- response_body eval
qr/apisix_http_status\{code="200",route="10",matched_uri="\/hello",matched_host="",service="",consumer="",node="127.0.0.1",dummy=""\} \d+/
=== TEST 7: set route
--- yaml_config
plugin_attr:
prometheus:
default_buckets:
- 15
- 55
- 105
- 205
- 505
--- config
location /t {
content_by_lua_block {
local t = require("lib.test_admin").test
local code, body = t('/apisix/admin/routes/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"prometheus": {}
},
"upstream": {
"nodes": {
"127.0.0.1:1980": 1
},
"type": "roundrobin"
},
"uri": "/hello1"
}]]
)
if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- pipelined_requests eval
["GET /t", "GET /hello1"]
--- response_body eval
["passed\n", "hello1 world\n"]
=== TEST 8: fetch metrics
--- request
GET /apisix/prometheus/metrics
--- response_body eval
qr/apisix_http_latency_bucket\{type="upstream",route="1",service="",consumer="",node="127.0.0.1",le="15"\} \d+
apisix_http_latency_bucket\{type="upstream",route="1",service="",consumer="",node="127.0.0.1",le="55"\} \d+
apisix_http_latency_bucket\{type="upstream",route="1",service="",consumer="",node="127.0.0.1",le="105"\} \d+
apisix_http_latency_bucket\{type="upstream",route="1",service="",consumer="",node="127.0.0.1",le="205"\} \d+
apisix_http_latency_bucket\{type="upstream",route="1",service="",consumer="",node="127.0.0.1",le="505"\} \d+/

0 comments on commit f3b549b

Please sign in to comment.