Merge pull request #802 from prometheus-community/superq/descmap

Refactor collector descriptors
This commit is contained in:
Ben Kochie 2023-06-01 15:57:44 +02:00 committed by GitHub
commit 2297ff8f0a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 52 additions and 54 deletions

View File

@ -41,13 +41,11 @@ func NewPGDatabaseCollector(config collectorConfig) (Collector, error) {
}, nil }, nil
} }
var pgDatabase = map[string]*prometheus.Desc{ var pgDatabaseSizeDesc = prometheus.NewDesc(
"size_bytes": prometheus.NewDesc( "pg_database_size_bytes",
"pg_database_size_bytes", "Disk space used by the database",
"Disk space used by the database", []string{"datname"}, nil,
[]string{"datname"}, nil, )
),
}
// Update implements Collector and exposes database size. // Update implements Collector and exposes database size.
// It is called by the Prometheus registry when collecting metrics. // It is called by the Prometheus registry when collecting metrics.
@ -96,7 +94,7 @@ func (c PGDatabaseCollector) Update(ctx context.Context, db *sql.DB, ch chan<- p
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
pgDatabase["size_bytes"], pgDatabaseSizeDesc,
prometheus.GaugeValue, float64(size), datname, prometheus.GaugeValue, float64(size), datname,
) )
} }

View File

