@@ -17,6 +17,8 @@ import (
17
17
"context"
18
18
"database/sql"
19
19
20
+ "github.com/go-kit/log"
21
+ "github.com/go-kit/log/level"
20
22
"github.com/prometheus/client_golang/prometheus"
21
23
)
22
24
@@ -26,10 +28,12 @@ func init() {
26
28
registerCollector (statDatabaseSubsystem , defaultEnabled , NewPGStatDatabaseCollector )
27
29
}
28
30
29
- type PGStatDatabaseCollector struct {}
31
+ type PGStatDatabaseCollector struct {
32
+ log log.Logger
33
+ }
30
34
31
35
func NewPGStatDatabaseCollector (config collectorConfig ) (Collector , error ) {
32
- return & PGStatDatabaseCollector {}, nil
36
+ return & PGStatDatabaseCollector {log : config . logger }, nil
33
37
}
34
38
35
39
var (
@@ -228,7 +232,7 @@ var (
228
232
`
229
233
)
230
234
231
- func (PGStatDatabaseCollector ) Update (ctx context.Context , instance * instance , ch chan <- prometheus.Metric ) error {
235
+ func (c * PGStatDatabaseCollector ) Update (ctx context.Context , instance * instance , ch chan <- prometheus.Metric ) error {
232
236
db := instance .getDB ()
233
237
rows , err := db .QueryContext (ctx ,
234
238
statDatabaseQuery ,
@@ -267,217 +271,203 @@ func (PGStatDatabaseCollector) Update(ctx context.Context, instance *instance, c
267
271
if err != nil {
268
272
return err
269
273
}
270
- datidLabel := "unknown"
271
- if datid .Valid {
272
- datidLabel = datid .String
274
+
275
+ if ! datid .Valid {
276
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no datid" )
277
+ continue
273
278
}
274
- datnameLabel := "unknown"
275
- if datname . Valid {
276
- datnameLabel = datname . String
279
+ if ! datname . Valid {
280
+ level . Debug ( c . log ). Log ( "msg" , "Skipping collecting metric because it has no datname" )
281
+ continue
277
282
}
278
-
279
- numBackendsMetric := 0.0
280
- if numBackends .Valid {
281
- numBackendsMetric = numBackends .Float64
283
+ if ! numBackends .Valid {
284
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no numbackends" )
285
+ continue
286
+ }
287
+ if ! xactCommit .Valid {
288
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no xact_commit" )
289
+ continue
290
+ }
291
+ if ! xactRollback .Valid {
292
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no xact_rollback" )
293
+ continue
294
+ }
295
+ if ! blksRead .Valid {
296
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no blks_read" )
297
+ continue
298
+ }
299
+ if ! blksHit .Valid {
300
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no blks_hit" )
301
+ continue
302
+ }
303
+ if ! tupReturned .Valid {
304
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no tup_returned" )
305
+ continue
306
+ }
307
+ if ! tupFetched .Valid {
308
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no tup_fetched" )
309
+ continue
310
+ }
311
+ if ! tupInserted .Valid {
312
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no tup_inserted" )
313
+ continue
314
+ }
315
+ if ! tupUpdated .Valid {
316
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no tup_updated" )
317
+ continue
318
+ }
319
+ if ! tupDeleted .Valid {
320
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no tup_deleted" )
321
+ continue
322
+ }
323
+ if ! conflicts .Valid {
324
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no conflicts" )
325
+ continue
326
+ }
327
+ if ! tempFiles .Valid {
328
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no temp_files" )
329
+ continue
330
+ }
331
+ if ! tempBytes .Valid {
332
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no temp_bytes" )
333
+ continue
334
+ }
335
+ if ! deadlocks .Valid {
336
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no deadlocks" )
337
+ continue
338
+ }
339
+ if ! blkReadTime .Valid {
340
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no blk_read_time" )
341
+ continue
342
+ }
343
+ if ! blkWriteTime .Valid {
344
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no blk_write_time" )
345
+ continue
346
+ }
347
+ if ! statsReset .Valid {
348
+ level .Debug (c .log ).Log ("msg" , "Skipping collecting metric because it has no stats_reset" )
349
+ continue
282
350
}
351
+
352
+ labels := []string {datid .String , datname .String }
353
+
283
354
ch <- prometheus .MustNewConstMetric (
284
355
statDatabaseNumbackends ,
285
356
prometheus .GaugeValue ,
286
- numBackendsMetric ,
287
- datidLabel ,
288
- datnameLabel ,
357
+ numBackends .Float64 ,
358
+ labels ... ,
289
359
)
290
360
291
- xactCommitMetric := 0.0
292
- if xactCommit .Valid {
293
- xactCommitMetric = xactCommit .Float64
294
- }
295
361
ch <- prometheus .MustNewConstMetric (
296
362
statDatabaseXactCommit ,
297
363
prometheus .CounterValue ,
298
- xactCommitMetric ,
299
- datidLabel ,
300
- datnameLabel ,
364
+ xactCommit .Float64 ,
365
+ labels ... ,
301
366
)
302
367
303
- xactRollbackMetric := 0.0
304
- if xactRollback .Valid {
305
- xactRollbackMetric = xactRollback .Float64
306
- }
307
368
ch <- prometheus .MustNewConstMetric (
308
369
statDatabaseXactRollback ,
309
370
prometheus .CounterValue ,
310
- xactRollbackMetric ,
311
- datidLabel ,
312
- datnameLabel ,
371
+ xactRollback .Float64 ,
372
+ labels ... ,
313
373
)
314
374
315
- blksReadMetric := 0.0
316
- if blksRead .Valid {
317
- blksReadMetric = blksRead .Float64
318
- }
319
375
ch <- prometheus .MustNewConstMetric (
320
376
statDatabaseBlksRead ,
321
377
prometheus .CounterValue ,
322
- blksReadMetric ,
323
- datidLabel ,
324
- datnameLabel ,
378
+ blksRead .Float64 ,
379
+ labels ... ,
325
380
)
326
381
327
- blksHitMetric := 0.0
328
- if blksHit .Valid {
329
- blksHitMetric = blksHit .Float64
330
- }
331
382
ch <- prometheus .MustNewConstMetric (
332
383
statDatabaseBlksHit ,
333
384
prometheus .CounterValue ,
334
- blksHitMetric ,
335
- datidLabel ,
336
- datnameLabel ,
385
+ blksHit .Float64 ,
386
+ labels ... ,
337
387
)
338
388
339
- tupReturnedMetric := 0.0
340
- if tupReturned .Valid {
341
- tupReturnedMetric = tupReturned .Float64
342
- }
343
389
ch <- prometheus .MustNewConstMetric (
344
390
statDatabaseTupReturned ,
345
391
prometheus .CounterValue ,
346
- tupReturnedMetric ,
347
- datidLabel ,
348
- datnameLabel ,
392
+ tupReturned .Float64 ,
393
+ labels ... ,
349
394
)
350
395
351
- tupFetchedMetric := 0.0
352
- if tupFetched .Valid {
353
- tupFetchedMetric = tupFetched .Float64
354
- }
355
396
ch <- prometheus .MustNewConstMetric (
356
397
statDatabaseTupFetched ,
357
398
prometheus .CounterValue ,
358
- tupFetchedMetric ,
359
- datidLabel ,
360
- datnameLabel ,
399
+ tupFetched .Float64 ,
400
+ labels ... ,
361
401
)
362
402
363
- tupInsertedMetric := 0.0
364
- if tupInserted .Valid {
365
- tupInsertedMetric = tupInserted .Float64
366
- }
367
403
ch <- prometheus .MustNewConstMetric (
368
404
statDatabaseTupInserted ,
369
405
prometheus .CounterValue ,
370
- tupInsertedMetric ,
371
- datidLabel ,
372
- datnameLabel ,
406
+ tupInserted .Float64 ,
407
+ labels ... ,
373
408
)
374
409
375
- tupUpdatedMetric := 0.0
376
- if tupUpdated .Valid {
377
- tupUpdatedMetric = tupUpdated .Float64
378
- }
379
410
ch <- prometheus .MustNewConstMetric (
380
411
statDatabaseTupUpdated ,
381
412
prometheus .CounterValue ,
382
- tupUpdatedMetric ,
383
- datidLabel ,
384
- datnameLabel ,
413
+ tupUpdated .Float64 ,
414
+ labels ... ,
385
415
)
386
416
387
- tupDeletedMetric := 0.0
388
- if tupDeleted .Valid {
389
- tupDeletedMetric = tupDeleted .Float64
390
- }
391
417
ch <- prometheus .MustNewConstMetric (
392
418
statDatabaseTupDeleted ,
393
419
prometheus .CounterValue ,
394
- tupDeletedMetric ,
395
- datidLabel ,
396
- datnameLabel ,
420
+ tupDeleted .Float64 ,
421
+ labels ... ,
397
422
)
398
423
399
- conflictsMetric := 0.0
400
- if conflicts .Valid {
401
- conflictsMetric = conflicts .Float64
402
- }
403
424
ch <- prometheus .MustNewConstMetric (
404
425
statDatabaseConflicts ,
405
426
prometheus .CounterValue ,
406
- conflictsMetric ,
407
- datidLabel ,
408
- datnameLabel ,
427
+ conflicts .Float64 ,
428
+ labels ... ,
409
429
)
410
430
411
- tempFilesMetric := 0.0
412
- if tempFiles .Valid {
413
- tempFilesMetric = tempFiles .Float64
414
- }
415
431
ch <- prometheus .MustNewConstMetric (
416
432
statDatabaseTempFiles ,
417
433
prometheus .CounterValue ,
418
- tempFilesMetric ,
419
- datidLabel ,
420
- datnameLabel ,
434
+ tempFiles .Float64 ,
435
+ labels ... ,
421
436
)
422
437
423
- tempBytesMetric := 0.0
424
- if tempBytes .Valid {
425
- tempBytesMetric = tempBytes .Float64
426
- }
427
438
ch <- prometheus .MustNewConstMetric (
428
439
statDatabaseTempBytes ,
429
440
prometheus .CounterValue ,
430
- tempBytesMetric ,
431
- datidLabel ,
432
- datnameLabel ,
441
+ tempBytes .Float64 ,
442
+ labels ... ,
433
443
)
434
444
435
- deadlocksMetric := 0.0
436
- if deadlocks .Valid {
437
- deadlocksMetric = deadlocks .Float64
438
- }
439
445
ch <- prometheus .MustNewConstMetric (
440
446
statDatabaseDeadlocks ,
441
447
prometheus .CounterValue ,
442
- deadlocksMetric ,
443
- datidLabel ,
444
- datnameLabel ,
448
+ deadlocks .Float64 ,
449
+ labels ... ,
445
450
)
446
451
447
- blkReadTimeMetric := 0.0
448
- if blkReadTime .Valid {
449
- blkReadTimeMetric = blkReadTime .Float64
450
- }
451
452
ch <- prometheus .MustNewConstMetric (
452
453
statDatabaseBlkReadTime ,
453
454
prometheus .CounterValue ,
454
- blkReadTimeMetric ,
455
- datidLabel ,
456
- datnameLabel ,
455
+ blkReadTime .Float64 ,
456
+ labels ... ,
457
457
)
458
458
459
- blkWriteTimeMetric := 0.0
460
- if blkWriteTime .Valid {
461
- blkWriteTimeMetric = blkWriteTime .Float64
462
- }
463
459
ch <- prometheus .MustNewConstMetric (
464
460
statDatabaseBlkWriteTime ,
465
461
prometheus .CounterValue ,
466
- blkWriteTimeMetric ,
467
- datidLabel ,
468
- datnameLabel ,
462
+ blkWriteTime .Float64 ,
463
+ labels ... ,
469
464
)
470
465
471
- statsResetMetric := 0.0
472
- if statsReset .Valid {
473
- statsResetMetric = float64 (statsReset .Time .Unix ())
474
- }
475
466
ch <- prometheus .MustNewConstMetric (
476
467
statDatabaseStatsReset ,
477
468
prometheus .CounterValue ,
478
- statsResetMetric ,
479
- datidLabel ,
480
- datnameLabel ,
469
+ float64 (statsReset .Time .Unix ()),
470
+ labels ... ,
481
471
)
482
472
}
483
473
return nil
0 commit comments