influxdb/gather/metrics.go

70 lines
1.7 KiB
Go
Raw Normal View History

2018-08-21 20:16:15 +00:00
package gather
2018-09-07 15:45:28 +00:00
import (
2019-01-10 17:39:37 +00:00
"bytes"
"io"
"time"
"github.com/influxdata/influxdb/v2/kit/platform"
"github.com/influxdata/influxdb/v2/models"
dto "github.com/prometheus/client_model/go"
2018-09-07 15:45:28 +00:00
)
2018-08-21 20:16:15 +00:00
2019-01-10 17:39:37 +00:00
// MetricsCollection is the struct including metrics and other requirements.
type MetricsCollection struct {
OrgID platform.ID `json:"orgID"`
BucketID platform.ID `json:"bucketID"`
2019-01-10 17:39:37 +00:00
MetricsSlice MetricsSlice `json:"metrics"`
}
2018-09-07 15:45:28 +00:00
// Metrics is the default influx based metrics.
2018-08-21 20:16:15 +00:00
type Metrics struct {
Name string `json:"name"`
Tags map[string]string `json:"tags"`
Fields map[string]interface{} `json:"fields"`
2019-01-10 17:39:37 +00:00
Timestamp time.Time `json:"timestamp"`
Type dto.MetricType `json:"type"`
2018-09-07 15:45:28 +00:00
}
2019-01-10 17:39:37 +00:00
// 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
}
2019-01-10 17:39:37 +00:00
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
}
2019-01-10 17:39:37 +00:00
_, err = buf.WriteString(point.String())
if err != nil {
return nil, err
}
2019-01-10 17:39:37 +00:00
if mi < len(ms)-1 && len(ms) > 1 {
_, err = buf.WriteString("\n")
if err != nil {
return nil, err
}
}
}
return buf, nil
}