@ -34,74 +34,74 @@ func NewPGStatBGWriterCollector(collectorConfig) (Collector, error) {
const bgWriterSubsystem = "stat_bgwriter" const bgWriterSubsystem = "stat_bgwriter"
var statBGWriter = map[string]*prometheus.Desc{ var (
"checkpoints_timed": prometheus.NewDesc( statBGWriterCheckpointsTimedDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoints_timed_total"), prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoints_timed_total"),
"Number of scheduled checkpoints that have been performed", "Number of scheduled checkpoints that have been performed",
[]string{}, []string{},
prometheus.Labels{}, prometheus.Labels{},
), )
"checkpoints_req": prometheus.NewDesc( statBGWriterCheckpointsReqDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoints_req_total"), prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoints_req_total"),
"Number of requested checkpoints that have been performed", "Number of requested checkpoints that have been performed",
[]string{}, []string{},
prometheus.Labels{}, prometheus.Labels{},
), )
"checkpoint_write_time": prometheus.NewDesc( statBGWriterCheckpointsReqTimeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoint_write_time_total"), prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoint_write_time_total"),
"Total amount of time that has been spent in the portion of checkpoint processing where files are written to disk, in milliseconds", "Total amount of time that has been spent in the portion of checkpoint processing where files are written to disk, in milliseconds",
[]string{}, []string{},
prometheus.Labels{}, prometheus.Labels{},
), )
"checkpoint_sync_time": prometheus.NewDesc( statBGWriterCheckpointsSyncTimeDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoint_sync_time_total"), prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoint_sync_time_total"),
"Total amount of time that has been spent in the portion of checkpoint processing where files are synchronized to disk, in milliseconds", "Total amount of time that has been spent in the portion of checkpoint processing where files are synchronized to disk, in milliseconds",
[]string{}, []string{},
prometheus.Labels{}, prometheus.Labels{},
), )
"buffers_checkpoint": prometheus.NewDesc( statBGWriterBuffersCheckpointDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_checkpoint_total"), prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_checkpoint_total"),
"Number of buffers written during checkpoints", "Number of buffers written during checkpoints",
[]string{}, []string{},
prometheus.Labels{}, prometheus.Labels{},
), )
"buffers_clean": prometheus.NewDesc( statBGWriterBuffersCleanDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_clean_total"), prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_clean_total"),
"Number of buffers written by the background writer", "Number of buffers written by the background writer",
[]string{}, []string{},
prometheus.Labels{}, prometheus.Labels{},
), )
"maxwritten_clean": prometheus.NewDesc( statBGWriterMaxwrittenCleanDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, bgWriterSubsystem, "maxwritten_clean_total"), prometheus.BuildFQName(namespace, bgWriterSubsystem, "maxwritten_clean_total"),
"Number of times the background writer stopped a cleaning scan because it had written too many buffers", "Number of times the background writer stopped a cleaning scan because it had written too many buffers",
[]string{}, []string{},
prometheus.Labels{}, prometheus.Labels{},
), )
"buffers_backend": prometheus.NewDesc( statBGWriterBuffersBackendDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_backend_total"), prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_backend_total"),
"Number of buffers written directly by a backend", "Number of buffers written directly by a backend",
[]string{}, []string{},
prometheus.Labels{}, prometheus.Labels{},
), )
"buffers_backend_fsync": prometheus.NewDesc( statBGWriterBuffersBackendFsyncDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_backend_fsync_total"), prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_backend_fsync_total"),
"Number of times a backend had to execute its own fsync call (normally the background writer handles those even when the backend does its own write)", "Number of times a backend had to execute its own fsync call (normally the background writer handles those even when the backend does its own write)",
[]string{}, []string{},
prometheus.Labels{}, prometheus.Labels{},
), )
"buffers_alloc": prometheus.NewDesc( statBGWriterBuffersAllocDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_alloc_total"), prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_alloc_total"),
"Number of buffers allocated", "Number of buffers allocated",
[]string{}, []string{},
prometheus.Labels{}, prometheus.Labels{},
), )
"stats_reset": prometheus.NewDesc( statBGWriterStatsResetDesc = prometheus.NewDesc(
prometheus.BuildFQName(namespace, bgWriterSubsystem, "stats_reset_total"), prometheus.BuildFQName(namespace, bgWriterSubsystem, "stats_reset_total"),
"Time at which these statistics were last reset", "Time at which these statistics were last reset",
[]string{}, []string{},
prometheus.Labels{}, prometheus.Labels{},
), )
} )
func (PGStatBGWriterCollector) Update(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error { func (PGStatBGWriterCollector) Update(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
row := db.QueryRowContext(ctx, row := db.QueryRowContext(ctx,
@ -137,57 +137,57 @@ func (PGStatBGWriterCollector) Update(ctx context.Context, db *sql.DB, ch chan<-
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statBGWriter["checkpoints_timed"], statBGWriterCheckpointsTimedDesc,
prometheus.CounterValue, prometheus.CounterValue,
float64(cpt), float64(cpt),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statBGWriter["checkpoints_req"], statBGWriterCheckpointsReqDesc,
prometheus.CounterValue, prometheus.CounterValue,
float64(cpr), float64(cpr),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statBGWriter["checkpoint_write_time"], statBGWriterCheckpointsReqTimeDesc,
prometheus.CounterValue, prometheus.CounterValue,
float64(cpwt), float64(cpwt),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statBGWriter["checkpoint_sync_time"], statBGWriterCheckpointsSyncTimeDesc,
prometheus.CounterValue, prometheus.CounterValue,
float64(cpst), float64(cpst),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statBGWriter["buffers_checkpoint"], statBGWriterBuffersCheckpointDesc,
prometheus.CounterValue, prometheus.CounterValue,
float64(bcp), float64(bcp),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statBGWriter["buffers_clean"], statBGWriterBuffersCleanDesc,
prometheus.CounterValue, prometheus.CounterValue,
float64(bc), float64(bc),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statBGWriter["maxwritten_clean"], statBGWriterMaxwrittenCleanDesc,
prometheus.CounterValue, prometheus.CounterValue,
float64(mwc), float64(mwc),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statBGWriter["buffers_backend"], statBGWriterBuffersBackendDesc,
prometheus.CounterValue, prometheus.CounterValue,
float64(bb), float64(bb),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statBGWriter["buffers_backend_fsync"], statBGWriterBuffersBackendFsyncDesc,
prometheus.CounterValue, prometheus.CounterValue,
float64(bbf), float64(bbf),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statBGWriter["buffers_alloc"], statBGWriterBuffersAllocDesc,
prometheus.CounterValue, prometheus.CounterValue,
float64(ba), float64(ba),
) )
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
statBGWriter["stats_reset"], statBGWriterStatsResetDesc,
prometheus.CounterValue, prometheus.CounterValue,
float64(sr.Unix()), float64(sr.Unix()),
) )

View File

@ -33,23 +33,23 @@ func NewPGReplicationSlotCollector(config collectorConfig) (Collector, error) {
return &PGReplicationSlotCollector{log: config.logger}, nil return &PGReplicationSlotCollector{log: config.logger}, nil
} }
var pgReplicationSlot = map[string]*prometheus.Desc{ var (
"current_wal_lsn": prometheus.NewDesc( pgReplicationSlotCurrentWalDesc = prometheus.NewDesc(
"pg_replication_slot_current_wal_lsn", "pg_replication_slot_current_wal_lsn",
"current wal lsn value", "current wal lsn value",
[]string{"slot_name"}, nil, []string{"slot_name"}, nil,
), )
"confirmed_flush_lsn": prometheus.NewDesc( pgReplicationSlotCurrentFlushDesc = prometheus.NewDesc(
"pg_replication_slot_confirmed_flush_lsn", "pg_replication_slot_confirmed_flush_lsn",
"last lsn confirmed flushed to the replication slot", "last lsn confirmed flushed to the replication slot",
[]string{"slot_name"}, nil, []string{"slot_name"}, nil,
), )
"is_active": prometheus.NewDesc( pgReplicationSlotIsActiveDesc = prometheus.NewDesc(
"pg_replication_slot_is_active", "pg_replication_slot_is_active",
"last lsn confirmed flushed to the replication slot", "last lsn confirmed flushed to the replication slot",
[]string{"slot_name"}, nil, []string{"slot_name"}, nil,
), )
} )
func (PGReplicationSlotCollector) Update(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error { func (PGReplicationSlotCollector) Update(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
rows, err := db.QueryContext(ctx, rows, err := db.QueryContext(ctx,
@ -75,17 +75,17 @@ func (PGReplicationSlotCollector) Update(ctx context.Context, db *sql.DB, ch cha
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
pgReplicationSlot["current_wal_lsn"], pgReplicationSlotCurrentWalDesc,
prometheus.GaugeValue, float64(wal_lsn), slot_name, prometheus.GaugeValue, float64(wal_lsn), slot_name,
) )
if is_active { if is_active {
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
pgReplicationSlot["confirmed_flush_lsn"], pgReplicationSlotCurrentFlushDesc,
prometheus.GaugeValue, float64(flush_lsn), slot_name, prometheus.GaugeValue, float64(flush_lsn), slot_name,
) )
} }
ch <- prometheus.MustNewConstMetric( ch <- prometheus.MustNewConstMetric(
pgReplicationSlot["is_active"], pgReplicationSlotIsActiveDesc,
prometheus.GaugeValue, float64(flush_lsn), slot_name, prometheus.GaugeValue, float64(flush_lsn), slot_name,
) )
} }