Skip to content

Commit fbb8e38

Browse files
committed
chore (gofmt): gofmt applied
1 parent 2463050 commit fbb8e38

File tree

1 file changed

+84
-86
lines changed

1 file changed

+84
-86
lines changed

postgres_exporter.go

Lines changed: 84 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import (
1010
//"regexp"
1111
//"strconv"
1212
//"strings"
13-
"time"
1413
"math"
14+
"time"
1515

1616
_ "github.com/lib/pq"
1717
"github.com/prometheus/client_golang/prometheus"
@@ -27,17 +27,16 @@ var (
2727
"web.telemetry-path", "/metrics",
2828
"Path under which to expose metrics.",
2929
)
30-
authUser = flag.String("auth.user", "", "Username for basic auth.")
31-
authPass = flag.String("auth.pass", "", "Password for basic auth.")
30+
authUser = flag.String("auth.user", "", "Username for basic auth.")
31+
authPass = flag.String("auth.pass", "", "Password for basic auth.")
3232
)
3333

3434
// Metric name parts.
3535
const (
3636
// Namespace for all metrics.
3737
namespace = "pg"
3838
// Subsystems.
39-
exporter = "exporter"
40-
39+
exporter = "exporter"
4140
)
4241

4342
// landingPage contains the HTML served at '/'.
@@ -52,81 +51,80 @@ var landingPage = []byte(`<html>
5251
`)
5352

5453
type ColumnUsage int
54+
5555
const (
56-
DISCARD ColumnUsage = iota // Ignore this column
57-
LABEL ColumnUsage = iota // Use this column as a label
58-
COUNTER ColumnUsage = iota // Use this column as a counter
59-
GAUGE ColumnUsage = iota // Use this column as a gauge
60-
MAPPEDMETRIC ColumnUsage = iota // Use this column with the supplied mapping of text values
56+
DISCARD ColumnUsage = iota // Ignore this column
57+
LABEL ColumnUsage = iota // Use this column as a label
58+
COUNTER ColumnUsage = iota // Use this column as a counter
59+
GAUGE ColumnUsage = iota // Use this column as a gauge
60+
MAPPEDMETRIC ColumnUsage = iota // Use this column with the supplied mapping of text values
6161
)
6262

63-
64-
6563
// User-friendly representation of a prometheus descriptor map
6664
type ColumnMapping struct {
67-
usage ColumnUsage
65+
usage ColumnUsage
6866
description string
69-
mapping map[string]float64 // Optional column mapping for MAPPEDMETRIC
67+
mapping map[string]float64 // Optional column mapping for MAPPEDMETRIC
7068
}
7169

7270
// Groups metric maps under a shared set of labels
7371
type MetricMapNamespace struct {
74-
labels []string // Label names for this namespace
75-
columnMappings map[string]MetricMap // Column mappings in this namespace
72+
labels []string // Label names for this namespace
73+
columnMappings map[string]MetricMap // Column mappings in this namespace
7674
}
7775

7876
// Stores the prometheus metric description which a given column will be mapped
7977
// to by the collector
8078
type MetricMap struct {
81-
discard bool // Should metric be discarded during mapping?
82-
vtype prometheus.ValueType // Prometheus valuetype
83-
desc *prometheus.Desc // Prometheus descriptor
84-
mapping map[string]float64 // If not nil, maps text values to float64s
79+
discard bool // Should metric be discarded during mapping?
80+
vtype prometheus.ValueType // Prometheus valuetype
81+
desc *prometheus.Desc // Prometheus descriptor
82+
mapping map[string]float64 // If not nil, maps text values to float64s
8583
}
8684

