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

fix: the prometheus lables are inconsistent when using the same batch-processor instance on multi plugins #6047

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 2 additions & 2 deletions apisix/utils/batch-processor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ function batch_processor:push(entry)
local entries = self.entry_buffer.entries
table.insert(entries, entry)
-- add batch metric for every route
if batch_metrics then
if batch_metrics and self.name and self.route_id and self.server_addr then
self.label = {self.name, self.route_id, self.server_addr}
batch_metrics:set(#entries, self.label)
end
Expand Down Expand Up @@ -189,7 +189,7 @@ function batch_processor:process_buffer()
"buffercount[", #self.entry_buffer.entries ,"]")
self.batch_to_process[#self.batch_to_process + 1] = self.entry_buffer
self.entry_buffer = {entries = {}, retry_count = 0}
if batch_metrics then
if batch_metrics and self.name and self.route_id and self.server_addr then
self.label = {self.name, self.route_id, self.server_addr}
batch_metrics:set(0, self.label)
end
Expand Down
103 changes: 103 additions & 0 deletions t/plugin/prometheus3.t
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,106 @@ passed
[200, 200]
--- no_error_log
[error]



=== TEST 3: apisix_batch_process_entries, mess with global rules
--- 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": "/batch-process-metrics-aa"
}]]
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end

local code, body = t('/apisix/admin/plugin_metadata/error-log-logger',
ngx.HTTP_PUT,
[[{
"tcp": {
"host": "127.0.0.1",
"port": 1999
},
"max_retry_count": 1000,
"level": "NOTICE"
}]]
)
if code >= 300 then
ngx.status = code
ngx.say(body)
return
end

local code, body = t('/apisix/admin/global_rules/1',
ngx.HTTP_PUT,
[[{
"plugins": {
"http-logger": {
"uri": "http://127.0.0.1:1979"
}
}
}]]
)

if code >= 300 then
ngx.status = code
end
ngx.say(body)
}
}
--- response_body
passed



=== TEST 4: check metrics
--- yaml_config
plugins:
- error-log-logger
- prometheus
- http-logger
--- request
GET /t
--- config
location /t {
content_by_lua_block {
local http = require "resty.http"
local httpc = http.new()
local uri = "http://127.0.0.1:" .. ngx.var.server_port
.. "/batch-process-metrics-aa"
local res, err = httpc:request_uri(uri, {method = "GET"})
if not res then
ngx.say(err)
return
end

ngx.sleep(2)
local uri = "http://127.0.0.1:" .. ngx.var.server_port
.. "/apisix/prometheus/metrics"
local res, err = httpc:request_uri(uri, {method = "GET"})
if not res then
ngx.say(err)
return
end
ngx.say(res.body)
}
}
--- response_body_like eval
qr/apisix_batch_process_entries\{name="http logger",route_id="1",server_addr="127.0.0.1"\} \d+/