Skip to content

Commit 976abab

Browse files
author
Craig Ringer
committed
Document the custom query format
Add docs on the custom query format to the sample query yaml file. Signed-off-by: Craig Ringer <[email protected]>
1 parent 922d2aa commit 976abab

File tree

2 files changed

+137
-1
lines changed

2 files changed

+137
-1
lines changed

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ This will build the docker image as `prometheuscommunity/postgres_exporter:${bra
5454
Use the flag if you don't want to scrape `pg_settings`.
5555

5656
* `auto-discover-databases`
57-
Whether to discover the databases on a server dynamically.
57+
Whether to discover the databases on a server dynamically. If enabled, each query is run once
58+
per discovered database. See [Automatically discover databases](#automatically-discover-databases).
5859

5960
* `extend.query-path`
6061
Path to a YAML file containing custom queries to run. Check out [`queries.yaml`](queries.yaml)
@@ -196,6 +197,15 @@ To scrape metrics from all databases on a database server, the database DSN's ca
196197
`--auto-discover-databases` flag. When true, `SELECT datname FROM pg_database WHERE datallowconn = true AND datistemplate = false and datname != current_database()` is run for all configured DSN's. From the
197198
result a new set of DSN's is created for which the metrics are scraped.
198199

200+
Setting the `master` flag on a metric to `true` disables scraping of each
201+
discovered database for the metric. The metric will be scraped only once for
202+
the default database that `postgres_exporter` connects to.
203+
204+
For any query that is run on all autodiscovered databases it is **strongly
205+
recommended** that each metric has the value of `current_database()` mapped as
206+
a `LABEL` so that it emits distinct dimensions for each database scraped. See
207+
the examples in `queries.yaml` for details.
208+
199209
In addition, the option `--exclude-databases` adds the possibily to filter the result from the auto discovery to discard databases you do not need.
200210

201211
If you want to include only subset of databases, you can use option `--include-databases`. Exporter still makes request to

queries.yaml

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,124 @@
1+
#
2+
# This is a user-defined metrics mapping for postgres_exporter.
3+
#
4+
# It supplements the built-in set of metrics that postgres_exporter scrapes
5+
# with additional user-defined queries.
6+
#
7+
# Each top-level entry in the map defines a set of metrics whose values and
8+
# optionally labels (dimensions) are provided by a single SQL query. Each
9+
# result column of the query either supplies a value for a metric or adds a
10+
# Prometheus label (dimension) to qualify the metric.
11+
#
12+
# The emitted metrics are named {group_name}_{query_result_column_name}.
13+
#
14+
# Each entry in the top-level map must have a map value with keys:
15+
#
16+
# query SQL query string that is executed verbatim on the server
17+
# to capture metrics values and any qualifying LABEL values
18+
# specified by the 'metrics' key. Value required.
19+
#
20+
# Each result column maps to a Prometheus metric or a label
21+
# depending on the configuration in the 'metrics' map.
22+
#
23+
# master Boolean flag controlling whether this query is run only
24+
# on the default database postgres_exporter connects to.
25+
# Only has an effect if auto-discover databases is on. Defaults to
26+
# false.
27+
#
28+
# metrics Map of query result column names to metric type and description.
29+
# Optional. If no entry is present for a given result column name
30+
# the metric type is assumed to be COUNTER and no description
31+
# for the metric is supplied to the scraper.
32+
#
33+
# Each of the 'metrics' entries has a key that exactly matches a result column
34+
# name. Entries that don't match any column are ignored. Use `AS` to alias
35+
# column names explicitly in your queries to ensure they are predictable. The
36+
# value is a map with keys:
37+
#
38+
# usage String 'COUNTER', 'GAUGE', 'DISCARD', 'HISTOGRAM', 'DURATION',
39+
# 'MAPPEDMETRIC' or 'LABEL'. Defines how values in this column
40+
# are mapped to the metrics output.
41+
#
42+
# description Short text description of the metric for display in tools that
43+
# consume Prometheus metrics.
44+
#
45+
# Usage types:
46+
#
47+
# GAUGE Prometheus metric type. A numeric value that may increase or
48+
# decrease between scrapes.
49+
#
50+
# The postgres type must be an integer or floating point type,
51+
# timestamp (which will be converted to floating point unix
52+
# epoch seconds with fractional milliseconds), or boolean
53+
# (converted to 0 or 1).
54+
#
55+
# String values will be parsed as floating point numbers - so
56+
# they should be avoided.
57+
#
58+
# Time intervals should be exposed as fractional epoch seconds
59+
# using `extract(epoch from some_interval_value)`. This will
60+
# preserve fractional milliseconds. pg_exporter will NOT handle
61+
# ISO8601 interval string representations.
62+
#
63+
# COUNTER Prometheus metric type. A monotonically increasing value.
64+
# Decreases are treated as counter resets.
65+
#
66+
# Same data type handling as GAUGE.
67+
#
68+
# DURATION Converter that produces a GAUGE typed metric from a time
69+
# interval string representation compatible with golang's
70+
# time.ParseDuration function. Little-used.
71+
#
72+
# HISTOGRAM A Prometheus histogram metric per Prometheus documentation.
73+
#
74+
# LABEL Instead of using the value as a metric, use this column as
75+
# the value of a Prometheus label. All other metrics collected
76+
# for this query will have this label. Add multiple labels for
77+
# multiple dimensions.
78+
#
79+
# Unlike metrics values, labels may be strings - you may use the
80+
# "text" or "varchar" database types.
81+
#
82+
# DISCARD This column should be ignored.
83+
#
84+
# (default) If no usage for a metric is specified, it is assumed to be
85+
# a GAUGE.
86+
#
87+
# Metrics queries return one or more columns. Each column is bound to a result
88+
# metric or label depending on the configuration in the
89+
# metrics['column_name'].usage mapping for the column. Use `AS` to alias column
90+
# names in the query to predictably match the names in the metrics map and avoid
91+
# problems with "?column?" names, duplicate column names, etc.
92+
#
93+
# Metrics queries may return zero or more rows. If zero rows are returned, no
94+
# values will be scraped for the set of metrics. If one or more rows are
95+
# returned, each column is bound to a metric value or label dimension as
96+
# specified by the 'metrics' key. Since Prometheus expects each metric to be
97+
# unique in a given scrape, queries that return multiple rows should bind at
98+
# least one result column to a LABEL that makes each row a distinct metrics
99+
# dimension. For example, per-table metrics should bind the table's qualified
100+
# name to a LABEL.
101+
#
102+
# If auto-discover-databases is enabled, the exporter will run this query once
103+
# for each discovered database unless overridden by a 'master: true' setting on
104+
# the metric. The 'master' setting restricts the metric to run *only* on the
105+
# default database that postgres_exporter connects to with the user-supplied
106+
# connection string. Metrics queries not constrained to the master connection
107+
# should include a LABEL bound to the result of the SQL current_database()
108+
# function to ensure that the metrics are unique within a given scrape.
109+
#
110+
#
111+
112+
1113
pg_replication:
2114
query: "SELECT CASE WHEN NOT pg_is_in_recovery() THEN 0 ELSE GREATEST (0, EXTRACT(EPOCH FROM (now() - pg_last_xact_replay_timestamp()))) END AS lag"
115+
#
116+
# WARNING: "master" means the default database postgres_fdw connects to, NOT
117+
# the replication master in a primary/replica deployment!
118+
#
119+
# Use "master: true" for queries that only need to run once for the whole
120+
# server, rather than for each database.
121+
#
3122
master: true
4123
metrics:
5124
- lag:
@@ -15,6 +134,9 @@ pg_postmaster:
15134
description: "Time at which postmaster started"
16135

17136
pg_stat_user_tables:
137+
# Note that this query exposes current_database() as a column named
138+
# 'datname'. It does not have 'master: true' set, so it'll run for each
139+
# database discovered in pg_database (after applying include/exclude lists).
18140
query: |
19141
SELECT
20142
current_database() datname,
@@ -42,9 +164,13 @@ pg_stat_user_tables:
42164
FROM
43165
pg_stat_user_tables
44166
metrics:
167+
# The 'datname' column is mapped to a metric dimension, so each database
168+
# produces a unique metric even if they have tables with identical names.
45169
- datname:
46170
usage: "LABEL"
47171
description: "Name of current database"
172+
# This metric is multi-dimensional since it's also qualified by database
173+
# schema and table name.
48174
- schemaname:
49175
usage: "LABEL"
50176
description: "Name of the schema that this table is in"

0 commit comments

Comments
 (0)