8785
// Metric descriptors for dynamically created metrics.
88-
var metricMaps = map[string]map[string]ColumnMapping {
89-
"pgq_queue" : map[string]ColumnMapping {
90-
"name" : { LABEL, "Queue name", nil },
91-
"lag" : { COUNTER, "Queue lag in seconds", nil },
86+
var metricMaps = map[string]map[string]ColumnMapping{
87+
"pgq_queue": map[string]ColumnMapping{
88+
"name": {LABEL, "Queue name", nil},
89+
"lag": {COUNTER, "Queue lag in seconds", nil},
9290
},
93-
"pgq_consumer" : map[string]ColumnMapping {
94-
"name" : { LABEL, "Consumer name", nil },
95-
"lag" : { COUNTER, "Consumer lag in seconds", nil },
91+
"pgq_consumer": map[string]ColumnMapping{
92+
"name": {LABEL, "Consumer name", nil},
93+
"lag": {COUNTER, "Consumer lag in seconds", nil},
9694
},
97-
"pg_stat_bgwriter" : map[string]ColumnMapping {
98-
"checkpoints_timed" : { COUNTER, "Number of scheduled checkpoints that have been performed", nil },
99-
"checkpoints_req" : { COUNTER, "Number of requested checkpoints that have been performed", nil },
100-
"checkpoint_write_time" : { COUNTER, "Total amount of time that has been spent in the portion of checkpoint processing where files are written to disk, in milliseconds", nil },
101-
"checkpoint_sync_time" : { COUNTER, "Total amount of time that has been spent in the portion of checkpoint processing where files are synchronized to disk, in milliseconds", nil },
102-
"buffers_checkpoint" : { COUNTER, "Number of buffers written during checkpoints", nil },
103-
"buffers_clean" : { COUNTER, "Number of buffers written by the background writer", nil },
104-
"maxwritten_clean" : { COUNTER, "Number of times the background writer stopped a cleaning scan because it had written too many buffers", nil },
105-
"buffers_backend" : { COUNTER, "Number of buffers written directly by a backend", nil },
106-
"buffers_backend_fsync" : { COUNTER, "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)", nil },
107-
"buffers_alloc" : { COUNTER, "Number of buffers allocated", nil },
108-
"stats_reset" : { COUNTER, "Time at which these statistics were last reset", nil },
95+
"pg_stat_bgwriter": map[string]ColumnMapping{
96+
"checkpoints_timed": {COUNTER, "Number of scheduled checkpoints that have been performed", nil},
97+
"checkpoints_req": {COUNTER, "Number of requested checkpoints that have been performed", nil},
98+
"checkpoint_write_time": {COUNTER, "Total amount of time that has been spent in the portion of checkpoint processing where files are written to disk, in milliseconds", nil},
99+
"checkpoint_sync_time": {COUNTER, "Total amount of time that has been spent in the portion of checkpoint processing where files are synchronized to disk, in milliseconds", nil},
100+
"buffers_checkpoint": {COUNTER, "Number of buffers written during checkpoints", nil},
101+
"buffers_clean": {COUNTER, "Number of buffers written by the background writer", nil},
102+
"maxwritten_clean": {COUNTER, "Number of times the background writer stopped a cleaning scan because it had written too many buffers", nil},
103+
"buffers_backend": {COUNTER, "Number of buffers written directly by a backend", nil},
104+
"buffers_backend_fsync": {COUNTER, "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)", nil},
105+
"buffers_alloc": {COUNTER, "Number of buffers allocated", nil},
106+
"stats_reset": {COUNTER, "Time at which these statistics were last reset", nil},
109107
},
110-
"pg_stat_database" : map[string]ColumnMapping {
111-
"datid" : { LABEL, "OID of a database", nil },
112-
"datname" : { LABEL, "Name of this database", nil },
113-
"numbackends" : { GAUGE, "Number of backends currently connected to this database. This is the only column in this view that returns a value reflecting current state; all other columns return the accumulated values since the last reset.", nil },
114-
"xact_commit" : { COUNTER, "Number of transactions in this database that have been committed", nil },
115-
"xact_rollback" : { COUNTER, "Number of transactions in this database that have been rolled back", nil },
116-
"blks_read" : { COUNTER, "Number of disk blocks read in this database", nil },
117-
"blks_hit" : { COUNTER, "Number of times disk blocks were found already in the buffer cache, so that a read was not necessary (this only includes hits in the PostgreSQL buffer cache, not the operating system's file system cache)", nil },
118-
"tup_returned" : { COUNTER, "Number of rows returned by queries in this database", nil },
119-
"tup_fetched" : { COUNTER, "Number of rows fetched by queries in this database", nil },
120-
"tup_inserted" : { COUNTER, "Number of rows inserted by queries in this database", nil },
121-
"tup_updated" : { COUNTER, "Number of rows updated by queries in this database", nil },
122-
"tup_deleted" : { COUNTER, "Number of rows deleted by queries in this database", nil },
123-
"conflicts" : { COUNTER, "Number of queries canceled due to conflicts with recovery in this database. (Conflicts occur only on standby servers; see pg_stat_database_conflicts for details.)", nil },
124-
"temp_files" : { COUNTER, "Number of temporary files created by queries in this database. All temporary files are counted, regardless of why the temporary file was created (e.g., sorting or hashing), and regardless of the log_temp_files setting.", nil },
125-
"temp_bytes" : { COUNTER, "Total amount of data written to temporary files by queries in this database. All temporary files are counted, regardless of why the temporary file was created, and regardless of the log_temp_files setting.", nil },
126-
"deadlocks" : { COUNTER, "Number of deadlocks detected in this database", nil },
127-
"blk_read_time" : { COUNTER, "Time spent reading data file blocks by backends in this database, in milliseconds", nil },
128-
"blk_write_time" : { COUNTER, "Time spent writing data file blocks by backends in this database, in milliseconds", nil },
129-
"stats_reset" : { COUNTER, "Time at which these statistics were last reset", nil },
108+
"pg_stat_database": map[string]ColumnMapping{
109+
"datid": {LABEL, "OID of a database", nil},
110+
"datname": {LABEL, "Name of this database", nil},
111+
"numbackends": {GAUGE, "Number of backends currently connected to this database. This is the only column in this view that returns a value reflecting current state; all other columns return the accumulated values since the last reset.", nil},
112+
"xact_commit": {COUNTER, "Number of transactions in this database that have been committed", nil},
113+
"xact_rollback": {COUNTER, "Number of transactions in this database that have been rolled back", nil},
114+
"blks_read": {COUNTER, "Number of disk blocks read in this database", nil},
115+
"blks_hit": {COUNTER, "Number of times disk blocks were found already in the buffer cache, so that a read was not necessary (this only includes hits in the PostgreSQL buffer cache, not the operating system's file system cache)", nil},
116+
"tup_returned": {COUNTER, "Number of rows returned by queries in this database", nil},
117+
"tup_fetched": {COUNTER, "Number of rows fetched by queries in this database", nil},
118+
"tup_inserted": {COUNTER, "Number of rows inserted by queries in this database", nil},
119+
"tup_updated": {COUNTER, "Number of rows updated by queries in this database", nil},
120+
"tup_deleted": {COUNTER, "Number of rows deleted by queries in this database", nil},
121+
"conflicts": {COUNTER, "Number of queries canceled due to conflicts with recovery in this database. (Conflicts occur only on standby servers; see pg_stat_database_conflicts for details.)", nil},
122+
"temp_files": {COUNTER, "Number of temporary files created by queries in this database. All temporary files are counted, regardless of why the temporary file was created (e.g., sorting or hashing), and regardless of the log_temp_files setting.", nil},
123+
"temp_bytes": {COUNTER, "Total amount of data written to temporary files by queries in this database. All temporary files are counted, regardless of why the temporary file was created, and regardless of the log_temp_files setting.", nil},
124+
"deadlocks": {COUNTER, "Number of deadlocks detected in this database", nil},
125+
"blk_read_time": {COUNTER, "Time spent reading data file blocks by backends in this database, in milliseconds", nil},
126+
"blk_write_time": {COUNTER, "Time spent writing data file blocks by backends in this database, in milliseconds", nil},
127+
"stats_reset": {COUNTER, "Time at which these statistics were last reset", nil},
130128
},
131129
}
132130

@@ -147,30 +145,30 @@ func makeDescMap(metricMaps map[string]map[string]ColumnMapping) map[string]Metr
147145

148146
for columnName, columnMapping := range mappings {
149147
switch columnMapping.usage {
150-
case DISCARD, LABEL:
151-
thisMap[columnName] = MetricMap{
152-
discard : true,
153-
}
154-
case COUNTER:
155-
thisMap[columnName] = MetricMap{
156-
vtype : prometheus.CounterValue,
157-
desc : prometheus.NewDesc(fmt.Sprintf("%s_%s", namespace, columnName), columnMapping.description, constLabels, nil),
158-
}
159-
case GAUGE:
160-
thisMap[columnName] = MetricMap{
161-
vtype : prometheus.GaugeValue,
162-
desc : prometheus.NewDesc(fmt.Sprintf("%s_%s", namespace, columnName), columnMapping.description, constLabels, nil),
163-
}
164-
case MAPPEDMETRIC:
165-
thisMap[columnName] = MetricMap{
166-
vtype : prometheus.GaugeValue,
167-
desc : prometheus.NewDesc(fmt.Sprintf("%s_%s", namespace, columnName), columnMapping.description, constLabels, nil),
168-
mapping: columnMapping.mapping,
169-
}
148+
case DISCARD, LABEL:
149+
thisMap[columnName] = MetricMap{
150+
discard: true,
151+
}
152+
case COUNTER:
153+
thisMap[columnName] = MetricMap{
154+
vtype: prometheus.CounterValue,
155+
desc: prometheus.NewDesc(fmt.Sprintf("%s_%s", namespace, columnName), columnMapping.description, constLabels, nil),
156+
}
157+
case GAUGE:
158+
thisMap[columnName] = MetricMap{
159+
vtype: prometheus.GaugeValue,
160+
desc: prometheus.NewDesc(fmt.Sprintf("%s_%s", namespace, columnName), columnMapping.description, constLabels, nil),
161+
}
162+
case MAPPEDMETRIC:
163+
thisMap[columnName] = MetricMap{
164+
vtype: prometheus.GaugeValue,
165+
desc: prometheus.NewDesc(fmt.Sprintf("%s_%s", namespace, columnName), columnMapping.description, constLabels, nil),
166+
mapping: columnMapping.mapping,
167+
}
170168
}
171169
}
172170

173-
metricMap[namespace] = MetricMapNamespace{ constLabels, thisMap }
171+
metricMap[namespace] = MetricMapNamespace{constLabels, thisMap}
174172
}
175173

176174
return metricMap
@@ -219,7 +217,7 @@ type Exporter struct {
219217
dsn string
220218
duration, error prometheus.Gauge
221219
totalScrapes prometheus.Counter
222-
metricMap map[string]MetricMapNamespace
220+
metricMap map[string]MetricMapNamespace
223221
}
224222

225223
// NewExporter returns a new exporter for the provided DSN.
@@ -244,7 +242,7 @@ func NewExporter(dsn string) *Exporter {
244242
Name: "last_scrape_error",
245243
Help: "Whether the last scrape of metrics from PostgreSQL resulted in an error (1 for error, 0 for success).",
246244
}),
247-
metricMap : makeDescMap(metricMaps),
245+
metricMap: makeDescMap(metricMaps),
248246
}
249247
}
250248

@@ -310,10 +308,10 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
310308

311309
for namespace, mapping := range e.metricMap {
312310
log.Debugln("Querying namespace: ", namespace)
313-
func () { // Don't fail on a bad scrape of one metric
311+
func() { // Don't fail on a bad scrape of one metric
314312
rows, err := db.Query(fmt.Sprintf("SELECT * FROM %s;", namespace))
315313
if err != nil {
316-
log.Println("Error running query on database: ", namespace, err)
314+
log.Println("Error running query on database: ", namespace, err)
317315
e.error.Set(1)
318316
return
319317
}
@@ -365,7 +363,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
365363
}
366364

367365
value, ok := dbToFloat64(columnData[idx])
368-
if ! ok {
366+
if !ok {
369367
e.error.Set(1)
370368
log.Errorln("Unexpected error parsing column: ", namespace, columnName, columnData[idx])
371369
continue
@@ -380,7 +378,7 @@ func (e *Exporter) scrape(ch chan<- prometheus.Metric) {
380378
// Its not an error to fail here, since the values are
381379
// unexpected anyway.
382380
value, ok := dbToFloat64(columnData[idx])
383-
if ! ok {
381+
if !ok {
384382
log.Warnln("Unparseable column type - discarding: ", namespace, columnName, err)
385383
continue
386384
}

0 commit comments

Comments
 (0)