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


##########
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:
   I remember I tried using `resty.http` locally and ran into some problems. 
This was also one of the first methods suggested by @bzp2010. Because there 
were a lot of problems with the code, I didn't even submit it, and now I can't 
find it.
   
   As for cosocket, it's even harder for me, a beginner, to understand.
   
   Later, I switched to FFI under the suggestion of @bzp2010 , and found this 
approach to be actually very simple and straightforward.



-- 
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: notifications-unsubscr...@apisix.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to