-
Notifications
You must be signed in to change notification settings - Fork 0
/
gauge.go
68 lines (59 loc) · 1.59 KB
/
gauge.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
57
58
59
60
61
62
63
64
65
66
67
68
package metrics_exporter
import (
"fmt"
"sort"
"strings"
)
type Gauge struct {
Label string
Value string
}
type GaugesMetric struct {
Name string
Help string
Type string
Gauges []Gauge
}
func NewGaugesMetrics(Name string, Help string, Type string) (gm *GaugesMetric) {
return &GaugesMetric{Name: Name, Help: Help, Type: Type}
}
func (gm *GaugesMetric) ParseMetricMap(MetricMap map[string]string, Value string) {
labels := make([]string, 0, len(MetricMap))
for k, v := range MetricMap {
if k == "__name__" || k == "job" || k == "instance" {
continue
}
labels = append(labels, fmt.Sprintf("%s=\"%s\"", k, v))
}
sort.Strings(labels)
sortedLabels := strings.Join(labels, ",")
gauge := gm.InitCounterByLabel(sortedLabels)
gauge.Value = Value
}
func (gm *GaugesMetric) InitCounterByLabel(label string) *Gauge {
counterMetric := Gauge{Label: label}
if gm.Gauges == nil {
gm.Gauges = []Gauge{}
}
gm.Gauges = append(gm.Gauges, counterMetric)
return &gm.Gauges[len(gm.Gauges)-1]
}
func (gm *GaugesMetric) Sort() {
sort.Slice(gm.Gauges, func(i, j int) bool {
return gm.Gauges[i].Label < gm.Gauges[j].Label
})
}
func (gm *GaugesMetric) Print() string {
gm.Sort()
var builder strings.Builder
builder.WriteString(fmt.Sprintf("# HELP %s %s\n", gm.Name, gm.Help))
builder.WriteString(fmt.Sprintf("# TYPE %s %s\n", gm.Name, gm.Type))
for _, gauge := range gm.Gauges {
if gauge.Label == "" {
builder.WriteString(fmt.Sprintf("%s %s\n", gm.Name, gauge.Value))
} else {
builder.WriteString(fmt.Sprintf("%s{%s} %s\n", gm.Name, gauge.Label, gauge.Value))
}
}
return builder.String()
}