Skip to content

Commit 36b47eb

Browse files
authored
When prefix is empty, no more dots should be written (#1005)
Signed-off-by: zhijian <[email protected]>
1 parent 5d78aaa commit 36b47eb

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

prometheus/graphite/bridge.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,16 @@ func writeMetrics(w io.Writer, mfs []*dto.MetricFamily, useTags bool, prefix str
197197

198198
buf := bufio.NewWriter(w)
199199
for _, s := range vec {
200-
for _, c := range prefix {
201-
if _, err := buf.WriteRune(c); err != nil {
200+
if prefix != "" {
201+
for _, c := range prefix {
202+
if _, err := buf.WriteRune(c); err != nil {
203+
return err
204+
}
205+
}
206+
if err := buf.WriteByte('.'); err != nil {
202207
return err
203208
}
204209
}
205-
if err := buf.WriteByte('.'); err != nil {
206-
return err
207-
}
208210
if err := writeMetric(buf, s.Metric, useTags); err != nil {
209211
return err
210212
}

prometheus/graphite/bridge_test.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ func testWriteSummary(t *testing.T, useTags bool) {
101101
{prefix: "prefix"},
102102
{prefix: "pre/fix"},
103103
{prefix: "pre.fix"},
104+
{prefix: ""},
104105
}
105106

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

144-
wantWithPrefix := fmt.Sprintf(want,
145-
tc.prefix, tc.prefix, tc.prefix, tc.prefix, tc.prefix,
146-
tc.prefix, tc.prefix, tc.prefix, tc.prefix, tc.prefix,
147-
)
145+
var wantWithPrefix string
146+
if tc.prefix == "" {
147+
wantWithPrefix = strings.ReplaceAll(want, "%s.", "")
148+
} else {
149+
wantWithPrefix = fmt.Sprintf(want,
150+
tc.prefix, tc.prefix, tc.prefix, tc.prefix, tc.prefix,
151+
tc.prefix, tc.prefix, tc.prefix, tc.prefix, tc.prefix,
152+
)
153+
}
148154

149155
got := buf.String()
150156

0 commit comments

Comments
 (0)