forked from AplaProject/go-apla
/
collector.go
51 lines (44 loc) · 1.05 KB
/
collector.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
47
48
49
50
51
package metric
// CollectorFunc represents function for collects values of metrics
type CollectorFunc func() ([]*Value, error)
// Value represents value of metrics
type Value struct {
Time int64
Metric string
Key string
Value int64
}
// ToMap returns values as map
func (v *Value) ToMap() map[string]interface{} {
return map[string]interface{}{
"time": v.Time,
"metric": v.Metric,
"key": v.Key,
"value": v.Value,
}
}
// Collector represents struct that works with the collection of metrics
type Collector struct {
funcs []CollectorFunc
}
// Values returns values of all metrics
func (c *Collector) Values() []interface{} {
values := make([]interface{}, 0)
for _, fn := range c.funcs {
result, err := fn()
if err != nil {
continue
}
for _, v := range result {
values = append(values, v.ToMap())
}
}
return values
}
// NewCollector creates new collector
func NewCollector(funcs ...CollectorFunc) *Collector {
c := &Collector{}
c.funcs = make([]CollectorFunc, 0, len(funcs))
c.funcs = append(c.funcs, funcs...)
return c
}