Skip to content

Commit 9b3f7b6

Browse files
committed
extend metrics with labels
1 parent 1985984 commit 9b3f7b6

File tree

1 file changed

+41
-29
lines changed

1 file changed

+41
-29
lines changed

main.go

+41-29
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,10 @@ type ProxyServer struct {
5252
workerCount int
5353

5454
// 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
5959
totalDropped prometheus.Counter
6060
totalFailedBodyRead prometheus.Counter
6161
usedQueueLength prometheus.Gauge
@@ -74,32 +74,44 @@ func NewProxyServer(configPath string, queueSize, workerCount int) *ProxyServer
7474
queue: make(chan *ForwardedRequest, queueSize), // Capped channel
7575
workerCount: workerCount,
7676
// 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+
),
93105
totalDropped: prometheus.NewCounter(prometheus.CounterOpts{
94-
Name: "proxy_dropped_total",
106+
Name: "pxfd_http_proxy_dropped_total",
95107
Help: "Total number of dropped requests",
96108
}),
97109
totalFailedBodyRead: prometheus.NewCounter(prometheus.CounterOpts{
98-
Name: "proxy_failed_body_read_total",
110+
Name: "pxfd_http_proxy_failed_body_read_total",
99111
Help: "Total number of requests with failed body reads",
100112
}),
101113
usedQueueLength: prometheus.NewGauge(prometheus.GaugeOpts{
102-
Name: "proxy_queue_length",
114+
Name: "pxfd_http_proxy_queue_length",
103115
Help: "Current length of the request queue",
104116
}),
105117
}
@@ -252,15 +264,15 @@ func (p *ProxyServer) getClientForHost(hostBackend string, timeout float32) (*ht
252264

253265
// proxyRequest forwards the request to the backend with retries based on the configuration
254266
func (p *ProxyServer) proxyRequest(r *ForwardedRequest) {
255-
// Increment total requests counter
256-
p.totalRequests.Inc()
257267

258268
host := r.Req.Host // The front-facing host
259269
host , _, _ = strings.Cut(host, ":") //strip port
260270
backends, found := p.getBackendsForHost(host)
261271

272+
p.totalRequests.WithLabelValues(host).Inc()
273+
262274
if !found || len(backends) == 0 {
263-
p.totalFailed.Inc() // Increment failed request counter
275+
p.totalFailed.WithLabelValues(host).Inc() // Increment failed request counter
264276
log.Printf("Error host: '%v' not found in config file, droping request\n", host)
265277
return
266278
}
@@ -285,17 +297,17 @@ func (p *ProxyServer) proxyRequest(r *ForwardedRequest) {
285297
resp.Body.Close()
286298
if err == nil && resp.StatusCode < 400 {
287299
// Successfully forwarded request
288-
p.totalForwarded.Inc()
300+
p.totalForwarded.WithLabelValues(host, backend.Backend).Inc()
289301
return
290302
}
291303
lastErr = err
292-
p.totalRetries.Inc() // Increment retries counter
304+
p.totalRetries.WithLabelValues(host, backend.Backend).Inc()
293305
time.Sleep(time.Duration(backend.Delay) * time.Second) // Small delay before retrying
294306
}
295307
}
296308

297309
// If we get here, all backends failed
298-
p.totalFailed.Inc() // Increment failed requests counter
310+
p.totalFailed.WithLabelValues(host).Inc()
299311
log.Printf("All backends failed for host %s: %v\n", host, lastErr)
300312
}
301313

0 commit comments

Comments
 (0)