-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
metrics.go
116 lines (100 loc) · 2.61 KB
/
metrics.go
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
package gather
import (
"bytes"
"io"
"time"
"github.com/gogo/protobuf/proto"
"github.com/influxdata/influxdb/v2"
"github.com/influxdata/influxdb/v2/models"
)
// MetricsCollection is the struct including metrics and other requirements.
type MetricsCollection struct {
OrgID influxdb.ID `json:"orgID"`
BucketID influxdb.ID `json:"bucketID"`
MetricsSlice MetricsSlice `json:"metrics"`
}
// Metrics is the default influx based metrics.
type Metrics struct {
Name string `json:"name"`
Tags map[string]string `json:"tags"`
Fields map[string]interface{} `json:"fields"`
Timestamp time.Time `json:"timestamp"`
Type MetricType `json:"type"`
}
// MetricsSlice is a slice of Metrics
type MetricsSlice []Metrics
// Points convert the MetricsSlice to model.Points
func (ms MetricsSlice) Points() (models.Points, error) {
ps := make([]models.Point, len(ms))
for mi, m := range ms {
point, err := models.NewPoint(m.Name, models.NewTags(m.Tags), m.Fields, m.Timestamp)
if err != nil {
return ps, err
}
ps[mi] = point
}
return ps, nil
}
// Reader returns an io.Reader that enumerates the metrics.
// All metrics are allocated into the underlying buffer.
func (ms MetricsSlice) Reader() (io.Reader, error) {
buf := new(bytes.Buffer)
for mi, m := range ms {
point, err := models.NewPoint(m.Name, models.NewTags(m.Tags), m.Fields, m.Timestamp)
if err != nil {
return nil, err
}
_, err = buf.WriteString(point.String())
if err != nil {
return nil, err
}
if mi < len(ms)-1 && len(ms) > 1 {
_, err = buf.WriteString("\n")
if err != nil {
return nil, err
}
}
}
return buf, nil
}
// MetricType is prometheus metrics type.
type MetricType int
// the set of metric types
const (
MetricTypeCounter MetricType = iota
MetricTypeGauge
MetricTypeSummary
MetricTypeUntyped
MetricTypeHistogrm
)
var metricTypeName = []string{
"COUNTER",
"GAUGE",
"SUMMARY",
"UNTYPED",
"HISTOGRAM",
}
var metricTypeValue = map[string]int32{
"COUNTER": 0,
"GAUGE": 1,
"SUMMARY": 2,
"UNTYPED": 3,
"HISTOGRAM": 4,
}
// Valid returns whether the metrics type is valid.
func (x MetricType) Valid() bool {
return x >= MetricTypeCounter && x <= MetricTypeHistogrm
}
// String returns the string value of MetricType.
func (x MetricType) String() string {
return metricTypeName[x]
}
// UnmarshalJSON implements the unmarshaler interface.
func (x *MetricType) UnmarshalJSON(data []byte) error {
value, err := proto.UnmarshalJSONEnum(metricTypeValue, data, "MetricType")
if err != nil {
return err
}
*x = MetricType(value)
return nil
}