-
Notifications
You must be signed in to change notification settings - Fork 2
/
prometheus.go
73 lines (64 loc) · 1.5 KB
/
prometheus.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
69
70
71
72
73
package main
import (
"fmt"
"io"
"strings"
)
type metrics []metric
type metric struct {
Name string
Tags *map[string]string
Value float64
}
type CovidStat struct {
location string
infected uint64
deaths uint64
}
type Exporter interface {
GetMetrics() (metrics, error)
Health() []error
}
func writeMetrics(metrics metrics, w io.Writer) error {
for _, m := range metrics {
_, err := io.WriteString(w, formatMetric(m))
if err != nil {
return err
}
}
return nil
}
func formatMetric(m metric) string {
tags := []string{}
if m.Tags != nil {
for k, v := range *m.Tags {
tags = append(tags, k+`="`+v+`"`)
}
return fmt.Sprintf("%s{%s} %f\n", m.Name, strings.Join(tags, ","), m.Value)
}
return fmt.Sprintf("%s %f\n", m.Name, m.Value)
}
func (metrics metrics) findMetric(metricName string, tagMatch string) *metric {
for _, m := range metrics {
if m.Name == metricName && tagMatch == "" {
return &m
} else if m.Name == metricName {
for k, v := range *m.Tags {
if fmt.Sprintf("%s=%s", k, v) == tagMatch {
return &m
}
}
}
}
return nil
}
func (metrics metrics) checkMetric(metricName, tagMatch string, checkFunction func(x float64) bool) error {
metric := metrics.findMetric(metricName, tagMatch)
if metric == nil {
return fmt.Errorf("Could not find metric %s / (%s)", metricName, tagMatch)
}
if !checkFunction((*metric).Value) {
return fmt.Errorf("Check metric for metric %s / (%s) failed with value: %f", metricName, tagMatch, (*metric).Value)
}
return nil
}