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,23 @@ 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+ # Shared dictionary pour stocker les métriques
20+ lua_shared_dict metrics 10M ;
21+
22+ # Initialisation des métriques
23+ init_by_lua_block {
24+ -- Initialiser les compteurs HTTP
25+ ngx.shared.metrics:set ( "http_requests_total" , 0)
26+ ngx.shared.metrics:set ( "healthz_requests_total" , 0)
27+ ngx.shared.metrics:set ( "metrics_requests_total" , 0)
28+ }
29+
1630 log_format main '$remote_addr - $remote_user [$time_local] '
1731 '"$request" $status $body_bytes_sent '
1832 '"$http_referer" "$http_user_agent"' ;
@@ -24,11 +38,88 @@ http {
2438
2539 location / {
2640 return 404 ;
41+
42+ log_by_lua_block {
43+ local metrics = ngx.shared.metrics
44+ local total = metrics:get( "http_requests_total" ) or 0
45+ metrics:set ( "http_requests_total" , total + 1)
46+ }
2747 }
2848
2949 location /healthz {
3050 default_type text/plain;
3151 return 200 "OK\n " ;
52+
53+ log_by_lua_block {
54+ local metrics = ngx.shared.metrics
55+
56+ -- Incrémenter compteur healthz
57+ local healthz = metrics:get( "healthz_requests_total" ) or 0
58+ metrics:set ( "healthz_requests_total" , healthz + 1)
59+
60+ -- Incrémenter compteur total
61+ local total = metrics:get( "http_requests_total" ) or 0
62+ metrics:set ( "http_requests_total" , total + 1)
63+ }
64+ }
65+
66+ location /metrics {
67+ default_type text/plain;
68+ content_by_lua_block {
69+ local metrics = ngx.shared.metrics
70+ local stream_metrics = ngx.shared.stream_metrics
71+ local output = {}
72+
73+ -- En-têtes Prometheus
74+ table.insert( output, "# HELP nginx_up Nginx is running" )
75+ table.insert( output, "# TYPE nginx_up gauge" )
76+ table.insert( output, "nginx_up 1" )
77+ table.insert( output, "" )
78+
79+ -- Métriques HTTP
80+ table.insert( output, "# HELP nginx_http_requests_total Total HTTP requests" )
81+ table.insert( output, "# TYPE nginx_http_requests_total counter" )
82+ table.insert( output, "nginx_http_requests_total " .. ( metrics:get( "http_requests_total" ) or 0))
83+ table.insert( output, "" )
84+
85+ table.insert( output, "# HELP nginx_healthz_requests_total Total healthz requests" )
86+ table.insert( output, "# TYPE nginx_healthz_requests_total counter" )
87+ table.insert( output, "nginx_healthz_requests_total " .. ( metrics:get( "healthz_requests_total" ) or 0))
88+ table.insert( output, "" )
89+
90+ -- Métriques Stream
91+ if stream_metrics then
92+ table.insert( output, "# HELP nginx_stream_connections_total Total stream connections" )
93+ table.insert( output, "# TYPE nginx_stream_connections_total counter" )
94+ table.insert( output, "nginx_stream_connections_total " .. ( stream_metrics:get( "stream_connections_total" ) or 0))
95+ table.insert( output, "" )
96+
97+ -- Temps de connexion upstream moyen
98+ local sum = stream_metrics:get( "upstream_connect_time_sum" ) or 0
99+ local count = stream_metrics:get( "upstream_connect_time_count" ) or 0
100+ local avg = count > 0 and ( sum / count) or 0
101+
102+ table.insert( output, "# HELP nginx_stream_upstream_connect_time_seconds Average upstream connect time" )
103+ table.insert( output, "# TYPE nginx_stream_upstream_connect_time_seconds gauge" )
104+ table.insert( output, "nginx_stream_upstream_connect_time_seconds " .. string.format( "%.6f" , avg))
105+ table.insert( output, "" )
106+
107+ table.insert( output, "# HELP nginx_stream_upstream_connect_time_sum_seconds Total upstream connect time" )
108+ table.insert( output, "# TYPE nginx_stream_upstream_connect_time_sum_seconds counter" )
109+ table.insert( output, "nginx_stream_upstream_connect_time_sum_seconds " .. string.format( "%.6f" , sum))
110+ table.insert( output, "" )
111+
112+ table.insert( output, "# HELP nginx_stream_upstream_connect_time_count_total Total upstream connections" )
113+ table.insert( output, "# TYPE nginx_stream_upstream_connect_time_count_total counter" )
114+ table.insert( output, "nginx_stream_upstream_connect_time_count_total " .. count)
115+ end
116+
117+ ngx.say( table.concat( output, "\n " ))
118+
119+ -- Incrémenter le compteur de requêtes metrics
120+ local metrics_count = metrics:get( "metrics_requests_total" ) or 0
121+ metrics:set ( "metrics_requests_total" , metrics_count + 1)
122+ }
32123 }
33124 }
34125}
@@ -37,13 +128,22 @@ stream {
37128 map_hash_bucket_size 128 ;
38129 map_hash_max_size 4096 ;
39130
131+ # Shared dictionary pour les métriques stream
132+ lua_shared_dict stream_metrics 10M ;
133+
134+ init_by_lua_block {
135+ -- Initialiser les métriques stream
136+ ngx.shared.stream_metrics:set ( "stream_connections_total" , 0)
137+ ngx.shared.stream_metrics:set ( "upstream_connect_time_sum" , 0)
138+ ngx.shared.stream_metrics:set ( "upstream_connect_time_count" , 0)
139+ }
140+
40141 log_format main '$proxy_protocol_addr - $remote_addr [$time_local] '
41142 '$protocol $status $bytes_sent $bytes_received '
42143 '$session_time "$upstream_addr" '
43144 '"$upstream_bytes_sent" "$upstream_bytes_received" "$upstream_connect_time"' ;
44145
45146 access_log /dev/stdout main;
46-
47147 resolver kube-dns.kube-system.svc.cluster.local valid=30s ;
48148 resolver_timeout 5s ;
49149
@@ -52,5 +152,23 @@ stream {
52152 ssl_preread on;
53153 proxy_pass $ssl_preread_server_name :443 ;
54154 proxy_protocol off;
155+
156+ log_by_lua_block {
157+ local metrics = ngx.shared.stream_metrics
158+ local connect_time = tonumber( ngx.var.upstream_connect_time) or 0
159+ local upstream = ngx.var.upstream_addr or "unknown"
160+
161+ -- Incrémenter le nombre de connexions
162+ local connections = metrics:get( "stream_connections_total" ) or 0
163+ metrics:set ( "stream_connections_total" , connections + 1)
164+
165+ -- Enregistrer les temps de connexion upstream
166+ if connect_time > 0 then
167+ local sum = metrics:get( "upstream_connect_time_sum" ) or 0
168+ local count = metrics:get( "upstream_connect_time_count" ) or 0
169+ metrics:set ( "upstream_connect_time_sum" , sum + connect_time)
170+ metrics:set ( "upstream_connect_time_count" , count + 1)
171+ end
172+ }
55173 }
56- }
174+ }
0 commit comments