Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from Mar 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.", "")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sneaky, but makes sense. (:

} 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