-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmetrics.go
228 lines (195 loc) · 6.52 KB
/
metrics.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package main
import (
"context"
"fmt"
"io"
"log"
"os"
"regexp"
"sync"
"time"
"github.com/danroux/sk8l/protos"
"google.golang.org/grpc"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
var metricsNamesMap = &sync.Map{}
var (
namespace = os.Getenv("K8_NAMESPACE")
optNamespace = "sk8l"
summaryMap = &sync.Map{}
failingCronjobsOpts = prometheus.GaugeOpts{
Namespace: optNamespace,
Name: "failing_cronjobs_total",
Subsystem: namespace,
}
runningCronjobsOpts = prometheus.GaugeOpts{
Namespace: optNamespace,
Name: "running_cronjobs_total",
Subsystem: namespace,
}
completedCronjobsOpts = prometheus.GaugeOpts{
Namespace: optNamespace,
Name: "completed_cronjobs_total",
Subsystem: namespace,
}
registeredCronjobsOpts = prometheus.GaugeOpts{
Namespace: optNamespace,
Name: "registered_cronjobs_total",
Subsystem: namespace,
}
failingCronjobsGauge = promauto.NewGauge(failingCronjobsOpts)
runningCronjobsGauge = promauto.NewGauge(runningCronjobsOpts)
completedCronjobsGauge = promauto.NewGauge(completedCronjobsOpts)
registeredCronjobsGauge = promauto.NewGauge(registeredCronjobsOpts)
cronjobFailingJobs float64
cronjobCompletions float64
completedCronjobs float64
jobDuration float64
failingJobs float64
runningCronjobs float64
cronjobCompletionsOpts prometheus.GaugeOpts
cronjobDurationOpts prometheus.GaugeOpts
failingJobsOpts prometheus.GaugeOpts
completionsKey string
durationKey string
failuresKey string
metricNameRegex = regexp.MustCompile(`_*[^0-9A-Za-z_]+_*`)
)
func recordMetrics(ctx context.Context, svr *Sk8lServer) {
conn, err := grpc.NewClient(svr.Target, svr.Options...)
if err != nil {
panic(fmt.Sprintf("grpc.NewClient(%s) failed: %v", svr.Target, err))
}
c := protos.NewCronjobClient(conn)
subSystem := svr.K8sClient.namespace
log.Println("Metrics: Starting metrics collection")
req := &protos.CronjobsRequest{}
cronjobsClient, err := c.GetCronjobs(ctx, req)
if err != nil {
panic(err)
}
go func() {
for {
select {
case <-ctx.Done():
log.Println("Metrics: Shutdown - Stopping metrics collection")
return
default:
cronjobsResponse, err := cronjobsClient.Recv()
if err == io.EOF {
break
}
if err != nil {
log.Printf("Error: %v.GetCronjobs(_) = _, %v\n", cronjobsClient, err)
}
registeredCronjobs := len(cronjobsResponse.Cronjobs)
registeredCronjobsGauge.Set(float64(registeredCronjobs))
var metricNames []string
for _, cj := range cronjobsResponse.Cronjobs {
sanitizedCjName := sanitizeMetricName(cj.Name)
runningCronjobs += float64(len(cj.RunningJobs))
completionMetricName := fmt.Sprintf("%s_completion_total", sanitizedCjName)
failureMetricName := fmt.Sprintf("%s_failure_total", sanitizedCjName)
durationMetricName := fmt.Sprintf("%s_duration_seconds", sanitizedCjName)
metricNames = []string{
fmt.Sprintf("%s_%s", MetricPrefix, completionMetricName),
fmt.Sprintf("%s_%s", MetricPrefix, failureMetricName),
fmt.Sprintf("%s_%s", MetricPrefix, durationMetricName),
}
metricsNamesMap.Store(sanitizedCjName, metricNames)
for _, job := range cj.Jobs {
if job.Failed {
cronjobFailingJobs++
}
if job.Status.CompletionTime != nil {
cronjobCompletions++
}
sanitizedJobName := job.Name
labels := prometheus.Labels{}
labels["job_name"] = sanitizedJobName
cronjobDurationOpts = prometheus.GaugeOpts{
Name: durationMetricName,
Namespace: optNamespace,
Subsystem: subSystem,
Help: fmt.Sprintf("Duration of %s in seconds", sanitizedCjName),
ConstLabels: labels,
}
durationKey = fmt.Sprintf(
"%s_%s_%s_%s_durations",
cronjobDurationOpts.Namespace,
cronjobDurationOpts.Subsystem,
sanitizedCjName,
sanitizedJobName,
)
if *job.Status.Active > 0 {
jobDuration = float64(job.DurationInS)
} else {
jobDuration = float64(0)
}
if jobsDurationssGauge, ok := summaryMap.Load(durationKey); ok {
jobsDurationssGauge.(prometheus.Gauge).Set(jobDuration)
} else {
jobsDurationssGauge := promauto.NewGauge(cronjobDurationOpts)
summaryMap.Store(durationKey, jobsDurationssGauge)
jobsDurationssGauge.Set(jobDuration)
}
}
cronjobCompletionsOpts = prometheus.GaugeOpts{
Name: completionMetricName,
Namespace: optNamespace,
Subsystem: subSystem,
Help: fmt.Sprintf("%s completion total", sanitizedCjName),
}
completionsKey = fmt.Sprintf(
"%s_%s_%s_completions",
cronjobCompletionsOpts.Namespace,
cronjobCompletionsOpts.Subsystem,
sanitizedCjName,
)
if cronjobCompletionsGauge, ok := summaryMap.Load(completionsKey); ok {
cronjobCompletionsGauge.(prometheus.Gauge).Set(cronjobCompletions)
} else {
cronjobCompletionsGauge := promauto.NewGauge(cronjobCompletionsOpts)
summaryMap.Store(completionsKey, cronjobCompletionsGauge)
cronjobCompletionsGauge.Set(cronjobCompletions)
}
failingJobsOpts = prometheus.GaugeOpts{
Name: failureMetricName,
Namespace: optNamespace,
Subsystem: subSystem,
Help: fmt.Sprintf("%s failure total", sanitizedCjName),
}
failuresKey = fmt.Sprintf(
"%s_%s_%s_failures",
failingJobsOpts.Namespace,
failingJobsOpts.Subsystem,
sanitizedCjName,
)
if failingJobsGauge, ok := summaryMap.Load(failuresKey); ok {
failingJobsGauge.(prometheus.Gauge).Set(cronjobFailingJobs)
} else {
failingJobsGauge := promauto.NewGauge(failingJobsOpts)
summaryMap.Store(failuresKey, failingJobsGauge)
failingJobsGauge.Set(cronjobFailingJobs)
}
failingJobs += cronjobFailingJobs
completedCronjobs += cronjobCompletions
cronjobFailingJobs = 0
cronjobCompletions = 0
}
runningCronjobsGauge.Set(runningCronjobs)
failingCronjobsGauge.Set(failingJobs)
completedCronjobsGauge.Set(completedCronjobs)
failingJobs = 0
runningCronjobs = 0
completedCronjobs = 0
time.Sleep(10 * time.Second)
}
}
}()
}
// https://github.com/prometheus/node_exporter/blob/4a1b77600c1873a8233f3ffb55afcedbb63b8d84/collector/helper.go#L48
func sanitizeMetricName(metricName string) string {
return metricNameRegex.ReplaceAllString(metricName, "_")
}