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(prometheus): allow user configure DEFAULT_BUCKETS #9673

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
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
59 changes: 59 additions & 0 deletions t/plugin/prometheus4.t
Original file line number Diff line number Diff line change
Expand Up @@ -134,3 +134,62 @@ 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
--- 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)
}
}
--- response_body
passed



=== TEST 8: set default_buckets
--- yaml_config
plugin_attr:
prometheus:
default_buckets:
- 15
- 55
- 105
- 205
- 505
--- request
GET /hello1
jiangfucheng marked this conversation as resolved.
Show resolved Hide resolved



=== TEST 9: 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+/