diff --git a/apisix/plugins/prometheus/exporter.lua b/apisix/plugins/prometheus/exporter.lua index 1cb4a534cb9f..8e90326409bc 100644 --- a/apisix/plugins/prometheus/exporter.lua +++ b/apisix/plugins/prometheus/exporter.lua @@ -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", diff --git a/conf/config-default.yaml b/conf/config-default.yaml index d41df397b9e5..0e9802c7afb2 100755 --- a/conf/config-default.yaml +++ b/conf/config-default.yaml @@ -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: diff --git a/docs/en/latest/plugins/prometheus.md b/docs/en/latest/plugins/prometheus.md index df30025ce469..b2120ceafaf0 100644 --- a/docs/en/latest/plugins/prometheus.md +++ b/docs/en/latest/plugins/prometheus.md @@ -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. diff --git a/docs/zh/latest/plugins/prometheus.md b/docs/zh/latest/plugins/prometheus.md index b63d7f970710..f386d681e875 100644 --- a/docs/zh/latest/plugins/prometheus.md +++ b/docs/zh/latest/plugins/prometheus.md @@ -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 来暴露其指标信息。 diff --git a/t/plugin/prometheus4.t b/t/plugin/prometheus4.t index 999316a613bd..2a72736c8d3f 100644 --- a/t/plugin/prometheus4.t +++ b/t/plugin/prometheus4.t @@ -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+/