Skip to content

Commit

Permalink
When prefix is empty, no more dots should be written
Browse files Browse the repository at this point in the history
Signed-off-by: zhijian <djx@juicedata.io>
  • Loading branch information
zhijian-pro committed Mar 17, 2022
1 parent 868ec21 commit 22b0192
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
12 changes: 7 additions & 5 deletions prometheus/graphite/bridge.go
Expand Up @@ -197,14 +197,16 @@ func writeMetrics(w io.Writer, mfs []*dto.MetricFamily, useTags bool, prefix str

buf := bufio.NewWriter(w)
for _, s := range vec {
for _, c := range prefix {
if _, err := buf.WriteRune(c); err != nil {
if prefix != "" {
for _, c := range prefix {
if _, err := buf.WriteRune(c); err != nil {
return err
}
}
if err := buf.WriteByte('.'); err != nil {
return err
}
}
if err := buf.WriteByte('.'); err != nil {
return err
}
if err := writeMetric(buf, s.Metric, useTags); err != nil {
return err
}
Expand Down
14 changes: 10 additions & 4 deletions prometheus/graphite/bridge_test.go
Expand Up @@ -101,6 +101,7 @@ func testWriteSummary(t *testing.T, useTags bool) {
{prefix: "prefix"},
{prefix: "pre/fix"},
{prefix: "pre.fix"},
{prefix: ""},
}

var (
Expand Down Expand Up @@ -141,10 +142,15 @@ func testWriteSummary(t *testing.T, useTags bool) {
t.Fatalf("error: %v", err)
}

wantWithPrefix := fmt.Sprintf(want,
tc.prefix, tc.prefix, tc.prefix, tc.prefix, tc.prefix,
tc.prefix, tc.prefix, tc.prefix, tc.prefix, tc.prefix,
)
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()

Expand Down

0 comments on commit 22b0192

Please sign in to comment.