albertodonato / query-exporter

Export Prometheus metrics from SQL queries
GNU General Public License v3.0
436 stars 101 forks source link

How to injest database table data to prometheus #150

Closed anurag-appperfect closed 1 year ago

anurag-appperfect commented 1 year ago

I want to injest database query results to prometheus. I am executing a query and getting the result in table Screenshot 2023-05-23 at 4 50 09 PM 11 rows in set (0.006 sec)`

How to insert this data to prometheus using Prometheus

albertodonato commented 1 year ago

You can define a metric named metric9 and use name as a label. It depends on what you need to do.

anurag-appperfect commented 1 year ago

Thank you for your suggestion and do I need to pass separate parameters for each value of name ?

albertodonato commented 1 year ago

it depends on your query

anurag-appperfect commented 1 year ago
 query8:
     interval: 5
     databases: [db1]
     metrics: [metric8]
     sql: select count(*) AS metric8, :param AS study from study_db WHERE type_study = :param group by study_db order by COUNT(*) desc;
     parameters:
       - param: "OP"
       - param: "OT"
       - param: "XC"
       - param: "OPT"
       - param: "OP\\OPT"
       - param: "XC\\OPT"
       - param: "XC\\OT"
       - param: " "
       - param: "SC"
       - param: "XC\\OT\\OP"
       - param: "OT\\OP"

Here is the code sample, what should I do to avoid passing each value of param ?

albertodonato commented 1 year ago

If I understand your db schema correctly you could just have a query like:

select count(*) AS metric8, type_study as study from study_db group by type_study

with metric8 having a study label defined.