|
1 | 1 | package v1 |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "cmp" |
4 | 5 | "time" |
5 | 6 |
|
6 | 7 | "github.com/crowdsecurity/crowdsec/pkg/metrics" |
@@ -29,52 +30,41 @@ func PrometheusBouncersHasNonEmptyDecision(c *gin.Context) { |
29 | 30 | func PrometheusMachinesMiddleware() gin.HandlerFunc { |
30 | 31 | return func(c *gin.Context) { |
31 | 32 | 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 |
42 | 36 | } |
43 | 37 |
|
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() |
45 | 43 | } |
46 | 44 | } |
47 | 45 |
|
48 | 46 | func PrometheusBouncersMiddleware() gin.HandlerFunc { |
49 | 47 | return func(c *gin.Context) { |
50 | 48 | 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 |
61 | 52 | } |
62 | 53 |
|
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() |
64 | 59 | } |
65 | 60 | } |
66 | 61 |
|
67 | 62 | func PrometheusMiddleware() gin.HandlerFunc { |
68 | 63 | return func(c *gin.Context) { |
69 | 64 | startTime := time.Now() |
70 | 65 |
|
71 | | - fullPath := c.FullPath() |
72 | | - if fullPath == "" { |
73 | | - fullPath = "invalid-endpoint" |
74 | | - } |
75 | | - |
76 | 66 | metrics.LapiRouteHits.With(prometheus.Labels{ |
77 | | - "route": fullPath, |
| 67 | + "route": cmp.Or(c.FullPath(), "invalid-endpoint"), |
78 | 68 | "method": c.Request.Method, |
79 | 69 | }).Inc() |
80 | 70 | c.Next() |
|
0 commit comments