3

I am trying to use Grafana to chart the output of a query similar to:

SELECT count(*)
FROM myschema.table1
WHERE status_id = 2

Essentially I just want Grafana to run this query every X minutes and then chart the output over time, but from what I can see Grafana requires a specific column to be used as the time series.

Is there some way to achieve what I'm trying to do?

1
  • Grafana runs metric queries as-is (after macros/variables). You could try running your SELECT query, then INSERTing it back into the same database in a different table. Then, in another metric, you could query that table to get the data. Commented Jul 13, 2018 at 2:31

2 Answers 2

1

There are two parts to this.

  1. you want the data in time buckets
  2. you can set Grafana to auto refresh every so often but this is not related to the time buckets

You can use something like the following to achieve 1).

SELECT
  to_timestamp(trunc(  extract(epoch from created_at) / 60000) * 60000) AS time,
  count(*)
FROM
  measures.static_ip_addresses
GROUP BY time
ORDER BY time desc

The 60000 is for 1 minute buckets. You can change this to resize your time buckets. You can modify this query, for example, to set a range of the buckets using a where clause.

Sign up to request clarification or add additional context in comments.

Comments

0

I had a similar task and found a solution.

I just want Grafana to run this query every X minutes

Grafana is only a visualization solution, it does not store data itself, you need some time series database as a proxy. I used this scheme - PostgreSQL -> prometheus-sql -> Prometheus -> Grafana.

Below I added configuration files for prometheus-sql and Prometheus:

  1. docker-compose.yaml
version: '3.7'

volumes:
    prometheus_data: {}

services:

  prometheus:
    image: prom/prometheus
    volumes:
      - ./prometheus/:/etc/prometheus/
      - prometheus_data:/var/prometheus
    command:
      - '--config.file=/etc/prometheus/prometheus.yaml'
      - '--storage.tsdb.path=/var/prometheus'
      - '--web.console.libraries=/usr/share/prometheus/console_libraries'
      - '--web.console.templates=/usr/share/prometheus/consoles'
    ports:
      - 9090:9090
    user: root
    links:
      - prometheus-sql:prometheus-sql
    depends_on:
      - prometheus-sql
    restart: always
  sqlagent:
    image: dbhi/sql-agent
  prometheus-sql:
    image: dbhi/prometheus-sql
    ports:
      - 8080:8080
    links:
      - sqlagent:sqlagent
    depends_on:
      - sqlagent
    command:
      - -config
      - /etc/prometheus-sql/config.yml
      - -queries
      - /etc/prometheus-sql/queries.yml
      - -service
      - http://sqlagent:5000
    volumes:
      - ./sql/config.yml:/etc/prometheus-sql/config.yml
      - ./sql/queries.yml:/etc/prometheus-sql/queries.yml
  1. prometheus/prometheus.yaml:
scrape_configs:
  - job_name: 'Prometheus SQL'
    scrape_interval: 1m
    static_configs:
      - targets: ['prometheus-sql:8080']
  1. prometheus-sql/config.yml (fill your database connection settings here):
defaults:
  data-source: postgresql
  query-interval: 1m
  query-timeout: 30s
  query-value-on-error: -1

data-sources:
  postgresql:
    driver: postgresql
    properties:
      host: DATABASE_HOST
      port: 5432
      user: DATABASE_USER
      password: DATABASE_PASSWORD
      database: DATABASE_NAME
  1. prometheus-sql/queries.yml (tried to adopt to your case)
- table1_records_count_with_status_2:
    help: table1 records count with status_id = 2
    sql: >
        SELECT count(*) AS count FROM myschema.table1 WHERE status_id = 2
    data-field: count

Then in Grafana you could get time series data with the query like this:

query_result_table1_records_count_with_status_2{}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.