Skip to content

Commit c3eec62

Browse files
authored
Merge pull request #871 from Sticksman/bugfix/add-logger-stat-database
Add a logger to stat_database collector
2 parents 4aa8cd4 + 12c12cf commit c3eec62

File tree

2 files changed

+225
-186
lines changed

2 files changed

+225
-186
lines changed

collector/pg_stat_database.go

+118-128
Original file line numberDiff line numberDiff line change
@@ -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() {
2628
registerCollector(statDatabaseSubsystem, defaultEnabled, NewPGStatDatabaseCollector)
2729
}
2830

29-
type PGStatDatabaseCollector struct{}
31+
type PGStatDatabaseCollector struct {
32+
log log.Logger
33+
}
3034

3135
func NewPGStatDatabaseCollector(config collectorConfig) (Collector, error) {
32-
return &PGStatDatabaseCollector{}, nil
36+
return &PGStatDatabaseCollector{log: config.logger}, nil
3337
}
3438

3539
var (
@@ -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 {
232236
db := instance.getDB()
233237
rows, err := db.QueryContext(ctx,
234238
statDatabaseQuery,
@@ -267,217 +271,203 @@ func (PGStatDatabaseCollector) Update(ctx context.Context, instance *instance, c
267271
if err != nil {
268272
return 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+
283354
ch <- prometheus.MustNewConstMetric(
284355
statDatabaseNumbackends,
285356
prometheus.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-
}
295361
ch <- prometheus.MustNewConstMetric(
296362
statDatabaseXactCommit,
297363
prometheus.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-
}
307368
ch <- prometheus.MustNewConstMetric(
308369
statDatabaseXactRollback,
309370
prometheus.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-
}
319375
ch <- prometheus.MustNewConstMetric(
320376
statDatabaseBlksRead,
321377
prometheus.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-
}
331382
ch <- prometheus.MustNewConstMetric(
332383
statDatabaseBlksHit,
333384
prometheus.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-
}
343389
ch <- prometheus.MustNewConstMetric(
344390
statDatabaseTupReturned,
345391
prometheus.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-
}
355396
ch <- prometheus.MustNewConstMetric(
356397
statDatabaseTupFetched,
357398
prometheus.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-
}
367403
ch <- prometheus.MustNewConstMetric(
368404
statDatabaseTupInserted,
369405
prometheus.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-
}
379410
ch <- prometheus.MustNewConstMetric(
380411
statDatabaseTupUpdated,
381412
prometheus.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-
}
391417
ch <- prometheus.MustNewConstMetric(
392418
statDatabaseTupDeleted,
393419
prometheus.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-
}
403424
ch <- prometheus.MustNewConstMetric(
404425
statDatabaseConflicts,
405426
prometheus.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-
}
415431
ch <- prometheus.MustNewConstMetric(
416432
statDatabaseTempFiles,
417433
prometheus.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-
}
427438
ch <- prometheus.MustNewConstMetric(
428439
statDatabaseTempBytes,
429440
prometheus.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-
}
439445
ch <- prometheus.MustNewConstMetric(
440446
statDatabaseDeadlocks,
441447
prometheus.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-
}
451452
ch <- prometheus.MustNewConstMetric(
452453
statDatabaseBlkReadTime,
453454
prometheus.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-
}
463459
ch <- prometheus.MustNewConstMetric(
464460
statDatabaseBlkWriteTime,
465461
prometheus.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-
}
475466
ch <- prometheus.MustNewConstMetric(
476467
statDatabaseStatsReset,
477468
prometheus.CounterValue,
478-
statsResetMetric,
479-
datidLabel,
480-
datnameLabel,
469+
float64(statsReset.Time.Unix()),
470+
labels...,
481471
)
482472
}
483473
return nil

0 commit comments

Comments
 (0)