bzp2010 commented on code in PR #12383:
URL: https://github.com/apache/apisix/pull/12383#discussion_r2171439188
##########
apisix/plugins/prometheus/exporter.lua:
##########
@@ -310,39 +309,50 @@ function _M.stream_log(conf, ctx)
end
-local ngx_status_items = {"active", "accepted", "handled", "total",
- "reading", "writing", "waiting"}
+-- FFI definitions for nginx connection status
+-- Based on
https://github.com/nginx/nginx/blob/master/src/event/ngx_event.c#L61-L78
+ffi.cdef[[
+ typedef uint64_t ngx_atomic_uint_t;
+ extern ngx_atomic_uint_t *ngx_stat_accepted;
+ extern ngx_atomic_uint_t *ngx_stat_handled;
+ extern ngx_atomic_uint_t *ngx_stat_requests;
+ extern ngx_atomic_uint_t *ngx_stat_active;
+ extern ngx_atomic_uint_t *ngx_stat_reading;
+ extern ngx_atomic_uint_t *ngx_stat_writing;
+ extern ngx_atomic_uint_t *ngx_stat_waiting;
+]]
+
local label_values = {}
+-- Mapping of status names to FFI global variables and metrics
+local status_mapping = {
+ {name = "active", var = "ngx_stat_active"},
+ {name = "accepted", var = "ngx_stat_accepted"},
+ {name = "handled", var = "ngx_stat_handled"},
+ {name = "total", var = "ngx_stat_requests"},
+ {name = "reading", var = "ngx_stat_reading"},
+ {name = "writing", var = "ngx_stat_writing"},
+ {name = "waiting", var = "ngx_stat_waiting"},
+}
+
+-- Use FFI to get nginx status directly from global variables
local function nginx_status()
Review Comment:
Underhood: The `ngx.var` is related to the request context, which is
inaccessible; nginx subrequests are also not available. Hence the rewrite to
FFI.
This not only eliminates the total connections offset caused by the APISIX
plugin requesting the `/apisix/status` API itself but also improves efficiency.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]