-
Notifications
You must be signed in to change notification settings - Fork 12
/
collection.go
46 lines (38 loc) · 1.01 KB
/
collection.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package collector
import (
"github.com/iotaledger/hive.go/ds/shrinkingmap"
"github.com/iotaledger/hive.go/runtime/options"
)
type Collection struct {
CollectionName string
metrics *shrinkingmap.ShrinkingMap[string, *Metric]
}
func NewCollection(name string, opts ...options.Option[Collection]) *Collection {
return options.Apply(&Collection{
CollectionName: name,
metrics: shrinkingmap.New[string, *Metric](),
}, opts, func(collection *Collection) {
collection.metrics.ForEach(func(_ string, metric *Metric) bool {
metric.Namespace = collection.CollectionName
metric.initPromMetric()
return true
})
})
}
func (c *Collection) GetMetric(metricName string) *Metric {
metric, exists := c.metrics.Get(metricName)
if !exists {
return nil
}
return metric
}
func (c *Collection) addMetric(metric *Metric) {
if metric != nil {
c.metrics.Set(metric.Name, metric)
}
}
func WithMetric(metric *Metric) options.Option[Collection] {
return func(c *Collection) {
c.addMetric(metric)
}
}