fix: apply suggested changes

Signed-off-by: François Gouteroux <francois.gouteroux@gmail.com>
This commit is contained in:
François Gouteroux 2023-06-19 10:44:24 +02:00
parent 6ae4a46845
commit 3eaa7eb538

View file

@ -44,8 +44,8 @@ var MetricMetadataTypeValue = map[string]int32{
"STATESET": 7, "STATESET": 7,
} }
// FormatMetrics convert metric family to a writerequest. // CreateWriteRequest convert metric family to a writerequest.
func FormatMetrics(mf map[string]*dto.MetricFamily, extraLabels map[string]string) (*prompb.WriteRequest, error) { func CreateWriteRequest(mf map[string]*dto.MetricFamily, extraLabels map[string]string) (*prompb.WriteRequest, error) {
wr := &prompb.WriteRequest{} wr := &prompb.WriteRequest{}
// build metric list // build metric list
@ -76,16 +76,16 @@ func FormatMetrics(mf map[string]*dto.MetricFamily, extraLabels map[string]strin
return wr, nil return wr, nil
} }
func makeTimeserie(wr *prompb.WriteRequest, labels map[string]string, timestamp int64, value float64) { func toTimeseries(wr *prompb.WriteRequest, labels map[string]string, timestamp int64, value float64) {
var timeserie prompb.TimeSeries var ts prompb.TimeSeries
timeserie.Labels = makeLabels(labels) ts.Labels = makeLabels(labels)
timeserie.Samples = []prompb.Sample{ ts.Samples = []prompb.Sample{
{ {
Timestamp: timestamp, Timestamp: timestamp,
Value: value, Value: value,
}, },
} }
wr.Timeseries = append(wr.Timeseries, timeserie) wr.Timeseries = append(wr.Timeseries, ts)
} }
func makeTimeseries(wr *prompb.WriteRequest, labels map[string]string, m *dto.Metric) error { func makeTimeseries(wr *prompb.WriteRequest, labels map[string]string, m *dto.Metric) error {
@ -98,9 +98,9 @@ func makeTimeseries(wr *prompb.WriteRequest, labels map[string]string, m *dto.Me
switch { switch {
case m.Gauge != nil: case m.Gauge != nil:
makeTimeserie(wr, labels, timestamp, m.GetGauge().GetValue()) toTimeseries(wr, labels, timestamp, m.GetGauge().GetValue())
case m.Counter != nil: case m.Counter != nil:
makeTimeserie(wr, labels, timestamp, m.GetCounter().GetValue()) toTimeseries(wr, labels, timestamp, m.GetCounter().GetValue())
case m.Summary != nil: case m.Summary != nil:
metricName := labels[model.MetricNameLabel] metricName := labels[model.MetricNameLabel]
// Preserve metric name order with first quantile labels timeseries then sum suffix timeserie and finally count suffix timeserie // Preserve metric name order with first quantile labels timeseries then sum suffix timeserie and finally count suffix timeserie
@ -112,15 +112,15 @@ func makeTimeseries(wr *prompb.WriteRequest, labels map[string]string, m *dto.Me
for _, q := range m.GetSummary().Quantile { for _, q := range m.GetSummary().Quantile {
quantileLabels[model.QuantileLabel] = fmt.Sprint(q.GetQuantile()) quantileLabels[model.QuantileLabel] = fmt.Sprint(q.GetQuantile())
makeTimeserie(wr, quantileLabels, timestamp, q.GetValue()) toTimeseries(wr, quantileLabels, timestamp, q.GetValue())
} }
// Overwrite label model.MetricNameLabel for count and sum metrics // Overwrite label model.MetricNameLabel for count and sum metrics
// Add Summary sum timeserie // Add Summary sum timeserie
labels[model.MetricNameLabel] = metricName + sumStr labels[model.MetricNameLabel] = metricName + sumStr
makeTimeserie(wr, labels, timestamp, m.GetSummary().GetSampleSum()) toTimeseries(wr, labels, timestamp, m.GetSummary().GetSampleSum())
// Add Summary count timeserie // Add Summary count timeserie
labels[model.MetricNameLabel] = metricName + countStr labels[model.MetricNameLabel] = metricName + countStr
makeTimeserie(wr, labels, timestamp, float64(m.GetSummary().GetSampleCount())) toTimeseries(wr, labels, timestamp, float64(m.GetSummary().GetSampleCount()))
case m.Histogram != nil: case m.Histogram != nil:
metricName := labels[model.MetricNameLabel] metricName := labels[model.MetricNameLabel]
@ -133,18 +133,18 @@ func makeTimeseries(wr *prompb.WriteRequest, labels map[string]string, m *dto.Me
for _, b := range m.GetHistogram().Bucket { for _, b := range m.GetHistogram().Bucket {
bucketLabels[model.MetricNameLabel] = metricName + bucketStr bucketLabels[model.MetricNameLabel] = metricName + bucketStr
bucketLabels[model.BucketLabel] = fmt.Sprint(b.GetUpperBound()) bucketLabels[model.BucketLabel] = fmt.Sprint(b.GetUpperBound())
makeTimeserie(wr, bucketLabels, timestamp, float64(b.GetCumulativeCount())) toTimeseries(wr, bucketLabels, timestamp, float64(b.GetCumulativeCount()))
} }
// Overwrite label model.MetricNameLabel for count and sum metrics // Overwrite label model.MetricNameLabel for count and sum metrics
// Add Histogram sum timeserie // Add Histogram sum timeserie
labels[model.MetricNameLabel] = metricName + sumStr labels[model.MetricNameLabel] = metricName + sumStr
makeTimeserie(wr, labels, timestamp, m.GetHistogram().GetSampleSum()) toTimeseries(wr, labels, timestamp, m.GetHistogram().GetSampleSum())
// Add Histogram count timeserie // Add Histogram count timeserie
labels[model.MetricNameLabel] = metricName + countStr labels[model.MetricNameLabel] = metricName + countStr
makeTimeserie(wr, labels, timestamp, float64(m.GetHistogram().GetSampleCount())) toTimeseries(wr, labels, timestamp, float64(m.GetHistogram().GetSampleCount()))
case m.Untyped != nil: case m.Untyped != nil:
makeTimeserie(wr, labels, timestamp, m.GetUntyped().GetValue()) toTimeseries(wr, labels, timestamp, m.GetUntyped().GetValue())
default: default:
err = errors.New("unsupported metric type") err = errors.New("unsupported metric type")
} }
@ -208,5 +208,5 @@ func ParseMetricsTextAndFormat(input io.Reader, labels map[string]string) (*prom
if err != nil { if err != nil {
return nil, err return nil, err
} }
return FormatMetrics(mf, labels) return CreateWriteRequest(mf, labels)
} }