postgres_exporter icon indicating copy to clipboard operation
postgres_exporter copied to clipboard

query to custom database.

Open kilin-s opened this issue 8 years ago • 14 comments

Hello, I want to use queries.yaml for request to my custom postgres databases, but i don't understand how. Shold i use database name in DSN like this : DATA_SOURCE_NAME=postgresql://postgres_exporter:password@localhost:5432/my_custom_db?sslmode=disable or i can set database in queries.yaml?

kilin-s avatar Jun 21 '17 07:06 kilin-s

And how often query is execute?

kilin-s avatar Jun 21 '17 11:06 kilin-s

That is unnecessary. But if you need to request for example pg_stat_user_tables you need connect to custom db

uspen avatar Jun 21 '17 11:06 uspen

This is something I've not actually tried directly myself. A documentation improvement would be welcome so we can give a canonical example.

wrouesnel avatar Jul 26 '17 12:07 wrouesnel

Noted that we want to support some syntax in queries.yaml for multi-database queries.

wrouesnel avatar Nov 29 '17 01:11 wrouesnel

Hi,

any news on this features?

i would like use query.yaml for execute custom query in MYDB1, or MYDB2 or MYDB3.

thanks

imatpro avatar Feb 08 '18 15:02 imatpro

Probably pending the librarification of the collector, since functionally it's a bunch of collectors being aggregated, with only one including the default metrics.

wrouesnel avatar Mar 05 '18 22:03 wrouesnel

I resolve my problem with this exporter : https://github.com/justwatchcom/sql_exporter

imatpro avatar Apr 12 '18 09:04 imatpro

After using postgres_exporter for a while on single database instances and loving it, it was a rude awakening to discover that I cannot collect custom metrics on multi-database instances. Is there any rough time estimate when it might become available?

ipolishchuk avatar Apr 20 '18 21:04 ipolishchuk

Is there any news about this feature?

mubn avatar Jul 31 '19 13:07 mubn

Probably needs some ability to map multiple queries.yml files to individual DSNs.

wrouesnel avatar Oct 31 '19 14:10 wrouesnel

Expanding the Auto-discover Databases you can add a value in queries.yaml like: database: for example, and the query will only be executed in those databases matching that list, something like:

pg_custom_query:
   query: "SELECT somelabel, somemetric FROM  sometable"
   database: "mydb1,mydb2"
   metrics:
     - somelabel:
           usage: "LABEL"
     - somemetric:
           usage: "GAUGE"

This query will only be executed when mydb1 and/or mydb2 are discovered in the instance.

In that way custom queries can be executed together with other queries using master: "true"

isaiasanchez avatar Jul 08 '20 09:07 isaiasanchez

Expanding the Auto-discover Databases you can add a value in queries.yaml like: database: for example, and the query will only be executed in those databases matching that list, something like:

pg_custom_query:
   query: "SELECT somelabel, somemetric FROM  sometable"
   database: "mydb1,mydb2"
   metrics:
     - somelabel:
           usage: "LABEL"
     - somemetric:
           usage: "GAUGE"

This query will only be executed when mydb1 and/or mydb2 are discovered in the instance.

In that way custom queries can be executed together with other queries using master: "true"

It is don't work. I do

pg_statistic_custome:
  query: "select count(*) from msg_users_online muo where network_status = 1"
  database: "usersDB"
  metrics:
    - users_online:
        usage: "GAUGE"

And I have discovery

postgres_exporter --extend.query-path /etc/prometheus/postgres-queries.yml --auto-discover-databases --web.listen-address :9186

But in logs I have error

time="2020-07-15T11:17:24+03:00" level=info msg="Error running query on database \"127.0.0.1:5432\": pg_statistic_custome pq: relation \"msg_users_online\" does not exist" source="postgres_exporter.go:1356"

What I do wrong ?

Virkuz avatar Jul 15 '20 08:07 Virkuz

Well I was suggesting these configuration for developers to implement it in future version of the exporter, right now it doesn't work.

Now what you can do is something like:

pg_statistic_custome:
  query: "SELECT CASE WHEN current_database() = 'usersDB' THEN (select count(*) from msg_users_online muo where network_status = 1) ELSE 0 END"
  metrics:
    - users_online:
        usage: "GAUGE"

Or also you can configure your postgres_exporter database connection to use your database instead of default postgres and disable autodiscover.

isaiasanchez avatar Jul 22 '20 14:07 isaiasanchez

Just going to leave a comment here since it was super frustrating 😅

Trying to perform a query on a custom database I had to specify DATA_SOURCE_NAME using the following params:

DATA_SOURCE_NAME: "host=<host> port=<port> sslmode=disable user=<user> password=<pass> dbname=<dbname>"

You can find the available params here.

Using DATA_SOURCE_URI I only got "database does not exist" - seems a parsing issue. Using CASE WHEN current_database() postgres was evaluating the query on all databases so I got lots of errors on databases where my custom relations (table) did not exist.

But using the DATA_SOURCE_NAME formatted as above and in combination with PG_EXPORTER_EXTEND_QUERY_PATH and PG_EXPORTER_DISABLE_DEFAULT_METRICS=true I was able to make it work as I wanted 😅👍

Thanks!

asbjornenge avatar Mar 11 '21 11:03 asbjornenge