-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
104 lines (86 loc) · 2.41 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package main
import (
"regexp"
"strings"
"time"
"github.com/prometheus/client_golang/prometheus"
)
var (
cloudwatchAPICounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "yace_cloudwatch_requests_total",
Help: "Help is not implemented yet.",
})
)
type PrometheusMetric struct {
name *string
labels map[string]string
value *float64
includeTimestamp bool
timestamp time.Time
}
type PrometheusCollector struct {
metrics []*PrometheusMetric
}
func NewPrometheusCollector(metrics []*PrometheusMetric) *PrometheusCollector {
return &PrometheusCollector{
//metrics: removeDuplicatedMetrics(metrics),
metrics: metrics,
}
}
func (p *PrometheusCollector) Describe(descs chan<- *prometheus.Desc) {
for _, metric := range p.metrics {
descs <- createDesc(metric)
}
}
func (p *PrometheusCollector) Collect(metrics chan<- prometheus.Metric) {
for _, metric := range p.metrics {
metrics <- createMetric(metric)
}
}
func createDesc(metric *PrometheusMetric) *prometheus.Desc {
return prometheus.NewDesc(
*metric.name,
"Help is not implemented yet.",
nil,
metric.labels,
)
}
func createMetric(metric *PrometheusMetric) prometheus.Metric {
gauge := prometheus.NewGauge(prometheus.GaugeOpts{
Name: *metric.name,
Help: "Help is not implemented yet.",
ConstLabels: metric.labels,
})
gauge.Set(*metric.value)
if !metric.includeTimestamp {
return gauge
}
return prometheus.NewMetricWithTimestamp(metric.timestamp, gauge)
}
func removeDuplicatedMetrics(metrics []*PrometheusMetric) []*PrometheusMetric {
keys := make(map[string]bool)
filteredMetrics := []*PrometheusMetric{}
for _, metric := range metrics {
check := *metric.name + metric.labels["name"]
if _, value := keys[check]; !value {
keys[check] = true
filteredMetrics = append(filteredMetrics, metric)
}
}
return filteredMetrics
}
func promString(text string) string {
text = splitString(text)
return replaceWithUnderscores(text)
}
func promStringTag(text string) string {
return replaceWithUnderscores(text)
}
func replaceWithUnderscores(text string) string {
replacer := strings.NewReplacer(" ", "_", ",", "_", "\t", "_", ",", "_", "/", "_", "\\", "_", ".", "_", "-", "_", ":", "_", "=", "_")
return replacer.Replace(text)
}
func splitString(text string) string {
splitRegexp := regexp.MustCompile(`([a-z0-9])([A-Z])`)
return splitRegexp.ReplaceAllString(text, `$1.$2`)
}