Skip to main content

Library for building Grafana dashboards

Project description

https://circleci.com/gh/weaveworks/grafanalib.svg?style=shield

Do you like Grafana but wish you could version your dashboard configuration? Do you find yourself repeating common patterns? If so, grafanalib is for you.

grafanalib lets you generate Grafana dashboards from simple Python scripts.

Writing dashboards

The following will configure a dashboard with a single row, with one QPS graph broken down by status code and another latency graph showing median and 99th percentile latency:

from grafanalib.core import *


dashboard = Dashboard(
  title="Frontend Stats",
  rows=[
    Row(panels=[
      Graph(
        title="Frontend QPS",
        dataSource='My Prometheus',
        targets=[
          Target(
            expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"1.."}[1m]))',
            legendFormat="1xx",
            refId='A',
          ),
          Target(
            expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"2.."}[1m]))',
            legendFormat="2xx",
            refId='B',
          ),
          Target(
            expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"3.."}[1m]))',
            legendFormat="3xx",
            refId='C',
          ),
          Target(
            expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"4.."}[1m]))',
            legendFormat="4xx",
            refId='D',
          ),
          Target(
            expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"5.."}[1m]))',
            legendFormat="5xx",
            refId='E',
          ),
        ],
        yAxes=[
          YAxis(format=OPS_FORMAT),
          YAxis(format=SHORT_FORMAT),
        ],
        alert=Alert(
          name="Too many 500s on Nginx",
          message="More than 5 QPS of 500s on Nginx for 5 minutes",
          alertConditions=[
            AlertCondition(
              Target(
                expr='sum(irate(nginx_http_requests_total{job="default/frontend",status=~"5.."}[1m]))',
                legendFormat="5xx",
                refId='A',
              ),
              timeRange=TimeRange("5m", "now"),
              evaluator=GreaterThan(5),
              operator=OP_AND,
              reducerType=RTYPE_SUM,
            ),
          ],
        )
      ),
      Graph(
        title="Frontend latency",
        dataSource='My Prometheus',
        targets=[
          Target(
            expr='histogram_quantile(0.5, sum(irate(nginx_http_request_duration_seconds_bucket{job="default/frontend"}[1m])) by (le))',
            legendFormat="0.5 quantile",
            refId='A',
          ),
          Target(
            expr='histogram_quantile(0.99, sum(irate(nginx_http_request_duration_seconds_bucket{job="default/frontend"}[1m])) by (le))',
            legendFormat="0.99 quantile",
            refId='B',
          ),
        ],
        yAxes=single_y_axis(format=SECONDS_FORMAT),
      ),
    ]),
  ],
).auto_panel_ids()

There is a fair bit of repetition here, but once you figure out what works for your needs, you can factor that out. See our Weave-specific customizations for inspiration.

Generating dashboards

If you save the above as frontend.dashboard.py (the suffix must be .dashboard.py), you can then generate the JSON dashboard with:

$ generate-dashboard -o frontend.json frontend.dashboard.py

Installation

grafanalib is just a Python package, so:

$ pip install grafanalib

Support

This library is in its very early stages. We’ll probably make changes that break backwards compatibility, although we’ll try hard not to.

grafanalib works with Python 2.7, 3.4, 3.5, and 3.6.

Developing

If you’re working on the project, and need to build from source, it’s done as follows:

$ virtualenv .env
$ . ./.env/bin/activate
$ pip install -e .

gfdatasource

This module also provides a script and docker image which can configure grafana with new sources, or enable app plugins.

The script answers the –help with full usage information, but basic invocation looks like this:

$ <gfdatasource> --grafana-url http://grafana. datasource --data-source-url http://datasource
$ <gfdatasource> --grafana-url http://grafana. app --id my-plugin

Getting Help

If you have any questions about, feedback for or problems with grafanalib:

Your feedback is always welcome!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

grafanalib-0.5.3.tar.gz (25.3 kB view details)

Uploaded Source

Built Distributions

grafanalib-0.5.3-py3-none-any.whl (26.6 kB view details)

Uploaded Python 3

grafanalib-0.5.3-py2-none-any.whl (26.6 kB view details)

Uploaded Python 2

File details

Details for the file grafanalib-0.5.3.tar.gz.

File metadata

  • Download URL: grafanalib-0.5.3.tar.gz
  • Upload date:
  • Size: 25.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for grafanalib-0.5.3.tar.gz
Algorithm Hash digest
SHA256 aba4f87028edd826d6444967fdcfa7d1307e3e564bfe0cadea813a2595088e5d
MD5 137a2ce7a4c19fea4c16aec8f3238f66
BLAKE2b-256 5298c47b4438b83e4bb61900b3d7157f78364cfd2618022f1342b9d167b96aa2

See more details on using hashes here.

File details

Details for the file grafanalib-0.5.3-py3-none-any.whl.

File metadata

File hashes

Hashes for grafanalib-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 9d1372daa83bc7148d995059561bec24efaae7b628449b0a440e7e9f49b42521
MD5 49154bacdaa9ef1fe6410c2d5c4ad60d
BLAKE2b-256 dcb2690724e7e526f775c5f53d4c2d697293abf0e7455bed7d758282d6b40f09

See more details on using hashes here.

File details

Details for the file grafanalib-0.5.3-py2-none-any.whl.

File metadata

File hashes

Hashes for grafanalib-0.5.3-py2-none-any.whl
Algorithm Hash digest
SHA256 885a9c3ca9bd8cd2390a1c3cd4a2bd500fd19e1d2521a2b972503b509547d853
MD5 6280cb78fa58a8e09ebc957cc4065fdf
BLAKE2b-256 daa8ae814759bc99786f0ed33fae0accc177a513519dcdc0afdd931a89416401

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page