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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
|
package solomon
import (
"bytes"
"context"
"encoding"
"encoding/json"
"fmt"
"time"
"github.com/ydb-platform/ydb/library/go/core/xerrors"
"golang.org/x/exp/slices"
)
// Gather collects all metrics data via snapshots.
func (r Registry) Gather() (*Metrics, error) {
metrics := make([]Metric, 0)
var err error
r.metrics.Range(func(_, v interface{}) bool {
if s, ok := v.(Metric); ok {
metrics = append(metrics, s.Snapshot())
return true
}
err = fmt.Errorf("unexpected value type: %T", v)
return false
})
if err != nil {
return nil, err
}
return &Metrics{metrics: metrics}, nil
}
func NewMetrics(metrics []Metric) Metrics {
return Metrics{metrics: metrics}
}
func NewMetricsWithTimestamp(metrics []Metric, ts time.Time) Metrics {
return Metrics{metrics: metrics, timestamp: &ts}
}
type valueType uint8
const (
valueTypeNone valueType = iota
valueTypeOneWithoutTS valueType = 0x01
valueTypeOneWithTS valueType = 0x02
valueTypeManyWithTS valueType = 0x03
)
type metricType uint8
const (
typeUnspecified metricType = iota
typeGauge metricType = 0x01
typeCounter metricType = 0x02
typeRated metricType = 0x03
typeIGauge metricType = 0x04
typeHistogram metricType = 0x05
typeRatedHistogram metricType = 0x06
)
func (k metricType) String() string {
switch k {
case typeCounter:
return "COUNTER"
case typeGauge:
return "DGAUGE"
case typeIGauge:
return "IGAUGE"
case typeHistogram:
return "HIST"
case typeRated:
return "RATE"
case typeRatedHistogram:
return "HIST_RATE"
default:
panic("unknown metric type")
}
}
// Metric is an any abstract solomon Metric.
type Metric interface {
json.Marshaler
Name() string
getType() metricType
getLabels() map[string]string
getValue() interface{}
getNameTag() string
getTimestamp() *time.Time
Snapshot() Metric
}
// Rated marks given Solomon metric or vector as rated.
// Example:
//
// cnt := r.Counter("mycounter")
// Rated(cnt)
//
// cntvec := r.CounterVec("mycounter", []string{"mytag"})
// Rated(cntvec)
//
// For additional info: https://docs.yandex-team.ru/solomon/data-collection/dataformat/json
func Rated(s interface{}) {
switch st := s.(type) {
case *Counter:
st.metricType = typeRated
case *FuncCounter:
st.metricType = typeRated
case *Histogram:
st.metricType = typeRatedHistogram
case *CounterVec:
st.vec.rated = true
case *HistogramVec:
st.vec.rated = true
case *DurationHistogramVec:
st.vec.rated = true
}
// any other metrics types are unrateable
}
var (
_ json.Marshaler = (*Metrics)(nil)
_ encoding.BinaryMarshaler = (*Metrics)(nil)
)
type Metrics struct {
metrics []Metric
timestamp *time.Time
}
// MarshalJSON implements json.Marshaler.
func (s Metrics) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
Metrics []Metric `json:"metrics"`
Timestamp *int64 `json:"ts,omitempty"`
}{s.metrics, tsAsRef(s.timestamp)})
}
// MarshalBinary implements encoding.BinaryMarshaler.
func (s Metrics) MarshalBinary() ([]byte, error) {
var buf bytes.Buffer
se := NewSpackEncoder(context.Background(), CompressionNone, &s)
n, err := se.Encode(&buf)
if err != nil {
return nil, xerrors.Errorf("encode only %d bytes: %w", n, err)
}
return buf.Bytes(), nil
}
// SplitToChunks splits Metrics into a slice of chunks, each at most maxChunkSize long.
// The length of returned slice is always at least one.
// Zero maxChunkSize denotes unlimited chunk length.
func (s Metrics) SplitToChunks(maxChunkSize int) []Metrics {
if maxChunkSize == 0 || len(s.metrics) == 0 {
return []Metrics{s}
}
chunks := make([]Metrics, 0, len(s.metrics)/maxChunkSize+1)
for leftBound := 0; leftBound < len(s.metrics); leftBound += maxChunkSize {
rightBound := leftBound + maxChunkSize
if rightBound > len(s.metrics) {
rightBound = len(s.metrics)
}
chunk := s.metrics[leftBound:rightBound]
chunks = append(chunks, Metrics{metrics: chunk})
}
return chunks
}
// List return list of metrics
func (s Metrics) List() []Metric {
return slices.Clone(s.metrics)
}
func tsAsRef(t *time.Time) *int64 {
if t == nil {
return nil
}
ts := t.Unix()
return &ts
}
|