You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
cmd,collector: add --scrape.timeout to time-bound scrapes (#1)
Currently the exporter runs queries without any time bounds. This means
a poorly behaved query will just run for ever. A connection limit on the
exporter role should prevent queries from piling up, but query pile up
is still something to be concerned about.
Validated by creating this file:
```bash
❯ cat timeout_test_queries.yaml
pg_long_query:
query: "SELECT 5 as value FROM (SELECT pg_sleep(5)) as t"
metrics:
- value:
usage: "GAUGE"
description: "5 second query"
```
Running with a `--scrape.timeout` of 10s:
```bash
❯ ./postgres_exporter \
--extend.query-path=timeout_test_queries.yaml \
--scrape.timeout=10s \
--log.level=info
```
`cURL`-ing:
```bash
❯ curl http://localhost:9187/metrics | grep long_query
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 28414 0 28414 0 0 5645 0 --:--:-- 0:00:05 --:--:-- 5646# HELP pg_long_query_value 5 second query
# TYPE pg_long_query_value gauge
pg_long_query_value{server="127.0.0.1:5432"} 5
100 97364 0 97364 0 0 19341 0 --:--:-- 0:00:05 --:--:-- 25461
```
Repeating with a `--scrape.timeout` of 2s does not include the
`pg_long_query_value`
```bash
❯ curl http://localhost:9187/metrics | grep long_query
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 93091 0 93091 0 0 46352 0 --:--:-- 0:00:02 --:--:-- 46360
```
But does include partial results:
```bash
❯ curl http://localhost:9187/metrics | grep pg_settings | wc -l
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 93192 0 93192 0 0 46313 0 --:--:-- 0:00:02 --:--:-- 46318
810
```
---------
Signed-off-by: Max Englander <[email protected]>
Copy file name to clipboardExpand all lines: cmd/postgres_exporter/main.go
+3Lines changed: 3 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -49,6 +49,7 @@ var (
49
49
excludeDatabases=kingpin.Flag("exclude-databases", "A list of databases to remove when autoDiscoverDatabases is enabled (DEPRECATED)").Default("").Envar("PG_EXPORTER_EXCLUDE_DATABASES").String()
50
50
includeDatabases=kingpin.Flag("include-databases", "A list of databases to include when autoDiscoverDatabases is enabled (DEPRECATED)").Default("").Envar("PG_EXPORTER_INCLUDE_DATABASES").String()
51
51
metricPrefix=kingpin.Flag("metric-prefix", "A metric prefix can be used to have non-default (not \"pg\") prefixes for each of the metrics").Default("pg").Envar("PG_EXPORTER_METRIC_PREFIX").String()
52
+
scrapeTimeout=kingpin.Flag("scrape-timeout", "Maximum time for a scrape to complete before timing out (0 = no timeout)").Default("0").Envar("PG_EXPORTER_SCRAPE_TIMEOUT").Duration()
52
53
logger=promslog.NewNopLogger()
53
54
)
54
55
@@ -114,6 +115,7 @@ func main() {
114
115
WithConstantLabels(*constantLabelsList),
115
116
ExcludeDatabases(excludedDatabases),
116
117
IncludeDatabases(*includeDatabases),
118
+
WithTimeout(*scrapeTimeout),
117
119
}
118
120
119
121
exporter:=NewExporter(dsns, opts...)
@@ -136,6 +138,7 @@ func main() {
136
138
excludedDatabases,
137
139
dsn,
138
140
[]string{},
141
+
collector.WithTimeout(*scrapeTimeout),
139
142
)
140
143
iferr!=nil {
141
144
logger.Warn("Failed to create PostgresCollector", "err", err.Error())
query:="SELECT name, setting, COALESCE(unit, ''), short_desc, vartype FROM pg_settings WHERE vartype IN ('bool', 'integer', 'real') AND name != 'sync_commit_cancel_wait';"
0 commit comments