@@ -2,43 +2,50 @@ package collector
2
2
3
3
import (
4
4
"github.com/prometheus/client_golang/prometheus"
5
+
6
+ "github.com/iotaledger/hive.go/ds/shrinkingmap"
5
7
)
6
8
7
9
// Collector is responsible for creation and collection of metrics for the prometheus.
8
10
type Collector struct {
9
11
Registry * prometheus.Registry
10
- collections map [string ] * Collection
12
+ collections * shrinkingmap. ShrinkingMap [string , * Collection ]
11
13
}
12
14
13
15
// New creates an instance of Manager and creates a new prometheus registry for the protocol metrics collection.
14
16
func New () * Collector {
15
17
return & Collector {
16
18
Registry : prometheus .NewRegistry (),
17
- collections : make ( map [string ] * Collection ),
19
+ collections : shrinkingmap . New [string , * Collection ]( ),
18
20
}
19
21
}
20
22
21
- func (c * Collector ) RegisterCollection (coll * Collection ) {
22
- c .collections [ coll . CollectionName ] = coll
23
- for _ , m := range coll . metrics {
24
- c .Registry .MustRegister (m .promMetric )
25
- if m .initValueFunc != nil {
26
- metricValue , labelValues := m .initValueFunc ()
27
- m .update (metricValue , labelValues ... )
23
+ func (c * Collector ) RegisterCollection (collection * Collection ) {
24
+ c .collections . Set ( collection . CollectionName , collection )
25
+ collection . metrics . ForEach ( func ( _ string , metric * Metric ) bool {
26
+ c .Registry .MustRegister (metric .promMetric )
27
+ if metric .initValueFunc != nil {
28
+ metricValue , labelValues := metric .initValueFunc ()
29
+ metric .update (metricValue , labelValues ... )
28
30
}
29
- if m .initFunc != nil {
30
- m .initFunc ()
31
+ if metric .initFunc != nil {
32
+ metric .initFunc ()
31
33
}
32
- }
34
+
35
+ return true
36
+ })
33
37
}
34
38
35
39
// Collect collects all metrics from the registered collections.
36
40
func (c * Collector ) Collect () {
37
- for _ , collection := range c . collections {
38
- for _ , metric := range collection . metrics {
41
+ c . collections . ForEach ( func ( _ string , collection * Collection ) bool {
42
+ collection . metrics . ForEach ( func ( _ string , metric * Metric ) bool {
39
43
metric .collect ()
40
- }
41
- }
44
+ return true
45
+ })
46
+
47
+ return true
48
+ })
42
49
}
43
50
44
51
// Update updates the value of the existing metric defined by the subsystem and metricName.
@@ -78,11 +85,14 @@ func (c *Collector) ResetMetric(namespace string, metricName string) {
78
85
}
79
86
80
87
func (c * Collector ) Shutdown () {
81
- for _ , collection := range c . collections {
82
- for _ , metric := range collection . metrics {
88
+ c . collections . ForEach ( func ( _ string , collection * Collection ) bool {
89
+ collection . metrics . ForEach ( func ( _ string , metric * Metric ) bool {
83
90
metric .shutdown ()
84
- }
85
- }
91
+ return true
92
+ })
93
+
94
+ return true
95
+ })
86
96
}
87
97
88
98
func (c * Collector ) getMetric (subsystem string , metricName string ) * Metric {
@@ -95,9 +105,10 @@ func (c *Collector) getMetric(subsystem string, metricName string) *Metric {
95
105
}
96
106
97
107
func (c * Collector ) getCollection (subsystem string ) * Collection {
98
- if collection , exists := c .collections [subsystem ]; exists {
99
- return collection
108
+ collection , exists := c .collections .Get (subsystem )
109
+ if ! exists {
110
+ return nil
100
111
}
101
112
102
- return nil
113
+ return collection
103
114
}
0 commit comments