1- load_module "/usr/lib/nginx/modules/ngx_stream_module.so" ;
2-
31worker_processes 1;
4-
5- error_log stderr notice;
6- pid /var/run/nginx.pid ;
2+ error_log stderr notice;
3+ pid /var/run/nginx.pid ;
74
85events {
96 worker_connections 1024 ;
@@ -13,6 +10,21 @@ http {
1310 map_hash_bucket_size 128 ;
1411 map_hash_max_size 4096 ;
1512
13+ client_body_temp_path /var/run/nginx-client-body;
14+ proxy_temp_path /var/run/nginx-proxy;
15+ fastcgi_temp_path /var/run/nginx-fastcgi;
16+ uwsgi_temp_path /var/run/nginx-uwsgi;
17+ scgi_temp_path /var/run/nginx-scgi;
18+
19+ lua_shared_dict metrics 10M ;
20+
21+ init_by_lua_block {
22+ -- Initialiser les compteurs HTTP
23+ ngx.shared.metrics:set ( "http_requests_total" , 0)
24+ ngx.shared.metrics:set ( "healthz_requests_total" , 0)
25+ ngx.shared.metrics:set ( "metrics_requests_total" , 0)
26+ }
27+
1628 log_format main '$remote_addr - $remote_user [$time_local] '
1729 '"$request" $status $body_bytes_sent '
1830 '"$http_referer" "$http_user_agent"' ;
@@ -24,11 +36,82 @@ http {
2436
2537 location / {
2638 return 404 ;
39+
40+ log_by_lua_block {
41+ local metrics = ngx.shared.metrics
42+ local total = metrics:get( "http_requests_total" ) or 0
43+ metrics:set ( "http_requests_total" , total + 1)
44+ }
2745 }
2846
2947 location /healthz {
3048 default_type text/plain;
3149 return 200 "OK\n " ;
50+
51+ log_by_lua_block {
52+ local metrics = ngx.shared.metrics
53+
54+ local healthz = metrics:get( "healthz_requests_total" ) or 0
55+ metrics:set ( "healthz_requests_total" , healthz + 1)
56+
57+ local total = metrics:get( "http_requests_total" ) or 0
58+ metrics:set ( "http_requests_total" , total + 1)
59+ }
60+ }
61+
62+ location /metrics {
63+ default_type text/plain;
64+ content_by_lua_block {
65+ local metrics = ngx.shared.metrics
66+ local stream_metrics = ngx.shared.stream_metrics
67+ local output = {}
68+
69+ table.insert( output, "# HELP nginx_up Nginx is running" )
70+ table.insert( output, "# TYPE nginx_up gauge" )
71+ table.insert( output, "nginx_up 1" )
72+ table.insert( output, "" )
73+
74+ table.insert( output, "# HELP nginx_http_requests_total Total HTTP requests" )
75+ table.insert( output, "# TYPE nginx_http_requests_total counter" )
76+ table.insert( output, "nginx_http_requests_total " .. ( metrics:get( "http_requests_total" ) or 0))
77+ table.insert( output, "" )
78+
79+ table.insert( output, "# HELP nginx_healthz_requests_total Total healthz requests" )
80+ table.insert( output, "# TYPE nginx_healthz_requests_total counter" )
81+ table.insert( output, "nginx_healthz_requests_total " .. ( metrics:get( "healthz_requests_total" ) or 0))
82+ table.insert( output, "" )
83+
84+ if stream_metrics then
85+ table.insert( output, "# HELP nginx_stream_connections_total Total stream connections" )
86+ table.insert( output, "# TYPE nginx_stream_connections_total counter" )
87+ table.insert( output, "nginx_stream_connections_total " .. ( stream_metrics:get( "stream_connections_total" ) or 0))
88+ table.insert( output, "" )
89+
90+ local sum = stream_metrics:get( "upstream_connect_time_sum" ) or 0
91+ local count = stream_metrics:get( "upstream_connect_time_count" ) or 0
92+ local avg = count > 0 and ( sum / count) or 0
93+
94+ table.insert( output, "# HELP nginx_stream_upstream_connect_time_seconds Average upstream connect time" )
95+ table.insert( output, "# TYPE nginx_stream_upstream_connect_time_seconds gauge" )
96+ table.insert( output, "nginx_stream_upstream_connect_time_seconds " .. string.format( "%.6f" , avg))
97+ table.insert( output, "" )
98+
99+ table.insert( output, "# HELP nginx_stream_upstream_connect_time_sum_seconds Total upstream connect time" )
100+ table.insert( output, "# TYPE nginx_stream_upstream_connect_time_sum_seconds counter" )
101+ table.insert( output, "nginx_stream_upstream_connect_time_sum_seconds " .. string.format( "%.6f" , sum))
102+ table.insert( output, "" )
103+
104+ table.insert( output, "# HELP nginx_stream_upstream_connect_time_count_total Total upstream connections" )
105+ table.insert( output, "# TYPE nginx_stream_upstream_connect_time_count_total counter" )
106+ table.insert( output, "nginx_stream_upstream_connect_time_count_total " .. count)
107+ end
108+
109+ ngx.say( table.concat( output, "\n " ))
110+
111+ -- Incrémenter le compteur de requêtes metrics
112+ local metrics_count = metrics:get( "metrics_requests_total" ) or 0
113+ metrics:set ( "metrics_requests_total" , metrics_count + 1)
114+ }
32115 }
33116 }
34117}
@@ -37,13 +120,21 @@ stream {
37120 map_hash_bucket_size 128 ;
38121 map_hash_max_size 4096 ;
39122
123+ lua_shared_dict stream_metrics 10M ;
124+
125+ init_by_lua_block {
126+ -- Initialiser les métriques stream
127+ ngx.shared.stream_metrics:set ( "stream_connections_total" , 0)
128+ ngx.shared.stream_metrics:set ( "upstream_connect_time_sum" , 0)
129+ ngx.shared.stream_metrics:set ( "upstream_connect_time_count" , 0)
130+ }
131+
40132 log_format main '$proxy_protocol_addr - $remote_addr [$time_local] '
41133 '$protocol $status $bytes_sent $bytes_received '
42134 '$session_time "$upstream_addr" '
43135 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"' ;
44136
45137 access_log /dev/stdout main;
46-
47138 resolver kube-dns.kube-system.svc.cluster.local valid=30s ;
48139 resolver_timeout 5s ;
49140
@@ -52,5 +143,21 @@ stream {
52143 ssl_preread on;
53144 proxy_pass $ssl_preread_server_name :443 ;
54145 proxy_protocol off;
146+
147+ log_by_lua_block {
148+ local metrics = ngx.shared.stream_metrics
149+ local connect_time = tonumber( ngx.var.upstream_connect_time) or 0
150+ local upstream = ngx.var.upstream_addr or "unknown"
151+
152+ local connections = metrics:get( "stream_connections_total" ) or 0
153+ metrics:set ( "stream_connections_total" , connections + 1)
154+
155+ if connect_time > 0 then
156+ local sum = metrics:get( "upstream_connect_time_sum" ) or 0
157+ local count = metrics:get( "upstream_connect_time_count" ) or 0
158+ metrics:set ( "upstream_connect_time_sum" , sum + connect_time)
159+ metrics:set ( "upstream_connect_time_count" , count + 1)
160+ end
161+ }
55162 }
56- }
163+ }
0 commit comments