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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
|
package solomon
import (
"encoding/json"
"time"
"github.com/ydb-platform/ydb/library/go/core/metrics"
"go.uber.org/atomic"
)
var (
_ metrics.Gauge = (*Gauge)(nil)
_ Metric = (*Gauge)(nil)
)
// Gauge tracks single float64 value.
type Gauge struct {
name string
metricType metricType
tags map[string]string
value atomic.Float64
timestamp *time.Time
useNameTag bool
}
func NewGauge(name string, value float64, opts ...metricOpts) Gauge {
mOpts := MetricsOpts{}
for _, op := range opts {
op(&mOpts)
}
return Gauge{
name: name,
metricType: typeGauge,
tags: mOpts.tags,
value: *atomic.NewFloat64(value),
useNameTag: mOpts.useNameTag,
timestamp: mOpts.timestamp,
}
}
func (g *Gauge) Set(value float64) {
g.value.Store(value)
}
func (g *Gauge) Add(value float64) {
g.value.Add(value)
}
func (g *Gauge) Name() string {
return g.name
}
func (g *Gauge) getType() metricType {
return g.metricType
}
func (g *Gauge) getLabels() map[string]string {
return g.tags
}
func (g *Gauge) getValue() interface{} {
return g.value.Load()
}
func (g *Gauge) getTimestamp() *time.Time {
return g.timestamp
}
func (g *Gauge) getNameTag() string {
if g.useNameTag {
return "name"
} else {
return "sensor"
}
}
// MarshalJSON implements json.Marshaler.
func (g *Gauge) MarshalJSON() ([]byte, error) {
metricType := g.metricType.String()
value := g.value.Load()
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
}()
return json.Marshal(struct {
Type string `json:"type"`
Labels map[string]string `json:"labels"`
Value float64 `json:"value"`
Timestamp *int64 `json:"ts,omitempty"`
}{
Type: metricType,
Value: value,
Labels: labels,
Timestamp: tsAsRef(g.timestamp),
})
}
// Snapshot returns independent copy of metric.
func (g *Gauge) Snapshot() Metric {
return &Gauge{
name: g.name,
metricType: g.metricType,
tags: g.tags,
value: *atomic.NewFloat64(g.value.Load()),
useNameTag: g.useNameTag,
timestamp: g.timestamp,
}
}
|