Browse Source

skip NaN values when writing to graphite

ref #9279
bergquist 8 years ago
parent
commit
0a8a6fd872

+ 6 - 1
pkg/metrics/graphitebridge/graphite.go

@@ -20,6 +20,7 @@ import (
 	"errors"
 	"fmt"
 	"io"
+	"math"
 	"net"
 	"sort"
 	"strings"
@@ -208,6 +209,10 @@ func (b *Bridge) writeMetrics(w io.Writer, mfs []*dto.MetricFamily, prefix strin
 
 		buf := bufio.NewWriter(w)
 		for _, s := range vec {
+			if math.IsNaN(float64(s.Value)) {
+				continue
+			}
+
 			if err := writePrefix(buf, prefix); err != nil {
 				return err
 			}
@@ -357,7 +362,7 @@ func replaceInvalidRune(c rune) rune {
 	if c == ' ' {
 		return '.'
 	}
-	if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_' || c == ':' || (c >= '0' && c <= '9')) {
+	if !((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '-' || c == '_' || c == ':' || (c >= '0' && c <= '9')) {
 		return '_'
 	}
 	return c

+ 41 - 0
pkg/metrics/graphitebridge/graphite_test.go

@@ -410,6 +410,47 @@ func TestTrimGrafanaNamespace(t *testing.T) {
 	}
 }
 
+func TestSkipNanValues(t *testing.T) {
+	cntVec := prometheus.NewSummary(
+		prometheus.SummaryOpts{
+			Name:        "grafana_http_request_total",
+			Help:        "docstring",
+			ConstLabels: prometheus.Labels{"constname": "constvalue"},
+		})
+
+	reg := prometheus.NewRegistry()
+	reg.MustRegister(cntVec)
+
+	b, err := NewBridge(&Config{
+		URL:             "localhost:8080",
+		Gatherer:        reg,
+		CountersAsDelta: true,
+	})
+	if err != nil {
+		t.Fatalf("error creating bridge: %v", err)
+	}
+
+	// first collect
+	mfs, err := reg.Gather()
+	if err != nil {
+		t.Fatalf("error: %v", err)
+	}
+
+	var buf bytes.Buffer
+	err = b.writeMetrics(&buf, mfs, "prefix.", model.Time(1477043083))
+	if err != nil {
+		t.Fatalf("error: %v", err)
+	}
+
+	want := `prefix.http_request_total_sum.constname.constvalue 0 1477043
+prefix.http_request_total_count.constname.constvalue.count 0 1477043
+`
+
+	if got := buf.String(); want != got {
+		t.Fatalf("wanted \n%s\n, got \n%s\n", want, got)
+	}
+}
+
 func TestPush(t *testing.T) {
 	reg := prometheus.NewRegistry()
 	cntVec := prometheus.NewCounterVec(