forked from influxdata/influxdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
69 lines (59 loc) · 1.69 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
package gather
import (
"bytes"
"io"
"time"
"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/models"
dto "github.com/prometheus/client_model/go"
)
// MetricsCollection is the struct including metrics and other requirements.
type MetricsCollection struct {
OrgID platform.ID `json:"orgID"`
BucketID platform.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 dto.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
}