@@ -17,6 +17,8 @@ import (
1717"context"
1818"database/sql"
1919
20+ "github.com/go-kit/log"
21+ "github.com/go-kit/log/level"
2022"github.com/prometheus/client_golang/prometheus"
2123)
2224
@@ -26,10 +28,12 @@ func init() {
2628registerCollector (statDatabaseSubsystem , defaultEnabled , NewPGStatDatabaseCollector )
2729}
2830
29- type PGStatDatabaseCollector struct {}
31+ type PGStatDatabaseCollector struct {
32+ log log.Logger
33+ }
3034
3135func NewPGStatDatabaseCollector (config collectorConfig ) (Collector , error ) {
32- return & PGStatDatabaseCollector {}, nil
36+ return & PGStatDatabaseCollector {log : config . logger }, nil
3337}
3438
3539var (
@@ -228,7 +232,7 @@ var (
228232`
229233)
230234
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 {
232236db := instance .getDB ()
233237rows , err := db .QueryContext (ctx ,
234238statDatabaseQuery ,
@@ -267,217 +271,203 @@ func (PGStatDatabaseCollector) Update(ctx context.Context, instance *instance, c
267271if err != nil {
268272return err
269273}
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
273278}
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
277282}
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
282350}
351+
352+ labels := []string {datid .String , datname .String }
353+
283354ch <- prometheus .MustNewConstMetric (
284355statDatabaseNumbackends ,
285356prometheus .GaugeValue ,
286- numBackendsMetric ,
287- datidLabel ,
288- datnameLabel ,
357+ numBackends .Float64 ,
358+ labels ... ,
289359)
290360
291- xactCommitMetric := 0.0
292- if xactCommit .Valid {
293- xactCommitMetric = xactCommit .Float64
294- }
295361ch <- prometheus .MustNewConstMetric (
296362statDatabaseXactCommit ,
297363prometheus .CounterValue ,
298- xactCommitMetric ,
299- datidLabel ,
300- datnameLabel ,
364+ xactCommit .Float64 ,
365+ labels ... ,
301366)
302367
303- xactRollbackMetric := 0.0
304- if xactRollback .Valid {
305- xactRollbackMetric = xactRollback .Float64
306- }
307368ch <- prometheus .MustNewConstMetric (
308369statDatabaseXactRollback ,
309370prometheus .CounterValue ,
310- xactRollbackMetric ,
311- datidLabel ,
312- datnameLabel ,
371+ xactRollback .Float64 ,
372+ labels ... ,
313373)
314374
315- blksReadMetric := 0.0
316- if blksRead .Valid {
317- blksReadMetric = blksRead .Float64
318- }
319375ch <- prometheus .MustNewConstMetric (
320376statDatabaseBlksRead ,
321377prometheus .CounterValue ,
322- blksReadMetric ,
323- datidLabel ,
324- datnameLabel ,
378+ blksRead .Float64 ,
379+ labels ... ,
325380)
326381
327- blksHitMetric := 0.0
328- if blksHit .Valid {
329- blksHitMetric = blksHit .Float64
330- }
331382ch <- prometheus .MustNewConstMetric (
332383statDatabaseBlksHit ,
333384prometheus .CounterValue ,
334- blksHitMetric ,
335- datidLabel ,
336- datnameLabel ,
385+ blksHit .Float64 ,
386+ labels ... ,
337387)
338388
339- tupReturnedMetric := 0.0
340- if tupReturned .Valid {
341- tupReturnedMetric = tupReturned .Float64
342- }
343389ch <- prometheus .MustNewConstMetric (
344390statDatabaseTupReturned ,
345391prometheus .CounterValue ,
346- tupReturnedMetric ,
347- datidLabel ,
348- datnameLabel ,
392+ tupReturned .Float64 ,
393+ labels ... ,
349394)
350395
351- tupFetchedMetric := 0.0
352- if tupFetched .Valid {
353- tupFetchedMetric = tupFetched .Float64
354- }
355396ch <- prometheus .MustNewConstMetric (
356397statDatabaseTupFetched ,
357398prometheus .CounterValue ,
358- tupFetchedMetric ,
359- datidLabel ,
360- datnameLabel ,
399+ tupFetched .Float64 ,
400+ labels ... ,
361401)
362402
363- tupInsertedMetric := 0.0
364- if tupInserted .Valid {
365- tupInsertedMetric = tupInserted .Float64
366- }
367403ch <- prometheus .MustNewConstMetric (
368404statDatabaseTupInserted ,
369405prometheus .CounterValue ,
370- tupInsertedMetric ,
371- datidLabel ,
372- datnameLabel ,
406+ tupInserted .Float64 ,
407+ labels ... ,
373408)
374409
375- tupUpdatedMetric := 0.0
376- if tupUpdated .Valid {
377- tupUpdatedMetric = tupUpdated .Float64
378- }
379410ch <- prometheus .MustNewConstMetric (
380411statDatabaseTupUpdated ,
381412prometheus .CounterValue ,
382- tupUpdatedMetric ,
383- datidLabel ,
384- datnameLabel ,
413+ tupUpdated .Float64 ,
414+ labels ... ,
385415)
386416
387- tupDeletedMetric := 0.0
388- if tupDeleted .Valid {
389- tupDeletedMetric = tupDeleted .Float64
390- }
391417ch <- prometheus .MustNewConstMetric (
392418statDatabaseTupDeleted ,
393419prometheus .CounterValue ,
394- tupDeletedMetric ,
395- datidLabel ,
396- datnameLabel ,
420+ tupDeleted .Float64 ,
421+ labels ... ,
397422)
398423
399- conflictsMetric := 0.0
400- if conflicts .Valid {
401- conflictsMetric = conflicts .Float64
402- }
403424ch <- prometheus .MustNewConstMetric (
404425statDatabaseConflicts ,
405426prometheus .CounterValue ,
406- conflictsMetric ,
407- datidLabel ,
408- datnameLabel ,
427+ conflicts .Float64 ,
428+ labels ... ,
409429)
410430
411- tempFilesMetric := 0.0
412- if tempFiles .Valid {
413- tempFilesMetric = tempFiles .Float64
414- }
415431ch <- prometheus .MustNewConstMetric (
416432statDatabaseTempFiles ,
417433prometheus .CounterValue ,
418- tempFilesMetric ,
419- datidLabel ,
420- datnameLabel ,
434+ tempFiles .Float64 ,
435+ labels ... ,
421436)
422437
423- tempBytesMetric := 0.0
424- if tempBytes .Valid {
425- tempBytesMetric = tempBytes .Float64
426- }
427438ch <- prometheus .MustNewConstMetric (
428439statDatabaseTempBytes ,
429440prometheus .CounterValue ,
430- tempBytesMetric ,
431- datidLabel ,
432- datnameLabel ,
441+ tempBytes .Float64 ,
442+ labels ... ,
433443)
434444
435- deadlocksMetric := 0.0
436- if deadlocks .Valid {
437- deadlocksMetric = deadlocks .Float64
438- }
439445ch <- prometheus .MustNewConstMetric (
440446statDatabaseDeadlocks ,
441447prometheus .CounterValue ,
442- deadlocksMetric ,
443- datidLabel ,
444- datnameLabel ,
448+ deadlocks .Float64 ,
449+ labels ... ,
445450)
446451
447- blkReadTimeMetric := 0.0
448- if blkReadTime .Valid {
449- blkReadTimeMetric = blkReadTime .Float64
450- }
451452ch <- prometheus .MustNewConstMetric (
452453statDatabaseBlkReadTime ,
453454prometheus .CounterValue ,
454- blkReadTimeMetric ,
455- datidLabel ,
456- datnameLabel ,
455+ blkReadTime .Float64 ,
456+ labels ... ,
457457)
458458
459- blkWriteTimeMetric := 0.0
460- if blkWriteTime .Valid {
461- blkWriteTimeMetric = blkWriteTime .Float64
462- }
463459ch <- prometheus .MustNewConstMetric (
464460statDatabaseBlkWriteTime ,
465461prometheus .CounterValue ,
466- blkWriteTimeMetric ,
467- datidLabel ,
468- datnameLabel ,
462+ blkWriteTime .Float64 ,
463+ labels ... ,
469464)
470465
471- statsResetMetric := 0.0
472- if statsReset .Valid {
473- statsResetMetric = float64 (statsReset .Time .Unix ())
474- }
475466ch <- prometheus .MustNewConstMetric (
476467statDatabaseStatsReset ,
477468prometheus .CounterValue ,
478- statsResetMetric ,
479- datidLabel ,
480- datnameLabel ,
469+ float64 (statsReset .Time .Unix ()),
470+ labels ... ,
481471)
482472}
483473return nil
0 commit comments