This repository has been archived by the owner on Mar 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 115
/
prometheus.go
515 lines (448 loc) · 15.8 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
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
package emitter
import (
"fmt"
"net"
"net/http"
"sync"
"time"
"code.cloudfoundry.org/lager"
"github.com/concourse/atc/db"
"github.com/concourse/atc/metric"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
type PrometheusEmitter struct {
buildsStarted prometheus.Counter
buildsFinished prometheus.Counter
buildsSucceeded prometheus.Counter
buildsErrored prometheus.Counter
buildsFailed prometheus.Counter
buildsAborted prometheus.Counter
buildsFinishedVec *prometheus.CounterVec
buildDurationsVec *prometheus.HistogramVec
workerContainers *prometheus.GaugeVec
workerVolumes *prometheus.GaugeVec
httpRequestsDuration *prometheus.HistogramVec
schedulingFullDuration *prometheus.CounterVec
schedulingLoadingDuration *prometheus.CounterVec
pipelineScheduled *prometheus.CounterVec
dbQueriesTotal prometheus.Counter
dbConnections *prometheus.GaugeVec
resourceChecksVec *prometheus.CounterVec
workerLastSeen map[string]time.Time
mu sync.Mutex
}
type PrometheusConfig struct {
BindIP string `long:"prometheus-bind-ip" description:"IP to listen on to expose Prometheus metrics."`
BindPort string `long:"prometheus-bind-port" description:"Port to listen on to expose Prometheus metrics."`
}
func init() {
metric.RegisterEmitter(&PrometheusConfig{})
}
func (config *PrometheusConfig) Description() string { return "Prometheus" }
func (config *PrometheusConfig) IsConfigured() bool {
return config.BindPort != "" && config.BindIP != ""
}
func (config *PrometheusConfig) bind() string {
return fmt.Sprintf("%s:%s", config.BindIP, config.BindPort)
}
func (config *PrometheusConfig) NewEmitter() (metric.Emitter, error) {
// build metrics
buildsStarted := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "concourse",
Subsystem: "builds",
Name: "started_total",
Help: "Total number of Concourse builds started.",
})
prometheus.MustRegister(buildsStarted)
buildsFinished := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "concourse",
Subsystem: "builds",
Name: "finished_total",
Help: "Total number of Concourse builds finished.",
})
prometheus.MustRegister(buildsFinished)
buildsSucceeded := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "concourse",
Subsystem: "builds",
Name: "succeeded_total",
Help: "Total number of Concourse builds succeeded.",
})
prometheus.MustRegister(buildsSucceeded)
buildsErrored := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "concourse",
Subsystem: "builds",
Name: "errored_total",
Help: "Total number of Concourse builds errored.",
})
prometheus.MustRegister(buildsErrored)
buildsFailed := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "concourse",
Subsystem: "builds",
Name: "failed_total",
Help: "Total number of Concourse builds failed.",
})
prometheus.MustRegister(buildsFailed)
buildsAborted := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "concourse",
Subsystem: "builds",
Name: "aborted_total",
Help: "Total number of Concourse builds aborted.",
})
prometheus.MustRegister(buildsAborted)
buildsFinishedVec := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "concourse",
Subsystem: "builds",
Name: "finished",
Help: "Count of builds finished across various dimensions.",
},
[]string{"team", "pipeline", "status"},
)
prometheus.MustRegister(buildsFinishedVec)
buildDurationsVec := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "concourse",
Subsystem: "builds",
Name: "duration_seconds",
Help: "Build time in seconds",
Buckets: []float64{1, 60, 180, 300, 600, 900, 1200, 1800, 2700, 3600, 7200, 18000, 36000},
},
[]string{"team", "pipeline"},
)
prometheus.MustRegister(buildDurationsVec)
// worker metrics
workerContainers := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "concourse",
Subsystem: "workers",
Name: "containers",
Help: "Number of containers per worker",
},
[]string{"worker", "platform"},
)
prometheus.MustRegister(workerContainers)
workerVolumes := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "concourse",
Subsystem: "workers",
Name: "volumes",
Help: "Number of volumes per worker",
},
[]string{"worker", "platform"},
)
prometheus.MustRegister(workerVolumes)
// http metrics
httpRequestsDuration := prometheus.NewHistogramVec(
prometheus.HistogramOpts{
Namespace: "concourse",
Subsystem: "http_responses",
Name: "duration_seconds",
Help: "Response time in seconds",
},
[]string{"method", "route"},
)
prometheus.MustRegister(httpRequestsDuration)
// scheduling metrics
schedulingFullDuration := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "concourse",
Subsystem: "scheduling",
Name: "full_duration_seconds_total",
Help: "Total time taken to schedule an entire pipeline",
},
[]string{"pipeline"},
)
prometheus.MustRegister(schedulingFullDuration)
schedulingLoadingDuration := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "concourse",
Subsystem: "scheduling",
Name: "loading_duration_seconds_total",
Help: "Total time taken to load version information from the database for a pipeline",
},
[]string{"pipeline"},
)
prometheus.MustRegister(schedulingLoadingDuration)
pipelineScheduled := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "concourse",
Subsystem: "scheduling",
Name: "total",
Help: "Total number of times a pipeline has been scheduled",
},
[]string{"pipeline"},
)
prometheus.MustRegister(pipelineScheduled)
dbQueriesTotal := prometheus.NewCounter(prometheus.CounterOpts{
Namespace: "concourse",
Subsystem: "db",
Name: "queries_total",
Help: "Total number of database Concourse database queries",
})
prometheus.MustRegister(dbQueriesTotal)
dbConnections := prometheus.NewGaugeVec(
prometheus.GaugeOpts{
Namespace: "concourse",
Subsystem: "db",
Name: "connections",
Help: "Current number of concourse database connections",
},
[]string{"dbname"},
)
prometheus.MustRegister(dbConnections)
resourceChecksVec := prometheus.NewCounterVec(
prometheus.CounterOpts{
Namespace: "concourse",
Subsystem: "resource",
Name: "checks_total",
Help: "Counts the number of resource checks performed",
},
[]string{"team", "pipeline"},
)
prometheus.MustRegister(resourceChecksVec)
listener, err := net.Listen("tcp", config.bind())
if err != nil {
return nil, err
}
go http.Serve(listener, promhttp.Handler())
emitter := &PrometheusEmitter{
buildsStarted: buildsStarted,
buildsFinished: buildsFinished,
buildsFinishedVec: buildsFinishedVec,
buildDurationsVec: buildDurationsVec,
buildsSucceeded: buildsSucceeded,
buildsErrored: buildsErrored,
buildsFailed: buildsFailed,
buildsAborted: buildsAborted,
workerContainers: workerContainers,
workerVolumes: workerVolumes,
httpRequestsDuration: httpRequestsDuration,
schedulingFullDuration: schedulingFullDuration,
schedulingLoadingDuration: schedulingLoadingDuration,
pipelineScheduled: pipelineScheduled,
dbQueriesTotal: dbQueriesTotal,
dbConnections: dbConnections,
resourceChecksVec: resourceChecksVec,
workerLastSeen: map[string]time.Time{},
}
go emitter.periodicMetricGC()
return emitter, nil
}
// Emit processes incoming metrics.
// In order to provide idiomatic Prometheus metrics, we'll have to convert the various
// Event types (differentiated by the less-than-ideal string Name field) into different
// Prometheus metrics.
func (emitter *PrometheusEmitter) Emit(logger lager.Logger, event metric.Event) {
//update last seen counters, used to gc stale timeseries
emitter.updateLastSeen(event)
switch event.Name {
case "build started":
emitter.buildsStarted.Inc()
case "build finished":
emitter.buildFinishedMetrics(logger, event)
case "worker containers":
emitter.workerContainersMetric(logger, event)
case "worker volumes":
emitter.workerVolumesMetric(logger, event)
case "http response time":
emitter.httpResponseTimeMetrics(logger, event)
case "scheduling: full duration (ms)":
emitter.schedulingMetrics(logger, event)
case "scheduling: loading versions duration (ms)":
emitter.schedulingMetrics(logger, event)
case "scheduling: job duration (ms)":
emitter.schedulingMetrics(logger, event)
case "database queries":
emitter.databaseMetrics(logger, event)
case "database connections":
emitter.databaseMetrics(logger, event)
case "resource checked":
emitter.resourceMetric(logger, event)
default:
// unless we have a specific metric, we do nothing
}
}
func (emitter *PrometheusEmitter) buildFinishedMetrics(logger lager.Logger, event metric.Event) {
// concourse_builds_finished_total
emitter.buildsFinished.Inc()
// concourse_builds_finished
team, exists := event.Attributes["team_name"]
if !exists {
logger.Error("failed-to-find-team-name-in-event", fmt.Errorf("expected team_name to exist in event.Attributes"))
return
}
pipeline, exists := event.Attributes["pipeline"]
if !exists {
logger.Error("failed-to-find-pipeline-in-event", fmt.Errorf("expected pipeline to exist in event.Attributes"))
return
}
buildStatus, exists := event.Attributes["build_status"]
if !exists {
logger.Error("failed-to-find-build_status-in-event", fmt.Errorf("expected build_status to exist in event.Attributes"))
return
}
emitter.buildsFinishedVec.WithLabelValues(team, pipeline, buildStatus).Inc()
// concourse_builds_(aborted|succeeded|failed|errored)_total
switch buildStatus {
case string(db.BuildStatusAborted):
// concourse_builds_aborted_total
emitter.buildsAborted.Inc()
case string(db.BuildStatusSucceeded):
// concourse_builds_succeeded_total
emitter.buildsSucceeded.Inc()
case string(db.BuildStatusFailed):
// concourse_builds_failed_total
emitter.buildsFailed.Inc()
case string(db.BuildStatusErrored):
// concourse_builds_errored_total
emitter.buildsErrored.Inc()
}
// concourse_builds_duration_seconds
duration, ok := event.Value.(float64)
if !ok {
logger.Error("build-finished-event-value-type-mismatch", fmt.Errorf("expected event.Value to be a float64"))
return
}
// seconds are the standard prometheus base unit for time
duration = duration / 1000
emitter.buildDurationsVec.WithLabelValues(team, pipeline).Observe(duration)
}
func (emitter *PrometheusEmitter) workerContainersMetric(logger lager.Logger, event metric.Event) {
worker, exists := event.Attributes["worker"]
if !exists {
logger.Error("failed-to-find-worker-in-event", fmt.Errorf("expected worker to exist in event.Attributes"))
return
}
platform, exists := event.Attributes["platform"]
if !exists || platform == "" {
logger.Error("failed-to-find-platform-in-event", fmt.Errorf("expected platform to exist in event.Attributes"))
return
}
containers, ok := event.Value.(int)
if !ok {
logger.Error("worker-volumes-event-value-type-mismatch", fmt.Errorf("expected event.Value to be an int"))
return
}
emitter.workerContainers.WithLabelValues(worker, platform).Set(float64(containers))
}
func (emitter *PrometheusEmitter) workerVolumesMetric(logger lager.Logger, event metric.Event) {
worker, exists := event.Attributes["worker"]
if !exists {
logger.Error("failed-to-find-worker-in-event", fmt.Errorf("expected worker to exist in event.Attributes"))
return
}
platform, exists := event.Attributes["platform"]
if !exists || platform == "" {
logger.Error("failed-to-find-platform-in-event", fmt.Errorf("expected platform to exist in event.Attributes"))
return
}
volumes, ok := event.Value.(int)
if !ok {
logger.Error("worker-volumes-event-value-type-mismatch", fmt.Errorf("expected event.Value to be an int"))
return
}
emitter.workerVolumes.WithLabelValues(worker, platform).Set(float64(volumes))
}
func (emitter *PrometheusEmitter) httpResponseTimeMetrics(logger lager.Logger, event metric.Event) {
route, exists := event.Attributes["route"]
if !exists {
logger.Error("failed-to-find-route-in-event", fmt.Errorf("expected method to exist in event.Attributes"))
return
}
method, exists := event.Attributes["method"]
if !exists {
logger.Error("failed-to-find-method-in-event", fmt.Errorf("expected method to exist in event.Attributes"))
return
}
responseTime, ok := event.Value.(float64)
if !ok {
logger.Error("http-response-time-event-value-type-mismatch", fmt.Errorf("expected event.Value to be a float64"))
return
}
emitter.httpRequestsDuration.WithLabelValues(method, route).Observe(responseTime / 1000)
}
func (emitter *PrometheusEmitter) schedulingMetrics(logger lager.Logger, event metric.Event) {
pipeline, exists := event.Attributes["pipeline"]
if !exists {
logger.Error("failed-to-find-pipeline-in-event", fmt.Errorf("expected pipeline to exist in event.Attributes"))
return
}
duration, ok := event.Value.(float64)
if !ok {
logger.Error("scheduling-full-duration-value-type-mismatch", fmt.Errorf("expected event.Value to be a float64"))
return
}
switch event.Name {
case "scheduling: full duration (ms)":
// concourse_scheduling_full_duration_seconds_total
emitter.schedulingFullDuration.WithLabelValues(pipeline).Add(duration / 1000)
// concourse_scheduling_total
emitter.pipelineScheduled.WithLabelValues(pipeline).Inc()
case "scheduling: loading versions duration (ms)":
// concourse_scheduling_loading_duration_seconds_total
emitter.schedulingLoadingDuration.WithLabelValues(pipeline).Add(duration / 1000)
default:
}
}
func (emitter *PrometheusEmitter) databaseMetrics(logger lager.Logger, event metric.Event) {
value, ok := event.Value.(int)
if !ok {
logger.Error("db-value-type-mismatch", fmt.Errorf("expected event.Value to be a int"))
return
}
switch event.Name {
case "database queries":
emitter.dbQueriesTotal.Add(float64(value))
case "database connections":
connectionName, exists := event.Attributes["ConnectionName"]
if !exists {
logger.Error("failed-to-connection-name-in-event", fmt.Errorf("expected ConnectionName to exist in event.Attributes"))
return
}
emitter.dbConnections.WithLabelValues(connectionName).Set(float64(value))
default:
}
}
func (emitter *PrometheusEmitter) resourceMetric(logger lager.Logger, event metric.Event) {
pipeline, exists := event.Attributes["pipeline"]
if !exists {
logger.Error("failed-to-find-pipeline-in-event", fmt.Errorf("expected pipeline to exist in event.Attributes"))
return
}
team, exists := event.Attributes["team"]
if !exists {
logger.Error("failed-to-find-pipeline-in-event", fmt.Errorf("expected pipeline to exist in event.Attributes"))
return
}
emitter.resourceChecksVec.WithLabelValues(pipeline, team).Inc()
}
// updateLastSeen tracks for each worker when it last received a metric event.
func (emitter *PrometheusEmitter) updateLastSeen(event metric.Event) {
emitter.mu.Lock()
defer emitter.mu.Unlock()
if worker, exists := event.Attributes["worker"]; exists {
emitter.workerLastSeen[worker] = time.Now()
}
}
//periodically remove stale metrics for workers
func (emitter *PrometheusEmitter) periodicMetricGC() {
for {
emitter.mu.Lock()
now := time.Now()
for worker, lastSeen := range emitter.workerLastSeen {
if now.Sub(lastSeen) > 5*time.Minute {
// This is a little stupid but we don't know the platform here,
// but DeleteLabelValues requires an exact match on the label set.
// As a workaround we try all known values for the "platform" label
for _, platform := range []string{"linux", "windows", "darwin"} {
emitter.workerContainers.DeleteLabelValues(worker, platform)
emitter.workerVolumes.DeleteLabelValues(worker, platform)
}
delete(emitter.workerLastSeen, worker)
}
}
emitter.mu.Unlock()
time.Sleep(60 * time.Second)
}
}