aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/core/metrics/solomon/int_gauge.go
blob: 8733bf11fe6056d6966db6059675cc82f7e132b7 (plain) (blame)
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.IntGauge = (*IntGauge)(nil)
	_ Metric           = (*IntGauge)(nil)
)

// IntGauge tracks single float64 value.
type IntGauge struct {
	name       string
	metricType metricType
	tags       map[string]string
	value      atomic.Int64
	timestamp  *time.Time

	useNameTag bool
}

func NewIntGauge(name string, value int64, opts ...metricOpts) IntGauge {
	mOpts := MetricsOpts{}
	for _, op := range opts {
		op(&mOpts)
	}
	return IntGauge{
		name:       name,
		metricType: typeIGauge,
		tags:       mOpts.tags,
		value:      *atomic.NewInt64(value),
		useNameTag: mOpts.useNameTag,
		timestamp:  mOpts.timestamp,
	}
}

func (g *IntGauge) Set(value int64) {
	g.value.Store(value)
}

func (g *IntGauge) Add(value int64) {
	g.value.Add(value)
}

func (g *IntGauge) Name() string {
	return g.name
}

func (g *IntGauge) getType() metricType {
	return g.metricType
}

func (g *IntGauge) getLabels() map[string]string {
	return g.tags
}

func (g *IntGauge) getValue() interface{} {
	return g.value.Load()
}

func (g *IntGauge) getTimestamp() *time.Time {
	return g.timestamp
}

func (g *IntGauge) getNameTag() string {
	if g.useNameTag {
		return "name"
	} else {
		return "sensor"
	}
}

// MarshalJSON implements json.Marshaler.
func (g *IntGauge) 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     int64             `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 *IntGauge) Snapshot() Metric {
	return &IntGauge{
		name:       g.name,
		metricType: g.metricType,
		tags:       g.tags,
		value:      *atomic.NewInt64(g.value.Load()),

		useNameTag: g.useNameTag,
		timestamp:  g.timestamp,
	}
}