-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
56 lines (49 loc) · 1.47 KB
/
http.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
46
47
48
49
50
51
52
53
54
55
56
package promgo
import (
"fmt"
"net/http"
"sort"
"strings"
)
// Render ...
func Render() http.HandlerFunc {
return func(rw http.ResponseWriter, r *http.Request) {
m := defaultRegistry.Collect()
metrics := Metrics(m)
sort.Sort(metrics)
metricGroup := make(map[*Desc]Metrics)
for _, metric := range metrics {
if _, ok := metricGroup[metric.Desc]; !ok {
metricGroup[metric.Desc] = make([]Metric, 0)
}
metricGroup[metric.Desc] = append(metricGroup[metric.Desc], metric)
}
descs := make(Descs, 0, len(metricGroup))
for desc := range metricGroup {
descs = append(descs, desc)
}
sort.Sort(descs)
lines := []string{}
for _, desc := range descs {
lines = append(lines, fmt.Sprintf(`# HELP %s %s`, desc.GetName(), desc.GetHelp()))
lines = append(lines, fmt.Sprintf(`# TYPE %s %s`, desc.GetName(), desc.GetType()))
group := metricGroup[desc]
sort.Sort(group)
for _, m := range group {
vv := make([]string, 0, len(m.ConstLabels))
if len(m.ConstLabels) == 0 {
lines = append(lines, fmt.Sprintf(`%s %.2f`, m.GetFQName(), m.GetValue()))
continue
}
for _, l := range m.Desc.Labels {
vv = append(vv, fmt.Sprintf(`%s="%s"`, l, m.ConstLabels[l]))
}
lines = append(lines, fmt.Sprintf(`%s{%s} %.2f`, m.GetFQName(), strings.Join(vv, `,`), m.GetValue()))
}
}
lines = append(lines, "\n")
html := strings.Join(lines, "\n")
rw.Header().Set(`Content-Type`, `text/plain;charset=utf-8`)
fmt.Fprint(rw, html)
}
}