@@ -52,10 +52,10 @@ type ProxyServer struct {
52
52
workerCount int
53
53
54
54
// Prometheus metrics
55
- totalRequests prometheus.Counter
56
- totalForwarded prometheus.Counter
57
- totalRetries prometheus.Counter
58
- totalFailed prometheus.Counter
55
+ totalRequests * prometheus.CounterVec
56
+ totalForwarded * prometheus.CounterVec
57
+ totalRetries * prometheus.CounterVec
58
+ totalFailed * prometheus.CounterVec
59
59
totalDropped prometheus.Counter
60
60
totalFailedBodyRead prometheus.Counter
61
61
usedQueueLength prometheus.Gauge
@@ -74,32 +74,44 @@ func NewProxyServer(configPath string, queueSize, workerCount int) *ProxyServer
74
74
queue : make (chan * ForwardedRequest , queueSize ), // Capped channel
75
75
workerCount : workerCount ,
76
76
// Initialize Prometheus metrics
77
- totalRequests : prometheus .NewCounter (prometheus.CounterOpts {
78
- Name : "proxy_requests_total" ,
79
- Help : "Total number of incoming requests" ,
80
- }),
81
- totalForwarded : prometheus .NewCounter (prometheus.CounterOpts {
82
- Name : "proxy_forwarded_total" ,
83
- Help : "Total number of successfully forwarded requests" ,
84
- }),
85
- totalRetries : prometheus .NewCounter (prometheus.CounterOpts {
86
- Name : "proxy_retries_total" ,
87
- Help : "Total number of retries" ,
88
- }),
89
- totalFailed : prometheus .NewCounter (prometheus.CounterOpts {
90
- Name : "proxy_failed_total" ,
91
- Help : "Total number of failed requests" ,
92
- }),
77
+ totalRequests : prometheus .NewCounterVec (
78
+ prometheus.CounterOpts {
79
+ Name : "pxfd_http_proxy_requests_total" ,
80
+ Help : "Total number of incoming requests" ,
81
+ },
82
+ []string {"host" },
83
+ ),
84
+ totalForwarded : prometheus .NewCounterVec (
85
+ prometheus.CounterOpts {
86
+ Name : "pxfd_http_proxy_forwarded_total" ,
87
+ Help : "Total number of successfully forwarded requests" ,
88
+ },
89
+ []string {"host" , "backend" },
90
+ ),
91
+ totalRetries : prometheus .NewCounterVec (
92
+ prometheus.CounterOpts {
93
+ Name : "pxfd_http_proxy_retries_total" ,
94
+ Help : "Total number of retries" ,
95
+ },
96
+ []string {"host" , "backend" },
97
+ ),
98
+ totalFailed : prometheus .NewCounterVec (
99
+ prometheus.CounterOpts {
100
+ Name : "pxfd_http_proxy_failed_total" ,
101
+ Help : "Total number of failed requests" ,
102
+ },
103
+ []string {"host" },
104
+ ),
93
105
totalDropped : prometheus .NewCounter (prometheus.CounterOpts {
94
- Name : "proxy_dropped_total " ,
106
+ Name : "pxfd_http_proxy_dropped_total " ,
95
107
Help : "Total number of dropped requests" ,
96
108
}),
97
109
totalFailedBodyRead : prometheus .NewCounter (prometheus.CounterOpts {
98
- Name : "proxy_failed_body_read_total " ,
110
+ Name : "pxfd_http_proxy_failed_body_read_total " ,
99
111
Help : "Total number of requests with failed body reads" ,
100
112
}),
101
113
usedQueueLength : prometheus .NewGauge (prometheus.GaugeOpts {
102
- Name : "proxy_queue_length " ,
114
+ Name : "pxfd_http_proxy_queue_length " ,
103
115
Help : "Current length of the request queue" ,
104
116
}),
105
117
}
@@ -252,15 +264,15 @@ func (p *ProxyServer) getClientForHost(hostBackend string, timeout float32) (*ht
252
264
253
265
// proxyRequest forwards the request to the backend with retries based on the configuration
254
266
func (p * ProxyServer ) proxyRequest (r * ForwardedRequest ) {
255
- // Increment total requests counter
256
- p .totalRequests .Inc ()
257
267
258
268
host := r .Req .Host // The front-facing host
259
269
host , _ , _ = strings .Cut (host , ":" ) //strip port
260
270
backends , found := p .getBackendsForHost (host )
261
271
272
+ p .totalRequests .WithLabelValues (host ).Inc ()
273
+
262
274
if ! found || len (backends ) == 0 {
263
- p .totalFailed .Inc () // Increment failed request counter
275
+ p .totalFailed .WithLabelValues ( host ). Inc () // Increment failed request counter
264
276
log .Printf ("Error host: '%v' not found in config file, droping request\n " , host )
265
277
return
266
278
}
@@ -285,17 +297,17 @@ func (p *ProxyServer) proxyRequest(r *ForwardedRequest) {
285
297
resp .Body .Close ()
286
298
if err == nil && resp .StatusCode < 400 {
287
299
// Successfully forwarded request
288
- p .totalForwarded .Inc ()
300
+ p .totalForwarded .WithLabelValues ( host , backend . Backend ). Inc ()
289
301
return
290
302
}
291
303
lastErr = err
292
- p .totalRetries .Inc () // Increment retries counter
304
+ p .totalRetries .WithLabelValues ( host , backend . Backend ). Inc ()
293
305
time .Sleep (time .Duration (backend .Delay ) * time .Second ) // Small delay before retrying
294
306
}
295
307
}
296
308
297
309
// If we get here, all backends failed
298
- p .totalFailed .Inc () // Increment failed requests counter
310
+ p .totalFailed .WithLabelValues ( host ). Inc ()
299
311
log .Printf ("All backends failed for host %s: %v\n " , host , lastErr )
300
312
}
301
313
0 commit comments