-
Notifications
You must be signed in to change notification settings - Fork 2.6k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
feat: add batch process metrics #3070
Changes from 8 commits
51ad65f
38ca888
5bce894
40a88ac
9ace229
40be48a
bbabcf3
c2562b2
7a9def2
081ce7d
c3c78ac
75f4b14
045ba1a
9fe4ad3
ad38eb9
62ad1a4
6f7ecd5
21e0323
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 |
---|---|---|
|
@@ -27,6 +27,8 @@ local batch_processor_mt = { | |
} | ||
local execute_func | ||
local create_buffer_timer | ||
local batch_metrics | ||
local prometheus = require("apisix.plugins.prometheus.exporter") | ||
|
||
|
||
local schema = { | ||
|
@@ -131,7 +133,9 @@ function batch_processor:new(func, config) | |
entry_buffer = { entries = {}, retry_count = 0}, | ||
is_timer_running = false, | ||
first_entry_t = 0, | ||
last_entry_t = 0 | ||
last_entry_t = 0, | ||
route_id = config.route_id, | ||
server_addr = config.server_addr, | ||
} | ||
|
||
return setmetatable(processor, batch_processor_mt) | ||
|
@@ -146,8 +150,20 @@ function batch_processor:push(entry) | |
return | ||
end | ||
|
||
if not batch_metrics and prometheus.get_prometheus() and self.name | ||
and self.route_id and self.server_addr then | ||
batch_metrics = prometheus.get_prometheus():gauge("batch_process_entries", | ||
"batch process remaining entries", | ||
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. 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. added. |
||
{"name", "route_id", "server_addr"}) | ||
end | ||
|
||
local entries = self.entry_buffer.entries | ||
table.insert(entries, entry) | ||
-- add batch metric for every route | ||
if batch_metrics then | ||
local label = {self.name, self.route_id, self.server_addr} | ||
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. Look like we can store the 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. fixed. |
||
batch_metrics:set(#entries, label) | ||
end | ||
|
||
if #entries == 1 then | ||
self.first_entry_t = now() | ||
|
@@ -173,11 +189,16 @@ 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 | ||
local label = {self.name, self.route_id, self.server_addr} | ||
batch_metrics:set(0, label) | ||
end | ||
end | ||
|
||
for _, batch in ipairs(self.batch_to_process) do | ||
schedule_func_exec(self, 0, batch) | ||
end | ||
|
||
self.batch_to_process = {} | ||
end | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1088,3 +1088,207 @@ GET /apisix/prometheus/metrics | |
--- error_code: 200 | ||
--- no_error_log | ||
[error] | ||
|
||
|
||
|
||
=== TEST 62: set batch plugins | ||
--- config | ||
location /t { | ||
content_by_lua_block { | ||
local t = require("lib.test_admin").test | ||
local code, body = t('/apisix/admin/routes/9', | ||
ngx.HTTP_PUT, | ||
[[{ | ||
"methods": ["GET"], | ||
"plugins": { | ||
"prometheus": {}, | ||
"syslog": { | ||
"host": "127.0.0.1", | ||
"include_req_body": false, | ||
"max_retry_times": 1, | ||
"tls": false, | ||
"retry_interval": 1, | ||
"batch_max_size": 1000, | ||
"buffer_duration": 60, | ||
"port": 0, | ||
"name": "sys-logger", | ||
"flush_limit": 4096, | ||
"sock_type": "tcp", | ||
"timeout": 3, | ||
"drop_limit": 1048576, | ||
"pool_size": 5 | ||
}, | ||
"zipkin": { | ||
"endpoint": "http://127.0.0.1:9447", | ||
"service_name": "APISIX", | ||
"sample_ratio": 1 | ||
}, | ||
"http-logger": { | ||
"inactive_timeout": 5, | ||
"include_req_body": false, | ||
"timeout": 3, | ||
"name": "http-logger", | ||
"retry_delay": 1, | ||
"buffer_duration": 60, | ||
"uri": "http://127.0.0.1:19080/report", | ||
"concat_method": "json", | ||
"batch_max_size": 1000, | ||
"max_retry_count": 0 | ||
} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uris": ["/batch-process-metrics"] | ||
}]] | ||
) | ||
|
||
if code >= 300 then | ||
ngx.status = code | ||
end | ||
ngx.say(body) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
passed | ||
--- no_error_log | ||
[error] | ||
|
||
|
||
|
||
=== TEST 63: hit batch-process-metrics | ||
--- request | ||
GET /batch-process-metrics | ||
--- error_code: 404 | ||
|
||
|
||
|
||
=== TEST 64: check sys logger metrics | ||
--- request | ||
GET /apisix/prometheus/metrics | ||
--- error_code: 200 | ||
--- response_body_like eval | ||
qr/apisix_batch_process_entries{name="sys-logger",route_id="9"/ | ||
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. Escape 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. plugin set error. 😄 |
||
|
||
|
||
|
||
=== TEST 65: check zipkin log metrics | ||
--- request | ||
GET /apisix/prometheus/metrics | ||
--- error_code: 200 | ||
--- response_body_like eval | ||
qr/apisix_batch_process_entries{name="zipkin_report",route_id="9"/ | ||
|
||
|
||
|
||
=== TEST 66: check http log metrics | ||
--- request | ||
GET /apisix/prometheus/metrics | ||
--- error_code: 200 | ||
--- response_body_like eval | ||
qr/apisix_batch_process_entries{name="http-logger",route_id="9"/ | ||
|
||
|
||
|
||
=== TEST 67: set batch plugins | ||
--- 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, | ||
[[{ | ||
"methods": ["GET"], | ||
"plugins": { | ||
"prometheus": {}, | ||
"tcp-logger": { | ||
"host": "127.0.0.1", | ||
"include_req_body": false, | ||
"timeout": 1000, | ||
"name": "tcp-logger", | ||
"retry_delay": 1, | ||
"buffer_duration": 60, | ||
"port": 0, | ||
"batch_max_size": 1000, | ||
"inactive_timeout": 5, | ||
"tls": false, | ||
"max_retry_count": 0 | ||
}, | ||
"udp-logger": { | ||
"host": "127.0.0.1", | ||
"port": 0, | ||
"include_req_body": false, | ||
"timeout": 3, | ||
"batch_max_size": 1000, | ||
"name": "udp-logger", | ||
"inactive_timeout": 5, | ||
"buffer_duration": 60 | ||
}, | ||
"sls-logger": { | ||
"host": "127.0.0.1", | ||
"batch_max_size": 1000, | ||
"name": "sls-logger", | ||
"inactive_timeout": 5, | ||
"logstore": "your_logstore", | ||
"buffer_duration": 60, | ||
"port": 10009, | ||
"max_retry_count": 0, | ||
"retry_delay": 1, | ||
"access_key_id": "your_access_id", | ||
"access_key_secret": "your_key_secret", | ||
"timeout": 5000, | ||
"project": "your_project" | ||
} | ||
}, | ||
"upstream": { | ||
"nodes": { | ||
"127.0.0.1:1980": 1 | ||
}, | ||
"type": "roundrobin" | ||
}, | ||
"uris": ["/batch-process-metrics-10"] | ||
}]] | ||
) | ||
|
||
if code >= 300 then | ||
ngx.status = code | ||
end | ||
ngx.say(body) | ||
} | ||
} | ||
--- request | ||
GET /t | ||
--- response_body | ||
passed | ||
--- no_error_log | ||
[error] | ||
|
||
|
||
|
||
=== TEST 68: tigger metircs batch-process-metrics | ||
--- request | ||
GET /batch-process-metrics-10 | ||
--- error_code: 404 | ||
|
||
|
||
|
||
=== TEST 69: check tcp log metrics | ||
--- request | ||
GET /apisix/prometheus/metrics | ||
--- error_code: 200 | ||
--- response_body_like eval | ||
qr/apisix_batch_process_entries{name="tcp-logger",route_id="10"/ | ||
|
||
|
||
|
||
=== TEST 70: check udp log metrics | ||
--- request | ||
GET /apisix/prometheus/metrics | ||
--- error_code: 200 | ||
--- response_body_like eval | ||
qr/apisix_batch_process_entries{name="udp-logger",route_id="10"/ |
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.
We don't need this if we can't share the table.
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.
got it.