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 #6055

Merged
merged 1 commit into from
Jan 13, 2022
Merged
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
20 changes: 11 additions & 9 deletions apisix/utils/batch-processor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,15 @@ local function schedule_func_exec(self, delay, batch)
end


local function set_metrics(self, count)
-- add batch metric for every route
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(count, self.label)
end
end


function execute_func(premature, self, batch)
if premature then
return
Expand Down Expand Up @@ -160,11 +169,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
self.label = {self.name, self.route_id, self.server_addr}
batch_metrics:set(#entries, self.label)
end
set_metrics(self, #entries)

if #entries == 1 then
self.first_entry_t = now()
Expand All @@ -190,10 +195,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
self.label = {self.name, self.route_id, self.server_addr}
batch_metrics:set(0, self.label)
end
set_metrics(self, 0)
end

for _, batch in ipairs(self.batch_to_process) do
Expand Down
121 changes: 115 additions & 6 deletions t/plugin/prometheus3.t
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,19 @@ repeat_each(1);
no_long_string();
no_shuffle();
no_root_location();

add_block_preprocessor(sub {
my ($block) = @_;

if (!$block->request) {
$block->set_value("request", "GET /t");
}

if ((!defined $block->error_log) && (!defined $block->no_error_log)) {
$block->set_value("no_error_log", "[error]");
}
});

run_tests;

__DATA__
Expand Down Expand Up @@ -74,12 +87,8 @@ __DATA__
ngx.say(body)
}
}
--- request
GET /t
--- response_body
passed
--- no_error_log
[error]



Expand All @@ -88,5 +97,105 @@ passed
["GET /hello", "GET /apisix/prometheus/metrics"]
--- error_code eval
[200, 200]
--- no_error_log
[error]



=== TEST 3: apisix_batch_process_entries, mess with global rules
zhendongcmss marked this conversation as resolved.
Show resolved Hide resolved
--- 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+/