Skip to content

Commit a6ac91c

Browse files
authored
Merge pull request #1 from gojuno/multi-server-exporter
multi-server-exporter multi server exporter is introduced
2 parents 265a2ea + dcbf7b2 commit a6ac91c

File tree

6 files changed

+376
-213
lines changed

6 files changed

+376
-213
lines changed

README.md

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,16 +48,19 @@ Package vendoring is handled with [`govendor`](https://github.com/kardianos/gove
4848
Path under which to expose metrics. Default is `/metrics`.
4949

5050
* `disable-default-metrics`
51-
Use only metrics supplied from `queries.yaml` via `--extend.query-path`
51+
Use only metrics supplied from `queries.yaml` via `--extend.query-path`.
52+
53+
* `disable-settings-metrics`
54+
Use the flag if you don't want to scrape `pg_settings`.
5255

5356
* `extend.query-path`
5457
Path to a YAML file containing custom queries to run. Check out [`queries.yaml`](queries.yaml)
5558
for examples of the format.
56-
59+
5760
* `dumpmaps`
5861
Do not run - print the internal representation of the metric maps. Useful when debugging a custom
5962
queries file.
60-
63+
6164
* `log.level`
6265
Set logging level: one of `debug`, `info`, `warn`, `error`, `fatal`
6366

@@ -74,21 +77,23 @@ The following environment variables configure the exporter:
7477
URI may contain the username and password to connect with.
7578

7679
* `DATA_SOURCE_URI`
77-
an alternative to DATA_SOURCE_NAME which exclusively accepts the raw URI
80+
an alternative to `DATA_SOURCE_NAME` which exclusively accepts the raw URI
7881
without a username and password component.
7982

8083
* `DATA_SOURCE_USER`
8184
When using `DATA_SOURCE_URI`, this environment variable is used to specify
8285
the username.
86+
8387
* `DATA_SOURCE_USER_FILE`
8488
The same, but reads the username from a file.
8589

8690
* `DATA_SOURCE_PASS`
8791
When using `DATA_SOURCE_URI`, this environment variable is used to specify
8892
the password to connect with.
93+
8994
* `DATA_SOURCE_PASS_FILE`
9095
The same as above but reads the password from a file.
91-
96+
9297
* `PG_EXPORTER_WEB_LISTEN_ADDRESS`
9398
Address to listen on for web interface and telemetry. Default is `:9187`.
9499

@@ -98,10 +103,13 @@ The following environment variables configure the exporter:
98103
* `PG_EXPORTER_DISABLE_DEFAULT_METRICS`
99104
Use only metrics supplied from `queries.yaml`. Value can be `true` or `false`. Default is `false`.
100105

106+
* `PG_EXPORTER_DISABLE_SETTINGS_METRICS`
107+
Use the flag if you don't want to scrape `pg_settings`. Value can be `true` or `false`. Defauls is `false`.
108+
101109
* `PG_EXPORTER_EXTEND_QUERY_PATH`
102110
Path to a YAML file containing custom queries to run. Check out [`queries.yaml`](queries.yaml)
103111
for examples of the format.
104-
112+
105113
Settings set by environment variables starting with `PG_` will be overwritten by the corresponding CLI flag if given.
106114

107115
### Setting the Postgres server's data source name
@@ -113,6 +121,10 @@ For running it locally on a default Debian/Ubuntu install, this will work (trans
113121

114122
sudo -u postgres DATA_SOURCE_NAME="user=postgres host=/var/run/postgresql/ sslmode=disable" postgres_exporter
115123

124+
Also, you can set a list of sources to scrape different instances from the one exporter setup. Just define a string separated by comma.
125+
126+
sudo -u postgres DATA_SOURCE_NAME="port=5432, port=6432" postgres_exporter
127+
116128
See the [github.com/lib/pq](http://github.com/lib/pq) module for other ways to format the connection string.
117129

118130
### Adding new metrics
@@ -136,7 +148,7 @@ The -extend.query-path command-line argument specifies a YAML file containing ad
136148
Some examples are provided in [queries.yaml](queries.yaml).
137149

138150
### Disabling default metrics
139-
To work with non-officially-supported postgres versions you can try disabling (e.g. 8.2.15)
151+
To work with non-officially-supported postgres versions you can try disabling (e.g. 8.2.15)
140152
or a variant of postgres (e.g. Greenplum) you can disable the default metrics with the `--disable-default-metrics`
141153
flag. This removes all built-in metrics, and uses only metrics defined by queries in the `queries.yaml` file you supply
142154
(so you must supply one, otherwise the exporter will return nothing but internal statuses and not your database).

cmd/postgres_exporter/pg_setting.go

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

33
import (
4-
"database/sql"
5-
"errors"
64
"fmt"
75
"math"
86
"strconv"
@@ -13,29 +11,29 @@ import (
1311
)
1412

1513
// Query the pg_settings view containing runtime variables
16-
func querySettings(ch chan<- prometheus.Metric, db *sql.DB) error {
17-
log.Debugln("Querying pg_setting view")
14+
func querySettings(ch chan<- prometheus.Metric, server *Server) error {
15+
log.Debugf("Querying pg_setting view on %q", server)
1816

1917
// pg_settings docs: https://www.postgresql.org/docs/current/static/view-pg-settings.html
2018
//
2119
// NOTE: If you add more vartypes here, you must update the supported
2220
// types in normaliseUnit() below
2321
query := "SELECT name, setting, COALESCE(unit, ''), short_desc, vartype FROM pg_settings WHERE vartype IN ('bool', 'integer', 'real');"
2422

25-
rows, err := db.Query(query)
23+
rows, err := server.db.Query(query)
2624
if err != nil {
27-
return errors.New(fmt.Sprintln("Error running query on database: ", namespace, err))
25+
return fmt.Errorf("Error running query on database %q: %s %v", server, namespace, err)
2826
}
2927
defer rows.Close() // nolint: errcheck
3028

3129
for rows.Next() {
3230
s := &pgSetting{}
3331
err = rows.Scan(&s.name, &s.setting, &s.unit, &s.shortDesc, &s.vartype)
3432
if err != nil {
35-
return errors.New(fmt.Sprintln("Error retrieving rows:", namespace, err))
33+
return fmt.Errorf("Error retrieving rows on %q: %s %v", server, namespace, err)
3634
}
3735

38-
ch <- s.metric()
36+
ch <- s.metric(server.String())
3937
}
4038

4139
return nil
@@ -47,7 +45,7 @@ type pgSetting struct {
4745
name, setting, unit, shortDesc, vartype string
4846
}
4947

50-
func (s *pgSetting) metric() prometheus.Metric {
48+
func (s *pgSetting) metric(server string) prometheus.Metric {
5149
var (
5250
err error
5351
name = strings.Replace(s.name, ".", "_", -1)
@@ -79,7 +77,7 @@ func (s *pgSetting) metric() prometheus.Metric {
7977
}
8078

8179
desc := newDesc(subsystem, name, shortDesc)
82-
return prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, val)
80+
return prometheus.MustNewConstMetric(desc, prometheus.GaugeValue, val, server)
8381
}
8482

8583
// TODO: fix linter override

cmd/postgres_exporter/pg_setting_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ var fixtures = []fixture{
2525
unit: "seconds",
2626
err: "",
2727
},
28-
d: "Desc{fqName: \"pg_settings_seconds_fixture_metric_seconds\", help: \"Foo foo foo [Units converted to seconds.]\", constLabels: {}, variableLabels: []}",
28+
d: `Desc{fqName: "pg_settings_seconds_fixture_metric_seconds", help: "Foo foo foo [Units converted to seconds.]", constLabels: {}, variableLabels: [` + serverLabelName + `]}`,
2929
v: 5,
3030
},
3131
{
@@ -41,7 +41,7 @@ var fixtures = []fixture{
4141
unit: "seconds",
4242
err: "",
4343
},
44-
d: "Desc{fqName: \"pg_settings_milliseconds_fixture_metric_seconds\", help: \"Foo foo foo [Units converted to seconds.]\", constLabels: {}, variableLabels: []}",
44+
d: `Desc{fqName: "pg_settings_milliseconds_fixture_metric_seconds", help: "Foo foo foo [Units converted to seconds.]", constLabels: {}, variableLabels: [` + serverLabelName + `]}`,
4545
v: 5,
4646
},
4747
{
@@ -57,7 +57,7 @@ var fixtures = []fixture{
5757
unit: "bytes",
5858
err: "",
5959
},
60-
d: "Desc{fqName: \"pg_settings_eight_kb_fixture_metric_bytes\", help: \"Foo foo foo [Units converted to bytes.]\", constLabels: {}, variableLabels: []}",
60+
d: `Desc{fqName: "pg_settings_eight_kb_fixture_metric_bytes", help: "Foo foo foo [Units converted to bytes.]", constLabels: {}, variableLabels: [` + serverLabelName + `]}`,
6161
v: 139264,
6262
},
6363
{
@@ -73,7 +73,7 @@ var fixtures = []fixture{
7373
unit: "bytes",
7474
err: "",
7575
},
76-
d: "Desc{fqName: \"pg_settings_16_kb_real_fixture_metric_bytes\", help: \"Foo foo foo [Units converted to bytes.]\", constLabels: {}, variableLabels: []}",
76+
d: `Desc{fqName: "pg_settings_16_kb_real_fixture_metric_bytes", help: "Foo foo foo [Units converted to bytes.]", constLabels: {}, variableLabels: [` + serverLabelName + `]}`,
7777
v: 49152,
7878
},
7979
{
@@ -89,7 +89,7 @@ var fixtures = []fixture{
8989
unit: "bytes",
9090
err: "",
9191
},
92-
d: "Desc{fqName: \"pg_settings_16_mb_real_fixture_metric_bytes\", help: \"Foo foo foo [Units converted to bytes.]\", constLabels: {}, variableLabels: []}",
92+
d: `Desc{fqName: "pg_settings_16_mb_real_fixture_metric_bytes", help: "Foo foo foo [Units converted to bytes.]", constLabels: {}, variableLabels: [` + serverLabelName + `]}`,
9393
v: 5.0331648e+07,
9494
},
9595
{
@@ -105,7 +105,7 @@ var fixtures = []fixture{
105105
unit: "bytes",
106106
err: "",
107107
},
108-
d: "Desc{fqName: \"pg_settings_32_mb_real_fixture_metric_bytes\", help: \"Foo foo foo [Units converted to bytes.]\", constLabels: {}, variableLabels: []}",
108+
d: `Desc{fqName: "pg_settings_32_mb_real_fixture_metric_bytes", help: "Foo foo foo [Units converted to bytes.]", constLabels: {}, variableLabels: [` + serverLabelName + `]}`,
109109
v: 1.00663296e+08,
110110
},
111111
{
@@ -121,7 +121,7 @@ var fixtures = []fixture{
121121
unit: "bytes",
122122
err: "",
123123
},
124-
d: "Desc{fqName: \"pg_settings_64_mb_real_fixture_metric_bytes\", help: \"Foo foo foo [Units converted to bytes.]\", constLabels: {}, variableLabels: []}",
124+
d: `Desc{fqName: "pg_settings_64_mb_real_fixture_metric_bytes", help: "Foo foo foo [Units converted to bytes.]", constLabels: {}, variableLabels: [` + serverLabelName + `]}`,
125125
v: 2.01326592e+08,
126126
},
127127
{
@@ -137,7 +137,7 @@ var fixtures = []fixture{
137137
unit: "",
138138
err: "",
139139
},
140-
d: "Desc{fqName: \"pg_settings_bool_on_fixture_metric\", help: \"Foo foo foo\", constLabels: {}, variableLabels: []}",
140+
d: `Desc{fqName: "pg_settings_bool_on_fixture_metric", help: "Foo foo foo", constLabels: {}, variableLabels: [` + serverLabelName + `]}`,
141141
v: 1,
142142
},
143143
{
@@ -153,7 +153,7 @@ var fixtures = []fixture{
153153
unit: "",
154154
err: "",
155155
},
156-
d: "Desc{fqName: \"pg_settings_bool_off_fixture_metric\", help: \"Foo foo foo\", constLabels: {}, variableLabels: []}",
156+
d: `Desc{fqName: "pg_settings_bool_off_fixture_metric", help: "Foo foo foo", constLabels: {}, variableLabels: [` + serverLabelName + `]}`,
157157
v: 0,
158158
},
159159
{
@@ -169,7 +169,7 @@ var fixtures = []fixture{
169169
unit: "seconds",
170170
err: "",
171171
},
172-
d: "Desc{fqName: \"pg_settings_special_minus_one_value_seconds\", help: \"foo foo foo [Units converted to seconds.]\", constLabels: {}, variableLabels: []}",
172+
d: `Desc{fqName: "pg_settings_special_minus_one_value_seconds", help: "foo foo foo [Units converted to seconds.]", constLabels: {}, variableLabels: [` + serverLabelName + `]}`,
173173
v: -1,
174174
},
175175
{
@@ -185,7 +185,7 @@ var fixtures = []fixture{
185185
unit: "",
186186
err: "",
187187
},
188-
d: "Desc{fqName: \"pg_settings_rds_rds_superuser_reserved_connections\", help: \"Sets the number of connection slots reserved for rds_superusers.\", constLabels: {}, variableLabels: []}",
188+
d: `Desc{fqName: "pg_settings_rds_rds_superuser_reserved_connections", help: "Sets the number of connection slots reserved for rds_superusers.", constLabels: {}, variableLabels: [` + serverLabelName + `]}`,
189189
v: 2,
190190
},
191191
{
@@ -233,7 +233,7 @@ func (s *PgSettingSuite) TestMetric(c *C) {
233233

234234
for _, f := range fixtures {
235235
d := &dto.Metric{}
236-
m := f.p.metric()
236+
m := f.p.metric("")
237237
m.Write(d) // nolint: errcheck
238238

239239
c.Check(m.Desc().String(), Equals, f.d)

0 commit comments

Comments
 (0)