Skip to content

Commit 441316a

Browse files
committed
feat: global store for non-prometheus metrics with cidr and named port counters
Signed-off-by: Hunter Gregory <[email protected]>
1 parent 3367131 commit 441316a

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

npm/metrics/counts.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package metrics
2+
3+
import "sync"
4+
5+
var nonPrometheusCounts *counts
6+
7+
// counts is a struct holding non-Prometheus counts.
8+
type counts struct {
9+
sync.Mutex
10+
cidrNetPols int
11+
namedPortNetPols int
12+
}
13+
14+
func IncCidrNetPols() {
15+
if nonPrometheusCounts == nil {
16+
return
17+
}
18+
nonPrometheusCounts.Lock()
19+
defer nonPrometheusCounts.Unlock()
20+
nonPrometheusCounts.cidrNetPols++
21+
}
22+
23+
func DecCidrNetPols() {
24+
if nonPrometheusCounts == nil {
25+
return
26+
}
27+
nonPrometheusCounts.Lock()
28+
defer nonPrometheusCounts.Unlock()
29+
nonPrometheusCounts.cidrNetPols--
30+
}
31+
32+
func GetCidrNetPols() int {
33+
if nonPrometheusCounts == nil {
34+
return 0
35+
}
36+
nonPrometheusCounts.Lock()
37+
defer nonPrometheusCounts.Unlock()
38+
return nonPrometheusCounts.cidrNetPols
39+
}
40+
41+
func IncNamedPortNetPols() {
42+
if nonPrometheusCounts == nil {
43+
return
44+
}
45+
nonPrometheusCounts.Lock()
46+
defer nonPrometheusCounts.Unlock()
47+
nonPrometheusCounts.namedPortNetPols++
48+
}
49+
50+
func DecNamedPortNetPols() {
51+
if nonPrometheusCounts == nil {
52+
return
53+
}
54+
nonPrometheusCounts.Lock()
55+
defer nonPrometheusCounts.Unlock()
56+
nonPrometheusCounts.namedPortNetPols--
57+
}
58+
59+
func GetNamedPortNetPols() int {
60+
if nonPrometheusCounts == nil {
61+
return 0
62+
}
63+
nonPrometheusCounts.Lock()
64+
defer nonPrometheusCounts.Unlock()
65+
return nonPrometheusCounts.namedPortNetPols
66+
}

npm/metrics/prometheus-metrics.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ func InitializeAll() {
162162
return
163163
}
164164

165+
// initialize global variable (see counts.go)
166+
nonPrometheusCounts = &counts{}
167+
165168
initializeDaemonMetrics()
166169
initializeControllerMetrics()
167170

0 commit comments

Comments
 (0)