bzp2010 commented on code in PR #12383:
URL: https://github.com/apache/apisix/pull/12383#discussion_r2222320862


##########
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:
   @membphis 
   
   Actually, I recommend using these FFI API for data acquisition.
   I confirmed from the nginx code that this data is synchronized between 
workers by shared memory, so FFI API can access them.
   
   And this mechanism is entirely LuaJIT, not nginx fake request or openresty 
cosocket. 
   The former doesn't introduce any noise. 
   And when using any of the latter, no matter which one, the fetching behavior 
itself causes accepted/active/handled/reading/waiting/writing metrics to 
increase. waiting/writing. This is due to the fact that these mechanisms are 
always requesting APIs as network sockets, and they themselves cause the 
metrics to go up. This has always been a problem, and while I can understand it 
and it doesn't cause serious problems, it's always been confusing.
   
   I came up with this idea and @SkyeYoung  did it independently after some 
simple research, which I'm sure is not a difficult task for developers with 
almost any AI LLM assistance.
   These c-variables haven't changed in years, and I don't think it's going to 
change nearly as much in the future (there's no need), so it's not really an 
area that needs constant attention. If any future openresty/nginx breaks this 
convention, our test cases can find them.



-- 
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]

Reply via email to