Skip to content

Commit 52e6ef2

Browse files
committed
prometheus middleware: happy path
1 parent 42f7e2a commit 52e6ef2

File tree

1 file changed

+18
-28
lines changed

1 file changed

+18
-28
lines changed

pkg/apiserver/controllers/v1/metrics.go

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v1
22

33
import (
4+
"cmp"
45
"time"
56

67
"github.com/crowdsecurity/crowdsec/pkg/metrics"
@@ -29,52 +30,41 @@ func PrometheusBouncersHasNonEmptyDecision(c *gin.Context) {
2930
func PrometheusMachinesMiddleware() gin.HandlerFunc {
3031
return func(c *gin.Context) {
3132
machineID, _ := getMachineIDFromContext(c)
32-
if machineID != "" {
33-
fullPath := c.FullPath()
34-
if fullPath == "" {
35-
fullPath = "invalid-endpoint"
36-
}
37-
metrics.LapiMachineHits.With(prometheus.Labels{
38-
"machine": machineID,
39-
"route": fullPath,
40-
"method": c.Request.Method,
41-
}).Inc()
33+
if machineID == "" {
34+
c.Next()
35+
return
4236
}
4337

44-
c.Next()
38+
metrics.LapiMachineHits.With(prometheus.Labels{
39+
"machine": machineID,
40+
"route": cmp.Or(c.FullPath(), "invalid-endpoint"),
41+
"method": c.Request.Method,
42+
}).Inc()
4543
}
4644
}
4745

4846
func PrometheusBouncersMiddleware() gin.HandlerFunc {
4947
return func(c *gin.Context) {
5048
bouncer, _ := getBouncerFromContext(c)
51-
if bouncer != nil {
52-
fullPath := c.FullPath()
53-
if fullPath == "" {
54-
fullPath = "invalid-endpoint"
55-
}
56-
metrics.LapiBouncerHits.With(prometheus.Labels{
57-
"bouncer": bouncer.Name,
58-
"route": fullPath,
59-
"method": c.Request.Method,
60-
}).Inc()
49+
if bouncer == nil {
50+
c.Next()
51+
return
6152
}
6253

63-
c.Next()
54+
metrics.LapiBouncerHits.With(prometheus.Labels{
55+
"bouncer": bouncer.Name,
56+
"route": cmp.Or(c.FullPath(), "invalid-endpoint"),
57+
"method": c.Request.Method,
58+
}).Inc()
6459
}
6560
}
6661

6762
func PrometheusMiddleware() gin.HandlerFunc {
6863
return func(c *gin.Context) {
6964
startTime := time.Now()
7065

71-
fullPath := c.FullPath()
72-
if fullPath == "" {
73-
fullPath = "invalid-endpoint"
74-
}
75-
7666
metrics.LapiRouteHits.With(prometheus.Labels{
77-
"route": fullPath,
67+
"route": cmp.Or(c.FullPath(), "invalid-endpoint"),
7868
"method": c.Request.Method,
7969
}).Inc()
8070
c.Next()

0 commit comments

Comments
 (0)