Skip to content

Commit 2297ff8

Browse files
authored
Merge pull request prometheus-community#802 from prometheus-community/superq/descmap
Refactor collector descriptors
2 parents fb316ad + 425c493 commit 2297ff8

File tree

3 files changed

+52
-54
lines changed

3 files changed

+52
-54
lines changed

collector/pg_database.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,11 @@ func NewPGDatabaseCollector(config collectorConfig) (Collector, error) {
4141
}, nil
4242
}
4343

44-
var pgDatabase = map[string]*prometheus.Desc{
45-
"size_bytes": prometheus.NewDesc(
46-
"pg_database_size_bytes",
47-
"Disk space used by the database",
48-
[]string{"datname"}, nil,
49-
),
50-
}
44+
var pgDatabaseSizeDesc = prometheus.NewDesc(
45+
"pg_database_size_bytes",
46+
"Disk space used by the database",
47+
[]string{"datname"}, nil,
48+
)
5149

5250
// Update implements Collector and exposes database size.
5351
// 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
9694
}
9795

9896
ch <- prometheus.MustNewConstMetric(
99-
pgDatabase["size_bytes"],
97+
pgDatabaseSizeDesc,
10098
prometheus.GaugeValue, float64(size), datname,
10199
)
102100
}

collector/pg_stat_bgwriter.go

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -34,74 +34,74 @@ func NewPGStatBGWriterCollector(collectorConfig) (Collector, error) {
3434

3535
const bgWriterSubsystem = "stat_bgwriter"
3636

37-
var statBGWriter = map[string]*prometheus.Desc{
38-
"checkpoints_timed": prometheus.NewDesc(
37+
var (
38+
statBGWriterCheckpointsTimedDesc = prometheus.NewDesc(
3939
prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoints_timed_total"),
4040
"Number of scheduled checkpoints that have been performed",
4141
[]string{},
4242
prometheus.Labels{},
43-
),
44-
"checkpoints_req": prometheus.NewDesc(
43+
)
44+
statBGWriterCheckpointsReqDesc = prometheus.NewDesc(
4545
prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoints_req_total"),
4646
"Number of requested checkpoints that have been performed",
4747
[]string{},
4848
prometheus.Labels{},
49-
),
50-
"checkpoint_write_time": prometheus.NewDesc(
49+
)
50+
statBGWriterCheckpointsReqTimeDesc = prometheus.NewDesc(
5151
prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoint_write_time_total"),
5252
"Total amount of time that has been spent in the portion of checkpoint processing where files are written to disk, in milliseconds",
5353
[]string{},
5454
prometheus.Labels{},
55-
),
56-
"checkpoint_sync_time": prometheus.NewDesc(
55+
)
56+
statBGWriterCheckpointsSyncTimeDesc = prometheus.NewDesc(
5757
prometheus.BuildFQName(namespace, bgWriterSubsystem, "checkpoint_sync_time_total"),
5858
"Total amount of time that has been spent in the portion of checkpoint processing where files are synchronized to disk, in milliseconds",
5959
[]string{},
6060
prometheus.Labels{},
61-
),
62-
"buffers_checkpoint": prometheus.NewDesc(
61+
)
62+
statBGWriterBuffersCheckpointDesc = prometheus.NewDesc(
6363
prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_checkpoint_total"),
6464
"Number of buffers written during checkpoints",
6565
[]string{},
6666
prometheus.Labels{},
67-
),
68-
"buffers_clean": prometheus.NewDesc(
67+
)
68+
statBGWriterBuffersCleanDesc = prometheus.NewDesc(
6969
prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_clean_total"),
7070
"Number of buffers written by the background writer",
7171
[]string{},
7272
prometheus.Labels{},
73-
),
74-
"maxwritten_clean": prometheus.NewDesc(
73+
)
74+
statBGWriterMaxwrittenCleanDesc = prometheus.NewDesc(
7575
prometheus.BuildFQName(namespace, bgWriterSubsystem, "maxwritten_clean_total"),
7676
"Number of times the background writer stopped a cleaning scan because it had written too many buffers",
7777
[]string{},
7878
prometheus.Labels{},
79-
),
80-
"buffers_backend": prometheus.NewDesc(
79+
)
80+
statBGWriterBuffersBackendDesc = prometheus.NewDesc(
8181
prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_backend_total"),
8282
"Number of buffers written directly by a backend",
8383
[]string{},
8484
prometheus.Labels{},
85-
),
86-
"buffers_backend_fsync": prometheus.NewDesc(
85+
)
86+
statBGWriterBuffersBackendFsyncDesc = prometheus.NewDesc(
8787
prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_backend_fsync_total"),
8888
"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)",
8989
[]string{},
9090
prometheus.Labels{},
91-
),
92-
"buffers_alloc": prometheus.NewDesc(
91+
)
92+
statBGWriterBuffersAllocDesc = prometheus.NewDesc(
9393
prometheus.BuildFQName(namespace, bgWriterSubsystem, "buffers_alloc_total"),
9494
"Number of buffers allocated",
9595
[]string{},
9696
prometheus.Labels{},
97-
),
98-
"stats_reset": prometheus.NewDesc(
97+
)
98+
statBGWriterStatsResetDesc = prometheus.NewDesc(
9999
prometheus.BuildFQName(namespace, bgWriterSubsystem, "stats_reset_total"),
100100
"Time at which these statistics were last reset",
101101
[]string{},
102102
prometheus.Labels{},
103-
),
104-
}
103+
)
104+
)
105105

106106
func (PGStatBGWriterCollector) Update(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
107107
row := db.QueryRowContext(ctx,
@@ -137,57 +137,57 @@ func (PGStatBGWriterCollector) Update(ctx context.Context, db *sql.DB, ch chan<-
137137
}
138138

139139
ch <- prometheus.MustNewConstMetric(
140-
statBGWriter["checkpoints_timed"],
140+
statBGWriterCheckpointsTimedDesc,
141141
prometheus.CounterValue,
142142
float64(cpt),
143143
)
144144
ch <- prometheus.MustNewConstMetric(
145-
statBGWriter["checkpoints_req"],
145+
statBGWriterCheckpointsReqDesc,
146146
prometheus.CounterValue,
147147
float64(cpr),
148148
)
149149
ch <- prometheus.MustNewConstMetric(
150-
statBGWriter["checkpoint_write_time"],
150+
statBGWriterCheckpointsReqTimeDesc,
151151
prometheus.CounterValue,
152152
float64(cpwt),
153153
)
154154
ch <- prometheus.MustNewConstMetric(
155-
statBGWriter["checkpoint_sync_time"],
155+
statBGWriterCheckpointsSyncTimeDesc,
156156
prometheus.CounterValue,
157157
float64(cpst),
158158
)
159159
ch <- prometheus.MustNewConstMetric(
160-
statBGWriter["buffers_checkpoint"],
160+
statBGWriterBuffersCheckpointDesc,
161161
prometheus.CounterValue,
162162
float64(bcp),
163163
)
164164
ch <- prometheus.MustNewConstMetric(
165-
statBGWriter["buffers_clean"],
165+
statBGWriterBuffersCleanDesc,
166166
prometheus.CounterValue,
167167
float64(bc),
168168
)
169169
ch <- prometheus.MustNewConstMetric(
170-
statBGWriter["maxwritten_clean"],
170+
statBGWriterMaxwrittenCleanDesc,
171171
prometheus.CounterValue,
172172
float64(mwc),
173173
)
174174
ch <- prometheus.MustNewConstMetric(
175-
statBGWriter["buffers_backend"],
175+
statBGWriterBuffersBackendDesc,
176176
prometheus.CounterValue,
177177
float64(bb),
178178
)
179179
ch <- prometheus.MustNewConstMetric(
180-
statBGWriter["buffers_backend_fsync"],
180+
statBGWriterBuffersBackendFsyncDesc,
181181
prometheus.CounterValue,
182182
float64(bbf),
183183
)
184184
ch <- prometheus.MustNewConstMetric(
185-
statBGWriter["buffers_alloc"],
185+
statBGWriterBuffersAllocDesc,
186186
prometheus.CounterValue,
187187
float64(ba),
188188
)
189189
ch <- prometheus.MustNewConstMetric(
190-
statBGWriter["stats_reset"],
190+
statBGWriterStatsResetDesc,
191191
prometheus.CounterValue,
192192
float64(sr.Unix()),
193193
)

collector/replication_slots.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,23 @@ func NewPGReplicationSlotCollector(config collectorConfig) (Collector, error) {
3333
return &PGReplicationSlotCollector{log: config.logger}, nil
3434
}
3535

36-
var pgReplicationSlot = map[string]*prometheus.Desc{
37-
"current_wal_lsn": prometheus.NewDesc(
36+
var (
37+
pgReplicationSlotCurrentWalDesc = prometheus.NewDesc(
3838
"pg_replication_slot_current_wal_lsn",
3939
"current wal lsn value",
4040
[]string{"slot_name"}, nil,
41-
),
42-
"confirmed_flush_lsn": prometheus.NewDesc(
41+
)
42+
pgReplicationSlotCurrentFlushDesc = prometheus.NewDesc(
4343
"pg_replication_slot_confirmed_flush_lsn",
4444
"last lsn confirmed flushed to the replication slot",
4545
[]string{"slot_name"}, nil,
46-
),
47-
"is_active": prometheus.NewDesc(
46+
)
47+
pgReplicationSlotIsActiveDesc = prometheus.NewDesc(
4848
"pg_replication_slot_is_active",
4949
"last lsn confirmed flushed to the replication slot",
5050
[]string{"slot_name"}, nil,
51-
),
52-
}
51+
)
52+
)
5353

5454
func (PGReplicationSlotCollector) Update(ctx context.Context, db *sql.DB, ch chan<- prometheus.Metric) error {
5555
rows, err := db.QueryContext(ctx,
@@ -75,17 +75,17 @@ func (PGReplicationSlotCollector) Update(ctx context.Context, db *sql.DB, ch cha
7575
}
7676

7777
ch <- prometheus.MustNewConstMetric(
78-
pgReplicationSlot["current_wal_lsn"],
78+
pgReplicationSlotCurrentWalDesc,
7979
prometheus.GaugeValue, float64(wal_lsn), slot_name,
8080
)
8181
if is_active {
8282
ch <- prometheus.MustNewConstMetric(
83-
pgReplicationSlot["confirmed_flush_lsn"],
83+
pgReplicationSlotCurrentFlushDesc,
8484
prometheus.GaugeValue, float64(flush_lsn), slot_name,
8585
)
8686
}
8787
ch <- prometheus.MustNewConstMetric(
88-
pgReplicationSlot["is_active"],
88+
pgReplicationSlotIsActiveDesc,
8989
prometheus.GaugeValue, float64(flush_lsn), slot_name,
9090
)
9191
}

0 commit comments

Comments
 (0)