Skip to content
For the complete documentation index, see llms.txt. This page is also available as Markdown.

Build a Grafana dashboard

Verify data contracts through the Soda Cloud API, store the check results in PostgreSQL, and chart them in a Grafana dashboard.

This guide shows how to build a data quality dashboard in Grafana from Soda check results. A Python script verifies the data contracts of a data source through the Soda Cloud API, waits for the scans to finish, and writes one row per check result to a PostgreSQL table. Grafana reads the table, which keeps the results of every run.

Use the example as a starting point and adapt the queries and panels to your own dashboard.

Example dashboard

A data engineer wants one dashboard for the AdventureWorks datasets in a PostgreSQL data source. Two of them, dim_product and fact_internet_sales, have a published contract in Soda Cloud.

A Python script, soda_results.py, collects the results. Each time it runs, it:

  1. Lists the datasets in the data source that have a contract

  2. Verifies each contract, which starts a scan on the data source's Soda Runner

  3. Polls each scan until it reaches a final state

  4. Reads the checks of each scan and stores one row per check result in PostgreSQL

Grafana loads the PostgreSQL data source and the dashboard from provisioning files. The project has this layout:

soda-grafana/
├── soda_results.py
└── grafana/
    └── provisioning/
        ├── datasources/
        │   └── soda-results.yml
        └── dashboards/
            ├── soda.yml
            └── soda-check-results.json

Prerequisites

  • A data source in Soda Cloud that is connected to a Soda Runner, with a published contract on each dataset you want on the dashboard. See Onboard datasets on Soda Cloud.

  • A Soda Cloud API key. The user who generates the key needs the Manage contracts permission on the datasets, to verify their contracts. See Generate API keys and Global and dataset roles.

  • A PostgreSQL database for the results, and a user that can create a table in it

  • Python 3

  • Grafana, with access to its provisioning folder, or Docker to run Grafana locally

Install the libraries

Create a virtual environment in the project folder and install requests, to call the Soda Cloud API, and psycopg, to write to PostgreSQL:

Create the script

The script reads its settings from environment variables:

Variable
Value

SODA_CLOUD_URL

https://cloud.soda.io or https://cloud.us.soda.io, depending on your Soda region

SODA_CLOUD_API_KEY_ID

The ID of your Soda Cloud API key

SODA_CLOUD_API_KEY_SECRET

The secret of your Soda Cloud API key

SODA_DATA_SOURCE

The name of the data source in Soda Cloud

RESULTS_DB_HOST

Host name of the results database

RESULTS_DB_PORT

Port of the results database. The default is 5432.

RESULTS_DB_NAME

Name of the results database

RESULTS_DB_USER

User that creates the results table and writes to it

RESULTS_DB_PASSWORD

Password of that user

Save the script in the project folder:

A few details in the script:

  • POST /api/v1/contracts/{contractId}/verify starts a verification and returns the scan ID in the X-Soda-Scan-Id header. The endpoint accepts 10 requests per 60 seconds, so when it responds with 429, the script waits a minute and tries again.

  • A scan goes through queuing, executing, and started before it reaches a final state. The script polls GET /api/v1/scans/{scanId} every 10 seconds and stops on any final state: completed, completedWithWarnings, completedWithFailures, completedWithErrors, failed, canceled, or timedOut.

  • For a scan that ends in failed, canceled, or timedOut, the script stores no results. It prints the scan's failureReason, stores the results of the other scans, and then exits with an error.

  • The outcome of each check comes from the checks list of the scan. The check's name, column, and type come from GET /api/v1/checks, which takes up to 1000 check IDs in its checkIds parameter.

  • The insert passes every value as a query parameter. ON CONFLICT DO NOTHING skips the rows of a scan that is already stored.

  • The view soda_latest_check_results holds the results of the latest stored scan of each dataset.

For every endpoint and response field, see the REST API reference.

Coming from Soda v3? The v3 version of this guide started a scan definition with POST /api/v1/scans. In v4, you verify the contract of each dataset with POST /api/v1/contracts/{contractId}/verify, and GET /api/v1/scans/{scanId} reports the state and check results of the scan as before.

Run the script

Set the environment variables and run the script:

The script prints a line for each contract it verifies and each scan it stores:

In this run, fact_internet_sales has a sale with an amount of 0, so its contract fails. dim_product has a product without a subcategory, so its contract warns.

Run the script on a schedule, for example from cron or your orchestrator, to collect the results over time.

Provision Grafana

Grafana reads data sources and dashboards from YAML files in its provisioning folder when it starts. See Provision Grafana in the Grafana documentation.

Create a read-only user

Grafana doesn't validate the safety of the SQL queries that panels run, so give it a database user that can only read the results. After the script's first run has created the table and the view, run these statements as a database administrator:

Add the data source

Grafana replaces each $__env{…} with the value of an environment variable, so the file holds no credentials:

Besides the results database settings of the script, the file reads GRAFANA_DB_USER and GRAFANA_DB_PASSWORD, the read-only user, and RESULTS_DB_SSLMODE, the TLS/SSL mode: disable, require, verify-ca, or verify-full. Most cloud-hosted PostgreSQL services require at least require. See Configure the PostgreSQL data source in the Grafana documentation.

Add the dashboard

The dashboard provider tells Grafana to load the dashboard files from /etc/grafana/provisioning/dashboards, the default provisioning folder of the Grafana Docker image. If your Grafana reads provisioning files from another folder, change path:

The dashboard has four panels, each with a SQL query on the results table or view:

Panel
What it shows

Latest results

The number of passed, warned, failed, and not evaluated checks in the latest scan of each dataset

Latest results by dataset

The same results, per dataset

Failed and warned checks per scan

The number of failed and warned checks in every stored scan, per dataset, over the dashboard's time range

Latest check results

One row per check, failed and warned checks first

To change the panels, see Visualizations and the PostgreSQL query editor in the Grafana documentation.

Start Grafana

To try the dashboard on your machine, run Grafana in Docker with the provisioning folder mounted at /etc/grafana/provisioning. Set RESULTS_DB_HOST to an address of the results database that the container can reach.

Open http://localhost:3000 and sign in with admin as the user name and password. See Sign in to Grafana in the Grafana documentation. Then open Dashboards > Soda check results.

The Soda check results dashboard in Grafana, with one failed check on fact_internet_sales and one warned check on dim_product
The dashboard after two runs of the script: the second run found a sale with an amount of 0

Next steps

Last updated

Was this helpful?