aboutsummaryrefslogtreecommitdiffstats
path: root/library/go/core/metrics/solomon/func_counter.go
blob: db862869e485ee64424d75278b5dc3c1237fae0e (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
package solomon

import (
	"encoding/json"
	"time"

	"go.uber.org/atomic"
)

var _ Metric = (*FuncCounter)(nil)

// FuncCounter tracks int64 value returned by function.
type FuncCounter struct {
	name       string
	metricType metricType
	tags       map[string]string
	function   func() int64
	timestamp  *time.Time
	useNameTag bool
}

func (c *FuncCounter) Name() string {
	return c.name
}

func (c *FuncCounter) Function() func() int64 {
	return c.function
}

func (c *FuncCounter) getType() metricType {
	return c.metricType
}

func (c *FuncCounter) getLabels() map[string]string {
	return c.tags
}

func (c *FuncCounter) getValue() interface{} {
	return c.function()
}

func (c *FuncCounter) getTimestamp() *time.Time {
	return c.timestamp
}

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

// MarshalJSON implements json.Marshaler.
func (c *FuncCounter) MarshalJSON() ([]byte, error) {
	return json.Marshal(struct {
		Type      string            `json:"type"`
		Labels    map[string]string `json:"labels"`
		Value     int64             `json:"value"`
		Timestamp *int64            `json:"ts,omitempty"`
	}{
		Type:  c.metricType.String(),
		Value: c.function(),
		Labels: func() map[string]string {
			labels := make(map[string]string, len(c.tags)+1)
			labels[c.getNameTag()] = c.Name()
			for k, v := range c.tags {
				labels[k] = v
			}
			return labels
		}(),
		Timestamp: tsAsRef(c.timestamp),
	})
}

// Snapshot returns independent copy on metric.
func (c *FuncCounter) Snapshot() Metric {
	return &Counter{
		name:       c.name,
		metricType: c.metricType,
		tags:       c.tags,
		value:      *atomic.NewInt64(c.function()),

		useNameTag: c.useNameTag,
	}
}