Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from carlpett/issue/GH-9
Browse files Browse the repository at this point in the history
Set labels correctly
  • Loading branch information
carlpett authored Sep 12, 2018
2 parents f28516e + cd9ccf8 commit 7980722
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 17 deletions.
7 changes: 2 additions & 5 deletions linemetrics/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
8 changes: 2 additions & 6 deletions linemetrics/histogram.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
14 changes: 14 additions & 0 deletions linemetrics/linemetrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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
}
8 changes: 2 additions & 6 deletions linemetrics/summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down

0 comments on commit 7980722

Please sign in to comment.