When prefix is empty, no more dots should be written (#1005)

Signed-off-by: zhijian <djx@juicedata.io>
This commit is contained in:
zhijian 2022-03-17 23:43:50 +08:00 committed by GitHub
parent 5d78aaad41
commit 36b47eb0ab
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 9 deletions

View File

@ -197,6 +197,7 @@ func writeMetrics(w io.Writer, mfs []*dto.MetricFamily, useTags bool, prefix str
buf := bufio.NewWriter(w)
for _, s := range vec {
if prefix != "" {
for _, c := range prefix {
if _, err := buf.WriteRune(c); err != nil {
return err
@ -205,6 +206,7 @@ func writeMetrics(w io.Writer, mfs []*dto.MetricFamily, useTags bool, prefix str
if err := buf.WriteByte('.'); err != nil {
return err
}
}
if err := writeMetric(buf, s.Metric, useTags); err != nil {
return err
}

View File

@ -101,6 +101,7 @@ func testWriteSummary(t *testing.T, useTags bool) {
{prefix: "prefix"},
{prefix: "pre/fix"},
{prefix: "pre.fix"},
{prefix: ""},
}
var (
@ -141,10 +142,15 @@ func testWriteSummary(t *testing.T, useTags bool) {
t.Fatalf("error: %v", err)
}
wantWithPrefix := fmt.Sprintf(want,
var wantWithPrefix string
if tc.prefix == "" {
wantWithPrefix = strings.ReplaceAll(want, "%s.", "")
} else {
wantWithPrefix = fmt.Sprintf(want,
tc.prefix, tc.prefix, tc.prefix, tc.prefix, tc.prefix,
tc.prefix, tc.prefix, tc.prefix, tc.prefix, tc.prefix,
)
}
got := buf.String()