/
metric.go
56 lines (46 loc) · 1.18 KB
/
metric.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
52
53
54
55
56
package structures
// Metric like AxmMonitor
type Metric struct {
Name string `json:"name"`
Value float64 `json:"value"`
Category string `json:"type"`
Historic bool `json:"historic"`
Unit string `json:"unit"`
Aggregation string `json:"agg_type"`
Function func() float64 `json:"-"`
}
// Get current value and check func if provided
func (metric *Metric) Get() float64 {
if metric.Function != nil {
metric.Value = metric.Function()
}
return metric.Value
}
// Set current value
func (metric *Metric) Set(value float64) {
metric.Value = value
}
var defaultAggregation = "avg"
// CreateMetric with default values
func CreateMetric(name string, category string, unit string) Metric {
return Metric{
Name: name,
Category: category,
Unit: unit,
Value: 0,
Aggregation: defaultAggregation,
Historic: true,
}
}
// CreateFuncMetric with default values and exposed function
func CreateFuncMetric(name string, category string, unit string, cb func() float64) Metric {
return Metric{
Name: name,
Category: category,
Unit: unit,
Value: 0,
Aggregation: defaultAggregation,
Historic: true,
Function: cb,
}
}