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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
|
package solomon
import (
"encoding/json"
"time"
"go.uber.org/atomic"
)
var _ Metric = (*FuncGauge)(nil)
// FuncGauge tracks float64 value returned by function.
type FuncGauge struct {
name string
metricType metricType
tags map[string]string
function func() float64
timestamp *time.Time
useNameTag bool
}
func (g *FuncGauge) Name() string {
return g.name
}
func (g *FuncGauge) Function() func() float64 {
return g.function
}
func (g *FuncGauge) getType() metricType {
return g.metricType
}
func (g *FuncGauge) getLabels() map[string]string {
return g.tags
}
func (g *FuncGauge) getValue() interface{} {
return g.function()
}
func (g *FuncGauge) getTimestamp() *time.Time {
return g.timestamp
}
func (g *FuncGauge) getNameTag() string {
if g.useNameTag {
return "name"
} else {
return "sensor"
}
}
// MarshalJSON implements json.Marshaler.
func (g *FuncGauge) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Type string `json:"type"`
Labels map[string]string `json:"labels"`
Value float64 `json:"value"`
Timestamp *int64 `json:"ts,omitempty"`
}{
Type: g.metricType.String(),
Value: g.function(),
Labels: func() map[string]string {
labels := make(map[string]string, len(g.tags)+1)
labels[g.getNameTag()] = g.Name()
for k, v := range g.tags {
labels[k] = v
}
return labels
}(),
Timestamp: tsAsRef(g.timestamp),
})
}
// Snapshot returns independent copy on metric.
func (g *FuncGauge) Snapshot() Metric {
return &Gauge{
name: g.name,
metricType: g.metricType,
tags: g.tags,
value: *atomic.NewFloat64(g.function()),
useNameTag: g.useNameTag,
}
}
|