A tool to run queries in defined frequency and expose the count as prometheus metrics. Supports MongoDB and SQL

query2metric

A tool to run db queries in defined frequency and expose the count as prometheus metrics.

Why ?

Product metrics play an important role in understanding product adoption and historic metrics helps answer many questions about a product (for eg: which day of the week do I get the most signups). One common thing is that most of these metrics are extracted by querying the databases. The tool takes queries and time frequency as configuration and runs the queries in the specified intervals and exposes the output as prometheus metrics.

Example

Create a config.yaml file.

config.yaml

connections:
  - name: mongodb1
    type: MONGO
    connectionStringFromEnv: MONGO_CONN
    metrics:
      - name: active_user_count
        helpString: users in the product
        database: test
        collection: test
        query: '{"is_active":true}'
        time: 10
      - name: total_user_count
        helpString: users in the product
        database: test
        collection: test
        query: ""
        time: 120
  - name: postgres1
    type: SQL
    connectionStringFromEnv: POSTGRES_CONN
    metrics:
      - name: template_count
        helpString: products in the db
        query: select * from templates
        time: 2
      - name: active_template_count
        helpString: products in the db
        query: error
        time: 4

Along with the metrics defined, the success and failure count of queries are also exposed as prometheus metrics.

query2metric_success_count - No of successful queries coverted to metrics.

query2metric_error_count - No of errors when converting query to metric.

Note: Errors can occur due to invalid queries or connection issues to the db, one can use the logs to debug the issues.

How to use ?

At present the tool supports mongo and sql queries. Just create a config.yaml file and run the code.

Mongo

set type as MONGO and metrics as given in example with query,time (in seconds) etc.

connections:
- name: mongodb1
    type: MONGO
    connectionStringFromEnv: MONGO_CONN
    metrics:
      - name: active_user_count
        helpString: users in the product
        database: test
        collection: test
        query: '{"is_active":true}'
        time: 10

SQL

set type as SQL and metrics as give in example.

connections:
  - name: postgres1
    type: SQL
    connectionStringFromEnv: POSTGRES_CONN
    metrics:
      - name: template_count
        helpString: products in the db
        query: select * from templates
        time: 2

Run example using docker

You can run the example along with prometheus and grafana using docker.

docker-compose.yaml

docker-compose up

metrics output: localhost:8090/metrics.

prometheus dashboard: localhost:9090/graph.

grafana dashboard: localhost:3000/d/qqTN2unMk/example?orgId=1.

Example Output:

Credits

Owner
S Santhosh Nagaraj
Anbe Sivam ♥️ | Backend, Distributed Systems | SWE @kinvolk Past: @atlanhq @untitledsp
S Santhosh Nagaraj
Similar Resources

Get data from .csv files use SQL-like queries.

csvql Get data from .csv files use SQL-like queries. Задание Необходимо написать консольную программу, которая по заданному клиентом запросу осуществл

Dec 7, 2021

Opionated sql formatter for use with .go files containing backticked queries

fumpt-the-sql Opionated sql formatter for use with .go files containing backticked queries. Uses https://sqlformat.darold.net/ for the actual sql form

Dec 10, 2021

Go package for sharding databases ( Supports every ORM or raw SQL )

Go package for sharding databases ( Supports every ORM or raw SQL )

Octillery Octillery is a Go package for sharding databases. It can use with every OR Mapping library ( xorm , gorp , gorm , dbr ...) implementing data

Dec 16, 2022

Use SQL to query instances, domains and more from Prometheus.

Use SQL to query instances, domains and more from Prometheus.

Use SQL to instantly query Prometheus metrics, alerts, labels and more. Open source CLI. No DB required.

Nov 28, 2022

Go-sql-reader - Go utility to read the externalised sql with predefined tags

go-sql-reader go utility to read the externalised sql with predefined tags Usage

Jan 25, 2022

This Service collects slow queries and returns them in response.

pgsql-api-go This Service collects slow queries and returns them in response. Status This service is the very first version of the project. App is up

Dec 30, 2021

A database connection wrapper to cache prepared statements by transforming queries to use with array arguments.

sqlpp sqlpp is a sql(MySQL and PostgreSQL) database connection wrapper to cache prepared statements by transforming queries ("...in (?)...", []) to us

Feb 9, 2022

A reverse proxy for postgres which rewrites queries.

pg-rewrite-proxy A reverse proxy for postgres which rewrites queries. Arbitrary rewriting is supported by supplying an LUA script to the proxy applica

Dec 12, 2022

A simple Golang-based application that queries a PostgreSQL database

Qwik-E-Mart Demo App A simple Golang-based application that queries a PostgreSQL database named qwikemart to read and return customer data stored in t

Nov 6, 2021
Run SQL queries against JSON, CSV, Excel, Parquet, and more.

Run SQL queries against JSON, CSV, Excel, Parquet, and more This is a CLI companion to DataStation (a GUI) for running SQL queries against data files.

Dec 31, 2022
SQL API is designed to be able to run queries on databases without any configuration by simple HTTP call.

SQL API SQL API is designed to be able to run queries on databases without any configuration by simple HTTP call. The request contains the DB credenti

Dec 2, 2022
Parses a file and associate SQL queries to a map. Useful for separating SQL from code logic

goyesql This package is based on nleof/goyesql but is not compatible with it any more. This package introduces support for arbitrary tag types and cha

Oct 20, 2021
write APIs using direct SQL queries with no hassle, let's rethink about SQL

SQLer SQL-er is a tiny portable server enables you to write APIs using SQL query to be executed when anyone hits it, also it enables you to define val

Jan 7, 2023
CLI tool that can execute SQL queries on CSV, LTSV, JSON and TBLN. Can output to various formats.
CLI tool that can execute SQL queries on CSV, LTSV, JSON and TBLN. Can output to various formats.

trdsql CLI tool that can execute SQL queries on CSV, LTSV, JSON and TBLN. It is a tool like q, textql and others. The difference from these tools is t

Jan 1, 2023
Mergestat - a command-line tool for running SQL queries on git repositories and related data sources
Mergestat - a command-line tool for running SQL queries on git repositories and related data sources

Query git repositories with SQL. Generate reports, perform status checks, analyze codebases. ?? ??

Dec 30, 2022
WAL-G is an archival restoration tool for PostgreSQL, MySQL/MariaDB, and MS SQL Server (beta for MongoDB and Redis).

WAL-G is an archival restoration tool for PostgreSQL, MySQL/MariaDB, and MS SQL Server (beta for MongoDB and Redis).

Jan 1, 2023
sqlbench measures and compares the execution time of one or more SQL queries.

sqlbench sqlbench measures and compares the execution time of one or more SQL queries. The main use case is benchmarking simple CPU-bound query varian

Dec 6, 2022
Querycrate - A simple library that loads and keeps SQL queries from files

QueryCrate A simple library for loading & getting string queries from files. How

Feb 15, 2022
pggen - generate type safe Go methods from Postgres SQL queries

pggen - generate type safe Go methods from Postgres SQL queries pggen is a tool that generates Go code to provide a typesafe wrapper around Postgres q

Jan 3, 2023