forked from nerdswords/yet-another-cloudwatch-exporter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prometheus.go
157 lines (138 loc) · 3.83 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"regexp"
"sort"
"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.",
})
cloudwatchGetMetricDataAPICounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "yace_cloudwatch_getmetricdata_requests_total",
Help: "Help is not implemented yet.",
})
cloudwatchGetMetricStatisticsAPICounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "yace_cloudwatch_getmetricstatistics_requests_total",
Help: "Help is not implemented yet.",
})
resourceGroupTaggingAPICounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "yace_cloudwatch_resourcegrouptaggingapi_requests_total",
Help: "Help is not implemented yet.",
})
autoScalingAPICounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "yace_cloudwatch_autoscalingapi_requests_total",
Help: "Help is not implemented yet.",
})
apiGatewayAPICounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "yace_cloudwatch_apigatewayapi_requests_total",
})
ec2APICounter = prometheus.NewCounter(prometheus.CounterOpts{
Name: "yace_cloudwatch_ec2api_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),
}
}
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 + combineLabels(metric.labels)
if _, value := keys[check]; !value {
keys[check] = true
filteredMetrics = append(filteredMetrics, metric)
}
}
return filteredMetrics
}
func combineLabels(labels map[string]string) string {
var combinedLabels string
keys := make([]string, 0, len(labels))
for k := range labels {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
combinedLabels += promString(k) + promString(labels[k])
}
return combinedLabels
}
func promString(text string) string {
text = splitString(text)
return strings.ToLower(replaceWithUnderscores(text))
}
func promStringTag(text string) string {
if *labelsSnakeCase {
return promString(text)
}
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`)
}