forked from openshift/console
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.go
45 lines (41 loc) · 1.22 KB
/
helpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package metrics
import (
"bytes"
"strings"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/common/expfmt"
)
// (Only) Used in different metrics_tests.
//
// Format multiple metrics into the common prometheus text format with a comment
// for the metric title and description.
//
// # TEXT metric title
// # HELP metric description
// a_metric_total 1
func FormatMetrics(cs ...prometheus.Collector) string {
registry := prometheus.NewRegistry()
registry.MustRegister(cs...)
mfs, _ := registry.Gather()
writer := &bytes.Buffer{}
enc := expfmt.NewEncoder(writer, expfmt.FmtText)
for _, mf := range mfs {
enc.Encode(mf)
}
return writer.String()
}
// (Only) Used in different metrics_tests.
//
// Removes all lines starting with an # (comments) from the input string,
// so that the metric title and description could be ignored in unit tests.
func RemoveComments(s string) string {
lines := strings.Split(s, "\n")
filteredLines := make([]string, 0, len(lines))
for _, line := range lines {
trimmedLine := strings.TrimSpace(line)
if len(trimmedLine) > 0 && !strings.HasPrefix(trimmedLine, "#") {
filteredLines = append(filteredLines, trimmedLine)
}
}
return strings.Join(filteredLines, "\n")
}