Skip to content

Commit 46b0bd6

Browse files
authored
Merge branch 'master' into master
2 parents 29eee7c + 5f3a711 commit 46b0bd6

19 files changed

+893
-488
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,7 @@
1414
/release
1515
/*.prom
1616
/.metrics.*.*.prom
17+
/.metrics.*.*.prom.unique
18+
/.assets-branch
19+
/.metrics.*.added
20+
/.metrics.*.removed

.travis.yml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ services:
33
- docker
44
language: go
55
go:
6-
- '1.10'
6+
- '1.11'
77
before_install:
88
- go get -v github.com/mattn/goveralls
99
- sudo wget -O /usr/local/bin/p2 https://github.com/wrouesnel/p2cli/releases/download/r4/p2
@@ -12,6 +12,7 @@ before_install:
1212
&& sudo chmod +x /usr/local/bin/docker-compose
1313
- sudo apt-get update && sudo apt-get install postgresql-client-common
1414
script:
15+
- "./gh-assets-clone.sh"
1516
- go run mage.go -v all
1617
- "$HOME/gopath/bin/goveralls -coverprofile=cover.out -service=travis-ci"
1718
- go run mage.go docker
@@ -21,10 +22,17 @@ after_success:
2122
; docker push wrouesnel/postgres_exporter:$TRAVIS_TAG ; fi
2223
- if [ "$TRAVIS_BRANCH" == "master" ]; then docker push wrouesnel/postgres_exporter
2324
; fi
25+
- "./postgres-metrics-get-changes.sh .assets-branch/metriclists"
26+
- if [ "$TRAVIS_BRANCH" == "master" ]; then ./gh-metrics-push.sh ; fi
2427
env:
2528
global:
2629
- DOCKER_USER=wrouesnel
27-
- secure: f0H5HKL/5f/ZZVGZ7puegWZ6eig0TmruihuSEJCx1+Y6yDZn7l8lH+eETP9KAzH27c3CG1F9ytu/3gnnTOafXnDLlCve3fL5sKF3+pNQRwi3IojsODjdfPW+KEbG+1RD7IgkCn+DSRmvvpLr4zGOmZFEM1ZtLL878u4Hsrv/X5pDbKJgG/cXDRJfsu/EcpviO4WM8zOakBY8QihXhGpZiRtpRDC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o7SHMdsM+pcSwrzsM6OENgvxPNBb/DinmMyQKxTCVcVmMo7uIS89RIylvN79E8U6NagdFkiLfa3xEHq8zCzEkHi3bsLRvytgT2X0=
32+
- secure: 1JL8KcgkLueQ2DLL81UMYzIHX3qm1TjcO40QL2ZOfdirFWBshOiTwTXeWj5qZaGBzoVV5ezhyZaBY+t3/pObslm20ERce879hEw+TSnKN30wfBqNyv2r7rfsbKkXauultb8RNu9y/9XS0DCEyGdSTQh9UaCa4z6ulu39hffDddrGQjwW1P2gT3Npu1cDYd1iSO36rrA6yXjaoN8OW8U4znKVjOGnarxxFnXJkiYv2PfIrZA6BpL3d0syJtWDyr1G+B48oK9VK+fBV9K0G0E67fJvqB3ANXN3D41il3S+cs8Ulcd7hF+LWxpMsP2r1/XHYSDw3Iiz0QFKKzoyxNdipvdjAVDxrWylyLnmTBYzXk41kRv88mKVLBQM1dbzsLXYcsE2pgIZxxq9OHGZ5CUJ8t0oz5D9oXMUy4QOMQ36jZdvD048aB7DGp4EF2J7ILIhUZrHHErOlXotnsYvNMvamNwqB5Jg4NC+y5QHxERJ+HK5oPrLy+iCb2kmWatSB6vO5OeX/F7IRiqtZghJRddEeMdQ1a6H0GeV1BF7Hx8j3TPMJ66qSAb0RA1lQQCN4l+/YMEWmQD8amf1O5NY116waf+Co4qkvt3c4QctQOMwu3Ra7uLlp6GG61OmHhPTCGSv/LZp6CVtROLY5IltKv7qBzksjvXkO1SzhJOxi0JkZmg=
33+
branches:
34+
except:
35+
- assets
2836
deploy:
2937
skip_cleanup: true
3038
provider: releases

Dockerfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
1+
FROM debian:7.11-slim
2+
RUN useradd -u 20001 postgres_exporter
3+
14
FROM scratch
25

6+
COPY --from=0 /etc/passwd /etc/passwd
7+
USER postgres_exporter
8+
39
ARG binary
410

511
COPY $binary /postgres_exporter

Makefile

Lines changed: 0 additions & 117 deletions
This file was deleted.

README.md

Lines changed: 80 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
[![Build Status](https://travis-ci.org/wrouesnel/postgres_exporter.svg?branch=master)](https://travis-ci.org/wrouesnel/postgres_exporter)
22
[![Coverage Status](https://coveralls.io/repos/github/wrouesnel/postgres_exporter/badge.svg?branch=master)](https://coveralls.io/github/wrouesnel/postgres_exporter?branch=master)
33
[![Go Report Card](https://goreportcard.com/badge/github.com/wrouesnel/postgres_exporter)](https://goreportcard.com/report/github.com/wrouesnel/postgres_exporter)
4+
[![Docker Pulls](https://img.shields.io/docker/pulls/wrouesnel/postgres_exporter.svg)](https://hub.docker.com/r/wrouesnel/postgres_exporter/tags)
45

56
# PostgreSQL Server Exporter
67

78
Prometheus exporter for PostgreSQL server metrics.
8-
Supported Postgres versions: 9.1 and up.
9+
10+
CI Tested PostgreSQL versions: `9.4`, `9.5`, `9.6`, `10`, `11`
911

1012
## Quick Start
1113
This package is available for Docker:
@@ -24,7 +26,7 @@ The default make file behavior is to build the binary:
2426
```
2527
$ go get github.com/wrouesnel/postgres_exporter
2628
$ cd ${GOPATH-$HOME/go}/src/github.com/wrouesnel/postgres_exporter
27-
$ go run mage.go
29+
$ go run mage.go binary
2830
$ export DATA_SOURCE_NAME="postgresql://login:password@hostname:port/dbname"
2931
$ ./postgres_exporter <flags>
3032
```
@@ -48,16 +50,19 @@ Package vendoring is handled with [`govendor`](https://github.com/kardianos/gove
4850
Path under which to expose metrics. Default is `/metrics`.
4951

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

5358
* `extend.query-path`
5459
Path to a YAML file containing custom queries to run. Check out [`queries.yaml`](queries.yaml)
5560
for examples of the format.
56-
61+
5762
* `dumpmaps`
5863
Do not run - print the internal representation of the metric maps. Useful when debugging a custom
5964
queries file.
60-
65+
6166
* `log.level`
6267
Set logging level: one of `debug`, `info`, `warn`, `error`, `fatal`
6368

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

7984
* `DATA_SOURCE_URI`
80-
an alternative to DATA_SOURCE_NAME which exclusively accepts the raw URI
85+
an alternative to `DATA_SOURCE_NAME` which exclusively accepts the raw URI
8186
without a username and password component.
8287

8388
* `DATA_SOURCE_USER`
8489
When using `DATA_SOURCE_URI`, this environment variable is used to specify
8590
the username.
91+
8692
* `DATA_SOURCE_USER_FILE`
8793
The same, but reads the username from a file.
8894

8995
* `DATA_SOURCE_PASS`
9096
When using `DATA_SOURCE_URI`, this environment variable is used to specify
9197
the password to connect with.
98+
9299
* `DATA_SOURCE_PASS_FILE`
93100
The same as above but reads the password from a file.
94-
101+
95102
* `PG_EXPORTER_WEB_LISTEN_ADDRESS`
96103
Address to listen on for web interface and telemetry. Default is `:9187`.
97104

@@ -101,13 +108,16 @@ The following environment variables configure the exporter:
101108
* `PG_EXPORTER_DISABLE_DEFAULT_METRICS`
102109
Use only metrics supplied from `queries.yaml`. Value can be `true` or `false`. Default is `false`.
103110

111+
* `PG_EXPORTER_DISABLE_SETTINGS_METRICS`
112+
Use the flag if you don't want to scrape `pg_settings`. Value can be `true` or `false`. Defauls is `false`.
113+
104114
* `PG_EXPORTER_EXTEND_QUERY_PATH`
105115
Path to a YAML file containing custom queries to run. Check out [`queries.yaml`](queries.yaml)
106116
for examples of the format.
107117

108-
* `PG_EXPORTER_CONTANT_LABELS`
118+
* `PG_EXPORTER_CONSTANT_LABELS`
109119
Labels to set in all metrics. A list of `label=value` pairs, separated by commas.
110-
120+
111121
Settings set by environment variables starting with `PG_` will be overwritten by the corresponding CLI flag if given.
112122

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

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

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

124138
### Adding new metrics
@@ -142,31 +156,78 @@ The -extend.query-path command-line argument specifies a YAML file containing ad
142156
Some examples are provided in [queries.yaml](queries.yaml).
143157

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

164+
### Automatically discover databases
165+
To scrape metrics from all databases on a database server, the database DSN's can be dynamically discovered via the
166+
`--auto-discover-databases` flag. When true, `SELECT datname FROM pg_database` is run for all configured DSN's. From the
167+
result a new set of DSN's is created for which the metrics are scraped.
168+
150169
### Running as non-superuser
151170

152-
To be able to collect metrics from pg_stat_activity and pg_stat_replication as non-superuser you have to create views as a superuser, and assign permissions separately to those. In PostgreSQL, views run with the permissions of the user that created them so they can act as security barriers.
171+
To be able to collect metrics from `pg_stat_activity` and `pg_stat_replication`
172+
as non-superuser you have to create functions and views as a superuser, and
173+
assign permissions separately to those.
174+
175+
In PostgreSQL, views run with the permissions of the user that created them so
176+
they can act as security barriers. Functions need to be created to share this
177+
data with the non-superuser. Only creating the views will leave out the most
178+
important bits of data.
153179

154180
```sql
155-
CREATE USER postgres_exporter PASSWORD 'password';
181+
-- To use IF statements, hence to be able to check if the user exists before
182+
-- attempting creation, we need to switch to procedural SQL (PL/pgSQL)
183+
-- instead of standard SQL.
184+
-- More: https://www.postgresql.org/docs/9.3/plpgsql-overview.html
185+
-- To preserve compatibility with <9.0, DO blocks are not used; instead,
186+
-- a function is created and dropped.
187+
CREATE OR REPLACE FUNCTION __tmp_create_user() returns void as $$
188+
BEGIN
189+
IF NOT EXISTS (
190+
SELECT -- SELECT list can stay empty for this
191+
FROM pg_catalog.pg_user
192+
WHERE usename = 'postgres_exporter') THEN
193+
CREATE USER postgres_exporter;
194+
END IF;
195+
END;
196+
$$ language plpgsql;
197+
198+
SELECT __tmp_create_user();
199+
DROP FUNCTION __tmp_create_user();
200+
201+
ALTER USER postgres_exporter WITH PASSWORD 'password';
156202
ALTER USER postgres_exporter SET SEARCH_PATH TO postgres_exporter,pg_catalog;
157203

158-
-- If deploying as non-superuser (for example in AWS RDS)
159-
-- GRANT postgres_exporter TO :MASTER_USER;
160-
CREATE SCHEMA postgres_exporter AUTHORIZATION postgres_exporter;
204+
-- If deploying as non-superuser (for example in AWS RDS), uncomment the GRANT
205+
-- line below and replace <MASTER_USER> with your root user.
206+
-- GRANT postgres_exporter TO <MASTER_USER>;
207+
CREATE SCHEMA IF NOT EXISTS postgres_exporter;
208+
GRANT USAGE ON SCHEMA postgres_exporter TO postgres_exporter;
209+
210+
CREATE OR REPLACE FUNCTION get_pg_stat_activity() RETURNS SETOF pg_stat_activity AS
211+
$$ SELECT * FROM pg_catalog.pg_stat_activity; $$
212+
LANGUAGE sql
213+
VOLATILE
214+
SECURITY DEFINER;
161215

162-
CREATE VIEW postgres_exporter.pg_stat_activity
216+
CREATE OR REPLACE VIEW postgres_exporter.pg_stat_activity
163217
AS
164-
SELECT * from pg_catalog.pg_stat_activity;
218+
SELECT * from get_pg_stat_activity();
165219

166220
GRANT SELECT ON postgres_exporter.pg_stat_activity TO postgres_exporter;
167221

168-
CREATE VIEW postgres_exporter.pg_stat_replication AS
169-
SELECT * from pg_catalog.pg_stat_replication;
222+
CREATE OR REPLACE FUNCTION get_pg_stat_replication() RETURNS SETOF pg_stat_replication AS
223+
$$ SELECT * FROM pg_catalog.pg_stat_replication; $$
224+
LANGUAGE sql
225+
VOLATILE
226+
SECURITY DEFINER;
227+
228+
CREATE OR REPLACE VIEW postgres_exporter.pg_stat_replication
229+
AS
230+
SELECT * FROM get_pg_stat_replication();
170231

171232
GRANT SELECT ON postgres_exporter.pg_stat_replication TO postgres_exporter;
172233
```

0 commit comments

Comments
 (0)