Do not sanitize prefix in Graphite bridge

Refer to #418.

Signed-off-by: neeral <neeral@users.noreply.github.com>
This commit is contained in:
neeral 2018-06-22 10:16:17 -07:00
parent faf4ec335f
commit fe6c0fa342
2 changed files with 38 additions and 20 deletions

View File

@ -191,9 +191,11 @@ func writeMetrics(w io.Writer, mfs []*dto.MetricFamily, prefix string, now model
buf := bufio.NewWriter(w) buf := bufio.NewWriter(w)
for _, s := range vec { for _, s := range vec {
if err := writeSanitized(buf, prefix); err != nil { for _, c := range prefix {
if _, err := buf.WriteRune(c); err != nil {
return err return err
} }
}
if err := buf.WriteByte('.'); err != nil { if err := buf.WriteByte('.'); err != nil {
return err return err
} }
@ -273,7 +275,7 @@ func replaceInvalidRune(c rune) rune {
if c == ' ' { if c == ' ' {
return '.' 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 '_'
} }
return c return c

View File

@ -3,6 +3,7 @@ package graphite
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"fmt"
"io" "io"
"log" "log"
"net" "net"
@ -25,6 +26,7 @@ func TestSanitize(t *testing.T) {
{in: "hE/l1o", out: "hE_l1o"}, {in: "hE/l1o", out: "hE_l1o"},
{in: "he,*ll(.o", out: "he_ll_o"}, {in: "he,*ll(.o", out: "he_ll_o"},
{in: "hello_there%^&", out: "hello_there_"}, {in: "hello_there%^&", out: "hello_there_"},
{in: "hell-.o", out: "hell-_o"},
} }
var buf bytes.Buffer var buf bytes.Buffer
@ -72,27 +74,41 @@ func TestWriteSummary(t *testing.T) {
t.Fatalf("error: %v", err) t.Fatalf("error: %v", err)
} }
testCases := []struct {
prefix string
}{
{prefix: "prefix"},
{prefix: "pre/fix"},
{prefix: "pre.fix"},
}
const want = `%s.name.constname.constvalue.labelname.val1.quantile.0_5 20 1477043
%s.name.constname.constvalue.labelname.val1.quantile.0_9 30 1477043
%s.name.constname.constvalue.labelname.val1.quantile.0_99 30 1477043
%s.name_sum.constname.constvalue.labelname.val1 60 1477043
%s.name_count.constname.constvalue.labelname.val1 3 1477043
%s.name.constname.constvalue.labelname.val2.quantile.0_5 30 1477043
%s.name.constname.constvalue.labelname.val2.quantile.0_9 40 1477043
%s.name.constname.constvalue.labelname.val2.quantile.0_99 40 1477043
%s.name_sum.constname.constvalue.labelname.val2 90 1477043
%s.name_count.constname.constvalue.labelname.val2 3 1477043
`
for i, tc := range testCases {
now := model.Time(1477043083) now := model.Time(1477043083)
var buf bytes.Buffer var buf bytes.Buffer
err = writeMetrics(&buf, mfs, "prefix", now) err = writeMetrics(&buf, mfs, tc.prefix, now)
if err != nil { if err != nil {
t.Fatalf("error: %v", err) t.Fatalf("error: %v", err)
} }
want := `prefix.name.constname.constvalue.labelname.val1.quantile.0_5 20 1477043 wantWithPrefix := fmt.Sprintf(want,
prefix.name.constname.constvalue.labelname.val1.quantile.0_9 30 1477043 tc.prefix, tc.prefix, tc.prefix, tc.prefix, tc.prefix,
prefix.name.constname.constvalue.labelname.val1.quantile.0_99 30 1477043 tc.prefix, tc.prefix, tc.prefix, tc.prefix, tc.prefix,
prefix.name_sum.constname.constvalue.labelname.val1 60 1477043 )
prefix.name_count.constname.constvalue.labelname.val1 3 1477043 if got := buf.String(); wantWithPrefix != got {
prefix.name.constname.constvalue.labelname.val2.quantile.0_5 30 1477043 t.Fatalf("test case index %d: wanted \n%s\n, got \n%s\n", i, wantWithPrefix, got)
prefix.name.constname.constvalue.labelname.val2.quantile.0_9 40 1477043 }
prefix.name.constname.constvalue.labelname.val2.quantile.0_99 40 1477043
prefix.name_sum.constname.constvalue.labelname.val2 90 1477043
prefix.name_count.constname.constvalue.labelname.val2 3 1477043
`
if got := buf.String(); want != got {
t.Fatalf("wanted \n%s\n, got \n%s\n", want, got)
} }
} }