forked from webdevops/azure-devops-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
collector_structs.go
87 lines (70 loc) · 2.26 KB
/
collector_structs.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"crypto/sha1"
"fmt"
"github.com/prometheus/client_golang/prometheus"
"strings"
"time"
)
type MetricCollectorRow struct {
labels prometheus.Labels
value float64
}
type MetricCollectorList struct {
list map[string]MetricCollectorRow
}
func NewMetricCollectorList() *MetricCollectorList {
ret := MetricCollectorList{}
ret.Init()
return &ret
}
func (m *MetricCollectorList) Init() {
m.list = map[string]MetricCollectorRow{}
}
func (m *MetricCollectorList) hashLabels(labels prometheus.Labels) string {
list := []string{}
for key, value := range labels {
list = append(list, fmt.Sprintf("%s=%s", key, value))
}
return fmt.Sprintf("%x", sha1.Sum([]byte(strings.Join(list, "#"))))
}
func (m *MetricCollectorList) Add(labels prometheus.Labels, value float64) {
m.list[m.hashLabels(labels)] = MetricCollectorRow{labels: labels, value: value}
}
func (m *MetricCollectorList) AddInfo(labels prometheus.Labels) {
m.list[m.hashLabels(labels)] = MetricCollectorRow{labels: labels, value: 1}
}
func (m *MetricCollectorList) AddTime(labels prometheus.Labels, value time.Time) {
timeValue := timeToFloat64(value)
if timeValue > 0 {
m.list[m.hashLabels(labels)] = MetricCollectorRow{labels: labels, value: timeValue}
}
}
func (m *MetricCollectorList) AddDuration(labels prometheus.Labels, value time.Duration) {
m.list[m.hashLabels(labels)] = MetricCollectorRow{labels: labels, value: value.Seconds()}
}
func (m *MetricCollectorList) AddIfNotZero(labels prometheus.Labels, value float64) {
if value != 0 {
m.list[m.hashLabels(labels)] = MetricCollectorRow{labels: labels, value: value}
}
}
func (m *MetricCollectorList) AddIfGreaterZero(labels prometheus.Labels, value float64) {
if value > 0 {
m.list[m.hashLabels(labels)] = MetricCollectorRow{labels: labels, value: value}
}
}
func (m *MetricCollectorList) GaugeSet(gauge *prometheus.GaugeVec) {
for _, metric := range m.list {
gauge.With(metric.labels).Set(metric.value)
}
}
func (m *MetricCollectorList) CounterAdd(counter *prometheus.CounterVec) {
for _, metric := range m.list {
counter.With(metric.labels).Add(metric.value)
}
}
func (m *MetricCollectorList) SummarySet(counter *prometheus.SummaryVec) {
for _, metric := range m.list {
counter.With(metric.labels).Observe(metric.value)
}
}