2020-12-03 03:48:44 +08:00
|
|
|
package prometheus
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bufio"
|
|
|
|
|
"bytes"
|
|
|
|
|
"fmt"
|
2021-01-08 00:21:09 +08:00
|
|
|
"io"
|
2020-12-03 03:48:44 +08:00
|
|
|
"math"
|
2021-01-08 00:21:09 +08:00
|
|
|
"mime"
|
|
|
|
|
"net/http"
|
2020-12-03 03:48:44 +08:00
|
|
|
"time"
|
|
|
|
|
|
2021-04-14 02:40:03 +08:00
|
|
|
"github.com/matttproud/golang_protobuf_extensions/pbutil"
|
|
|
|
|
|
2020-12-03 03:48:44 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
"github.com/influxdata/telegraf/metric"
|
2021-02-09 00:18:40 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/prometheus/common"
|
2020-12-03 03:48:44 +08:00
|
|
|
|
|
|
|
|
dto "github.com/prometheus/client_model/go"
|
|
|
|
|
"github.com/prometheus/common/expfmt"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Parser struct {
|
|
|
|
|
DefaultTags map[string]string
|
2021-01-08 00:21:09 +08:00
|
|
|
Header http.Header
|
2020-12-03 03:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *Parser) Parse(buf []byte) ([]telegraf.Metric, error) {
|
|
|
|
|
var parser expfmt.TextParser
|
|
|
|
|
var metrics []telegraf.Metric
|
|
|
|
|
var err error
|
|
|
|
|
// parse even if the buffer begins with a newline
|
|
|
|
|
buf = bytes.TrimPrefix(buf, []byte("\n"))
|
|
|
|
|
// Read raw data
|
|
|
|
|
buffer := bytes.NewBuffer(buf)
|
|
|
|
|
reader := bufio.NewReader(buffer)
|
|
|
|
|
|
|
|
|
|
// Prepare output
|
|
|
|
|
metricFamilies := make(map[string]*dto.MetricFamily)
|
2021-01-08 00:21:09 +08:00
|
|
|
mediatype, params, err := mime.ParseMediaType(p.Header.Get("Content-Type"))
|
|
|
|
|
if err == nil && mediatype == "application/vnd.google.protobuf" &&
|
|
|
|
|
params["encoding"] == "delimited" &&
|
|
|
|
|
params["proto"] == "io.prometheus.client.MetricFamily" {
|
|
|
|
|
for {
|
|
|
|
|
mf := &dto.MetricFamily{}
|
|
|
|
|
if _, ierr := pbutil.ReadDelimited(reader, mf); ierr != nil {
|
|
|
|
|
if ierr == io.EOF {
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
return nil, fmt.Errorf("reading metric family protocol buffer failed: %s", ierr)
|
|
|
|
|
}
|
|
|
|
|
metricFamilies[mf.GetName()] = mf
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
metricFamilies, err = parser.TextToMetricFamilies(reader)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("reading text format failed: %s", err)
|
|
|
|
|
}
|
2020-12-03 03:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
now := time.Now()
|
|
|
|
|
|
|
|
|
|
// read metrics
|
|
|
|
|
for metricName, mf := range metricFamilies {
|
|
|
|
|
for _, m := range mf.Metric {
|
|
|
|
|
// reading tags
|
2021-02-09 00:18:40 +08:00
|
|
|
tags := common.MakeLabels(m, p.DefaultTags)
|
2020-12-03 03:48:44 +08:00
|
|
|
|
|
|
|
|
if mf.GetType() == dto.MetricType_SUMMARY {
|
|
|
|
|
// summary metric
|
|
|
|
|
telegrafMetrics := makeQuantiles(m, tags, metricName, mf.GetType(), now)
|
|
|
|
|
metrics = append(metrics, telegrafMetrics...)
|
|
|
|
|
} else if mf.GetType() == dto.MetricType_HISTOGRAM {
|
|
|
|
|
// histogram metric
|
|
|
|
|
telegrafMetrics := makeBuckets(m, tags, metricName, mf.GetType(), now)
|
|
|
|
|
metrics = append(metrics, telegrafMetrics...)
|
|
|
|
|
} else {
|
|
|
|
|
// standard metric
|
|
|
|
|
// reading fields
|
2021-03-04 03:56:31 +08:00
|
|
|
fields := getNameAndValue(m, metricName)
|
2020-12-03 03:48:44 +08:00
|
|
|
// converting to telegraf metric
|
|
|
|
|
if len(fields) > 0 {
|
|
|
|
|
t := getTimestamp(m, now)
|
2021-04-14 02:40:03 +08:00
|
|
|
m := metric.New("prometheus", tags, fields, t, common.ValueType(mf.GetType()))
|
|
|
|
|
metrics = append(metrics, m)
|
2020-12-03 03:48:44 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return metrics, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *Parser) ParseLine(line string) (telegraf.Metric, error) {
|
|
|
|
|
metrics, err := p.Parse([]byte(line))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(metrics) < 1 {
|
2021-02-09 00:18:40 +08:00
|
|
|
return nil, fmt.Errorf("no metrics in line")
|
2020-12-03 03:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(metrics) > 1 {
|
2021-02-09 00:18:40 +08:00
|
|
|
return nil, fmt.Errorf("more than one metric in line")
|
2020-12-03 03:48:44 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return metrics[0], nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (p *Parser) SetDefaultTags(tags map[string]string) {
|
|
|
|
|
p.DefaultTags = tags
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get Quantiles for summary metric & Buckets for histogram
|
|
|
|
|
func makeQuantiles(m *dto.Metric, tags map[string]string, metricName string, metricType dto.MetricType, now time.Time) []telegraf.Metric {
|
|
|
|
|
var metrics []telegraf.Metric
|
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
|
t := getTimestamp(m, now)
|
|
|
|
|
|
|
|
|
|
fields[metricName+"_count"] = float64(m.GetSummary().GetSampleCount())
|
|
|
|
|
fields[metricName+"_sum"] = float64(m.GetSummary().GetSampleSum())
|
2021-04-14 02:40:03 +08:00
|
|
|
met := metric.New("prometheus", tags, fields, t, common.ValueType(metricType))
|
|
|
|
|
metrics = append(metrics, met)
|
2020-12-03 03:48:44 +08:00
|
|
|
|
|
|
|
|
for _, q := range m.GetSummary().Quantile {
|
|
|
|
|
newTags := tags
|
|
|
|
|
fields = make(map[string]interface{})
|
|
|
|
|
|
|
|
|
|
newTags["quantile"] = fmt.Sprint(q.GetQuantile())
|
|
|
|
|
fields[metricName] = float64(q.GetValue())
|
|
|
|
|
|
2021-04-14 02:40:03 +08:00
|
|
|
quantileMetric := metric.New("prometheus", newTags, fields, t, common.ValueType(metricType))
|
|
|
|
|
metrics = append(metrics, quantileMetric)
|
2020-12-03 03:48:44 +08:00
|
|
|
}
|
|
|
|
|
return metrics
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get Buckets from histogram metric
|
|
|
|
|
func makeBuckets(m *dto.Metric, tags map[string]string, metricName string, metricType dto.MetricType, now time.Time) []telegraf.Metric {
|
|
|
|
|
var metrics []telegraf.Metric
|
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
|
t := getTimestamp(m, now)
|
|
|
|
|
|
|
|
|
|
fields[metricName+"_count"] = float64(m.GetHistogram().GetSampleCount())
|
|
|
|
|
fields[metricName+"_sum"] = float64(m.GetHistogram().GetSampleSum())
|
|
|
|
|
|
2021-04-14 02:40:03 +08:00
|
|
|
met := metric.New("prometheus", tags, fields, t, common.ValueType(metricType))
|
|
|
|
|
metrics = append(metrics, met)
|
2020-12-03 03:48:44 +08:00
|
|
|
|
|
|
|
|
for _, b := range m.GetHistogram().Bucket {
|
|
|
|
|
newTags := tags
|
|
|
|
|
fields = make(map[string]interface{})
|
|
|
|
|
newTags["le"] = fmt.Sprint(b.GetUpperBound())
|
|
|
|
|
fields[metricName+"_bucket"] = float64(b.GetCumulativeCount())
|
|
|
|
|
|
2021-04-14 02:40:03 +08:00
|
|
|
histogramMetric := metric.New("prometheus", newTags, fields, t, common.ValueType(metricType))
|
|
|
|
|
metrics = append(metrics, histogramMetric)
|
2020-12-03 03:48:44 +08:00
|
|
|
}
|
|
|
|
|
return metrics
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get name and value from metric
|
|
|
|
|
func getNameAndValue(m *dto.Metric, metricName string) map[string]interface{} {
|
|
|
|
|
fields := make(map[string]interface{})
|
|
|
|
|
if m.Gauge != nil {
|
|
|
|
|
if !math.IsNaN(m.GetGauge().GetValue()) {
|
|
|
|
|
fields[metricName] = float64(m.GetGauge().GetValue())
|
|
|
|
|
}
|
|
|
|
|
} else if m.Counter != nil {
|
|
|
|
|
if !math.IsNaN(m.GetCounter().GetValue()) {
|
|
|
|
|
fields[metricName] = float64(m.GetCounter().GetValue())
|
|
|
|
|
}
|
|
|
|
|
} else if m.Untyped != nil {
|
|
|
|
|
if !math.IsNaN(m.GetUntyped().GetValue()) {
|
|
|
|
|
fields[metricName] = float64(m.GetUntyped().GetValue())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return fields
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func getTimestamp(m *dto.Metric, now time.Time) time.Time {
|
|
|
|
|
var t time.Time
|
|
|
|
|
if m.TimestampMs != nil && *m.TimestampMs > 0 {
|
|
|
|
|
t = time.Unix(0, m.GetTimestampMs()*1000000)
|
|
|
|
|
} else {
|
|
|
|
|
t = now
|
|
|
|
|
}
|
|
|
|
|
return t
|
|
|
|
|
}
|