-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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): extra labels from nginx var for http request metrics #7549
Changes from 4 commits
5c92517
7edf7a0
7a13df6
363a752
7cde6e8
3d0baab
8c64d3e
4b6b16f
3fdb55d
dc1c895
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -522,6 +522,14 @@ plugin_attr: | |
export_addr: | ||
ip: 127.0.0.1 | ||
port: 9091 | ||
# custom_labels: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be better to use:
So we can add more options to a metric in the future. And we don't need to require the label name should be the variable name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. And please add a comment to show that the commented configurations are the example but not the default value. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The labels of extra_labels should be a list, because fields of dictionary in lua table are out of order, which would cause the metric full name may change each time it reloads the conf. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We can provide order on the implementation side, for example sorting them before using them. |
||
# http_status: | ||
# - upstream_addr | ||
# - upstream_status | ||
# http_latency: | ||
# - upstream_addr | ||
# bandwidth: | ||
# - upstream_addr | ||
server-info: | ||
report_ttl: 60 # live time for server info in etcd (unit: second) | ||
dubbo-proxy: | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,6 +53,29 @@ plugin_attr: | |
export_uri: /apisix/metrics | ||
``` | ||
|
||
### Specifying `custom_labels` | ||
|
||
For http request related metrics, you could specify extra labels, which match the nginx variables. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's use |
||
|
||
If you specify label for nonexist nginx variable, the label value would be "nil". | ||
kingluo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
Currently, only below metrics are supported: | ||
|
||
* http_status | ||
* http_latency | ||
* bandwidth | ||
|
||
|
||
Here is a configuration example: | ||
|
||
```yaml title="conf/config.yaml" | ||
plugin_attr: | ||
prometheus: | ||
custom_labels: | ||
http_status: | ||
- upstream_addr | ||
- upstream_status | ||
|
||
## API | ||
|
||
This Plugin will add the API endpoint `/apisix/prometheus/metrics` or your custom export URI for exposing the metrics. | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
# | ||
# Licensed to the Apache Software Foundation (ASF) under one or more | ||
# contributor license agreements. See the NOTICE file distributed with | ||
# this work for additional information regarding copyright ownership. | ||
# The ASF licenses this file to You under the Apache License, Version 2.0 | ||
# (the "License"); you may not use this file except in compliance with | ||
# the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
# | ||
BEGIN { | ||
if ($ENV{TEST_NGINX_CHECK_LEAK}) { | ||
$SkipReason = "unavailable for the hup tests"; | ||
|
||
} else { | ||
$ENV{TEST_NGINX_USE_HUP} = 1; | ||
undef $ENV{TEST_NGINX_USE_STAP}; | ||
} | ||
} | ||
|
||
use t::APISIX 'no_plan'; | ||
|
||
add_block_preprocessor(sub { | ||
my ($block) = @_; | ||
|
||
if ((!defined $block->error_log) && (!defined $block->no_error_log)) { | ||
$block->set_value("no_error_log", "[error]"); | ||
} | ||
|
||
if (!defined $block->request) { | ||
$block->set_value("request", "GET /t"); | ||
} | ||
}); | ||
|
||
run_tests; | ||
|
||
__DATA__ | ||
|
||
=== TEST 1: pre-create public API route | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
|
||
local t = require("lib.test_admin").test | ||
local code = t('/apisix/admin/routes/metrics', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"plugins": { | ||
"public-api": {} | ||
}, | ||
"uri": "/apisix/prometheus/metrics" | ||
}]] | ||
) | ||
if code >= 300 then | ||
ngx.status = code | ||
return | ||
end | ||
} | ||
} | ||
--- error_code: 200 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually we don't need to specify |
||
|
||
|
||
|
||
=== TEST 2: set route | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local t = require("lib.test_admin").test | ||
local code, body = t('/apisix/admin/routes/10', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"plugins": { | ||
"prometheus": {} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uri": "/hello" | ||
}]] | ||
) | ||
|
||
if code >= 300 then | ||
ngx.status = code | ||
end | ||
ngx.say(body) | ||
} | ||
} | ||
--- response_body | ||
passed | ||
|
||
|
||
|
||
=== TEST 3: client request | ||
--- yaml_config | ||
plugin_attr: | ||
prometheus: | ||
custom_labels: | ||
bandwidth: | ||
- upstream_addr | ||
- upstream_status | ||
--- request | ||
GET /hello | ||
--- error_code: 200 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually we don't need to specify |
||
|
||
|
||
|
||
=== TEST 4: fetch the prometheus metric data | ||
--- request | ||
GET /apisix/prometheus/metrics | ||
--- response_body eval | ||
qr/apisix_bandwidth\{type="egress",route="10",service="",consumer="",node="127.0.0.1",upstream_addr="127.0.0.1:1980",upstream_status="200"\} \d+/ | ||
|
||
|
||
|
||
=== TEST 5: client request, label with nonexist ngx variable | ||
--- yaml_config | ||
plugin_attr: | ||
prometheus: | ||
custom_labels: | ||
http_status: | ||
- dummy | ||
bandwidth: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need the bandwidth? |
||
- upstream_addr | ||
- upstream_status | ||
--- request | ||
GET /hello | ||
--- error_code: 200 | ||
|
||
|
||
|
||
=== TEST 6: fetch the prometheus metric data, with nil label | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is no longer a nil label? |
||
--- request | ||
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="nil"\} \d+/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would suggest using
""
, which is language-independent.