prometheus-community / postgres_exporter

A PostgreSQL metric exporter for Prometheus
Apache License 2.0
2.72k stars 725 forks source link

Adding new metrics via a config file problem #496

Open hunterhuang8810 opened 3 years ago

hunterhuang8810 commented 3 years ago

Hi Team,

From doc I see we can add new metrics via config file: Adding new metrics via a config file The -extend.query-path command-line argument specifies a YAML file containing additional queries to run. Some examples are provided in queries.yaml.

so I I try to test adding below metric pg_hunter in my queries.yaml:

pg_replication: 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" master: true metrics:

pg_hunter: query: "SELECT 8888,9999" master: true metrics:

pg_postmaster: query: "SELECT pg_postmaster_start_time as start_time_seconds from pg_postmaster_start_time()" master: true metrics:

And when I try to start postgres_exporter, it failed. Can't I add new metrics like above? how to do that?

[root@localhost postgres_exporter-0.9.0.linux-amd64]# ./postgres_exporter --extend.query-path queries.yaml INFO[0000] Starting Server: :9187 source="postgres_exporter.go:1850" INFO[0000] Established new database connection to "127.0.0.1:54322". source="postgres_exporter.go:983" INFO[0000] Semantic Version Changed on "127.0.0.1:54322": 0.0.0 -> 12.5.0 source="postgres_exporter.go:1552" panic: "pghunter?column?" is not a valid metric name

goroutine 17 [running]: github.com/prometheus/client_golang/prometheus.MustNewConstMetric(...) /go/pkg/mod/github.com/prometheus/client_golang@v1.9.0/prometheus/value.go:107 main.queryNamespaceMapping(0xc00008aa50, 0xc00017c650, 0x9, 0x0, 0x0, 0x0, 0xc0001c0930, 0x1, 0x0, 0x0, ...) /app/cmd/postgres_exporter/postgres_exporter.go:1443 +0x2c2a main.queryNamespaceMappings(0xc0000575c0, 0xc00008aa50, 0x0) /app/cmd/postgres_exporter/postgres_exporter.go:1495 +0x80f main.(Server).Scrape(0xc00008aa50, 0xc0000575c0, 0xc00008aa00, 0x0, 0x0) /app/cmd/postgres_exporter/postgres_exporter.go:1035 +0xac main.(Exporter).scrapeDSN(0xc00008a9a0, 0xc0000575c0, 0xc000028061, 0x37, 0x5, 0x1) /app/cmd/postgres_exporter/postgres_exporter.go:1728 +0x147 main.(Exporter).scrape(0xc00008a9a0, 0xc0000575c0) /app/cmd/postgres_exporter/postgres_exporter.go:1618 +0x134 main.(Exporter).Collect(0xc00008a9a0, 0xc0000575c0) /app/cmd/postgres_exporter/postgres_exporter.go:1259 +0x39 github.com/prometheus/client_golang/prometheus.(Registry).Gather.func1() /go/pkg/mod/github.com/prometheus/client_golang@v1.9.0/prometheus/registry.go:448 +0x169 created by github.com/prometheus/client_golang/prometheus.(Registry).Gather /go/pkg/mod/github.com/prometheus/client_golang@v1.9.0/prometheus/registry.go:538 +0xe8e [root@localhost postgres_exporter-0.9.0.linux-amd64]#

keithf4 commented 3 years ago

It's failing on the metric that has no column names. ?column? is the column name that postgres gives when you run a bare select statement like that

# SELECT 8888,9999;
 ?column? | ?column? 
----------+----------
     8888 |     9999

If you either give those columns an alias and reference it like you did your other metrics, it should work fine. Or remove that hunter metric and it should work fine.

hunterhuang8810 commented 3 years ago

OK , thanks

Is there doc to reference all the default metrics for postgres_exporter? seems most metrics is accumulative, but under many scenarios, delta metrics more helpful