make bridge prefix backwards compatible

This commit is contained in:
bergquist 2017-09-07 11:34:26 +02:00 committed by Carl Bergquist
parent 6d22a67a30
commit c177a9a77a
2 changed files with 53 additions and 12 deletions

View File

@ -220,10 +220,7 @@ func (b *Bridge) writeMetrics(w io.Writer, mfs []*dto.MetricFamily, prefix strin
buf := bufio.NewWriter(w)
for _, s := range vec {
if err := writeSanitized(buf, prefix); err != nil {
return err
}
if err := buf.WriteByte('.'); err != nil {
if err := writePrefix(buf, prefix); err != nil {
return err
}
@ -324,6 +321,16 @@ func addExtentionConventionForRollups(buf *bufio.Writer, mf *dto.MetricFamily, m
return nil
}
func writePrefix(buf *bufio.Writer, s string) error {
for _, c := range s {
if _, err := buf.WriteRune(replaceInvalid(c)); err != nil {
return err
}
}
return nil
}
func writeSanitized(buf *bufio.Writer, s string) error {
prevUnderscore := false
@ -345,6 +352,13 @@ func writeSanitized(buf *bufio.Writer, s string) error {
return nil
}
func replaceInvalid(c rune) rune {
if c == ' ' || c == '.' {
return '.'
}
return replaceInvalidRune(c)
}
func replaceInvalidRune(c rune) rune {
if c == ' ' {
return '.'

View File

@ -96,6 +96,33 @@ func TestSanitize(t *testing.T) {
}
}
func TestSanitizePrefix(t *testing.T) {
testCases := []struct {
in, out string
}{
{in: "service.prod.", out: "service.prod."},
{in: "service.prod", out: "service.prod"},
}
var buf bytes.Buffer
w := bufio.NewWriter(&buf)
for i, tc := range testCases {
if err := writePrefix(w, tc.in); err != nil {
t.Fatalf("write failed: %v", err)
}
if err := w.Flush(); err != nil {
t.Fatalf("flush failed: %v", err)
}
if want, got := tc.out, buf.String(); want != got {
t.Fatalf("test case index %d: got sanitized string %s, want %s", i, got, want)
}
buf.Reset()
}
}
func TestWriteSummary(t *testing.T) {
sumVec := prometheus.NewSummaryVec(
prometheus.SummaryOpts{
@ -133,7 +160,7 @@ func TestWriteSummary(t *testing.T) {
now := model.Time(1477043083)
var buf bytes.Buffer
err = b.writeMetrics(&buf, mfs, "prefix", now)
err = b.writeMetrics(&buf, mfs, "prefix.", now)
if err != nil {
t.Fatalf("error: %v", err)
}
@ -192,7 +219,7 @@ func TestWriteHistogram(t *testing.T) {
now := model.Time(1477043083)
var buf bytes.Buffer
err = b.writeMetrics(&buf, mfs, "prefix", now)
err = b.writeMetrics(&buf, mfs, "prefix.", now)
if err != nil {
t.Fatalf("error: %v", err)
}
@ -249,7 +276,7 @@ func TestCounterVec(t *testing.T) {
}
var buf bytes.Buffer
err = b.writeMetrics(&buf, mfs, "prefix", model.Time(1477043083))
err = b.writeMetrics(&buf, mfs, "prefix.", model.Time(1477043083))
if err != nil {
t.Fatalf("error: %v", err)
}
@ -271,7 +298,7 @@ prefix.page.response.constname.constvalue.labelname.val2.count 1 1477043
}
buf = bytes.Buffer{}
err = b.writeMetrics(&buf, mfs, "prefix", model.Time(1477053083))
err = b.writeMetrics(&buf, mfs, "prefix.", model.Time(1477053083))
if err != nil {
t.Fatalf("error: %v", err)
}
@ -313,7 +340,7 @@ func TestCounter(t *testing.T) {
}
var buf bytes.Buffer
err = b.writeMetrics(&buf, mfs, "prefix", model.Time(1477043083))
err = b.writeMetrics(&buf, mfs, "prefix.", model.Time(1477043083))
if err != nil {
t.Fatalf("error: %v", err)
}
@ -332,7 +359,7 @@ func TestCounter(t *testing.T) {
}
buf = bytes.Buffer{}
err = b.writeMetrics(&buf, mfs, "prefix", model.Time(1477053083))
err = b.writeMetrics(&buf, mfs, "prefix.", model.Time(1477053083))
if err != nil {
t.Fatalf("error: %v", err)
}
@ -372,7 +399,7 @@ func TestCanIgnoreSomeMetrics(t *testing.T) {
}
var buf bytes.Buffer
err = b.writeMetrics(&buf, mfs, "prefix", model.Time(1477043083))
err = b.writeMetrics(&buf, mfs, "prefix.", model.Time(1477043083))
if err != nil {
t.Fatalf("error: %v", err)
}
@ -402,7 +429,7 @@ func TestPush(t *testing.T) {
b, err := NewBridge(&Config{
URL: host + port,
Gatherer: reg,
Prefix: "prefix",
Prefix: "prefix.",
})
if err != nil {
t.Fatalf("error creating bridge: %v", err)