From cd9ccf8a21454db2d012489d532c40338e950d4a Mon Sep 17 00:00:00 2001 From: Calle Pettersson Date: Wed, 12 Sep 2018 20:34:22 +0200 Subject: [PATCH] Set labels correctly --- linemetrics/gauge.go | 7 ++----- linemetrics/histogram.go | 8 ++------ linemetrics/linemetrics.go | 14 ++++++++++++++ linemetrics/summary.go | 8 ++------ 4 files changed, 20 insertions(+), 17 deletions(-) diff --git a/linemetrics/gauge.go b/linemetrics/gauge.go index 8e909ee..f9a2f9e 100644 --- a/linemetrics/gauge.go +++ b/linemetrics/gauge.go @@ -16,14 +16,11 @@ type GaugeVecLineMetric struct { func (gauge GaugeVecLineMetric) MatchLine(s string) { matches := gauge.pattern.FindStringSubmatch(s) if len(matches) > 0 { - captures := matches[1:] - valueStr := captures[gauge.valueIdx] - value, err := strconv.ParseFloat(valueStr, 64) + labels, value, err := getLabelsAndValue(matches, gauge.valueIdx) if err != nil { - log.Warnf("Unable to convert %s to float\n", valueStr) return } - gauge.metric.WithLabelValues(captures...).Set(value) + gauge.metric.WithLabelValues(labels...).Set(value) } } diff --git a/linemetrics/histogram.go b/linemetrics/histogram.go index e244653..95b3441 100644 --- a/linemetrics/histogram.go +++ b/linemetrics/histogram.go @@ -36,15 +36,11 @@ type HistogramVecLineMetric struct { func (histogram HistogramVecLineMetric) MatchLine(s string) { matches := histogram.pattern.FindStringSubmatch(s) if len(matches) > 0 { - captures := matches[1:] - valueStr := captures[histogram.valueIdx] - value, err := strconv.ParseFloat(valueStr, 64) + labels, value, err := getLabelsAndValue(matches, histogram.valueIdx) if err != nil { - log.Warnf("Unable to convert %s to float\n", valueStr) return } - capturedLabels := append(captures[0:histogram.valueIdx], captures[histogram.valueIdx+1:]...) - histogram.metric.WithLabelValues(capturedLabels...).Observe(value) + histogram.metric.WithLabelValues(labels...).Observe(value) } } diff --git a/linemetrics/linemetrics.go b/linemetrics/linemetrics.go index eab5a36..c2d9568 100644 --- a/linemetrics/linemetrics.go +++ b/linemetrics/linemetrics.go @@ -4,8 +4,10 @@ import ( "errors" "io/ioutil" "regexp" + "strconv" "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/common/log" "gopkg.in/yaml.v2" ) @@ -84,3 +86,15 @@ func getValueCaptureIndex(labels []string) (int, error) { return valueIdx, nil } + +func getLabelsAndValue(matches []string, valueIdx int) ([]string, float64, error) { + captures := matches[1:] + valueStr := captures[valueIdx] + value, err := strconv.ParseFloat(valueStr, 64) + if err != nil { + log.Warnf("Unable to convert %s to float\n", valueStr) + return nil, 0, err + } + labels := append(captures[0:valueIdx], captures[valueIdx+1:]...) + return labels, value, nil +} diff --git a/linemetrics/summary.go b/linemetrics/summary.go index d031e1d..427af40 100644 --- a/linemetrics/summary.go +++ b/linemetrics/summary.go @@ -36,15 +36,11 @@ type SummaryVecLineMetric struct { func (summary SummaryVecLineMetric) MatchLine(s string) { matches := summary.pattern.FindStringSubmatch(s) if len(matches) > 0 { - captures := matches[1:] - valueStr := captures[summary.valueIdx] - value, err := strconv.ParseFloat(valueStr, 64) + labels, value, err := getLabelsAndValue(matches, summary.valueIdx) if err != nil { - log.Warnf("Unable to convert %s to float\n", valueStr) return } - capturedLabels := append(captures[0:summary.valueIdx], captures[summary.valueIdx+1:]...) - summary.metric.WithLabelValues(capturedLabels...).Observe(value) + summary.metric.WithLabelValues(labels...).Observe(value) } }