diff --git a/apisix/utils/batch-processor.lua b/apisix/utils/batch-processor.lua index b94a0dd4f1e3..8ba48136f664 100644 --- a/apisix/utils/batch-processor.lua +++ b/apisix/utils/batch-processor.lua @@ -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 @@ -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() @@ -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 diff --git a/t/plugin/prometheus3.t b/t/plugin/prometheus3.t index eb11d37bd3a2..4f3df1f4451a 100644 --- a/t/plugin/prometheus3.t +++ b/t/plugin/prometheus3.t @@ -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__ @@ -74,12 +87,8 @@ __DATA__ ngx.say(body) } } ---- request -GET /t --- response_body passed ---- no_error_log -[error] @@ -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 +--- 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+/