Python library for building Grafana dashboards
Go to file
2016-12-13 17:17:41 +00:00
docs Document release process 2016-12-02 15:42:01 +00:00
gfdatasource gfdatasource, but in Python 2016-12-13 17:17:41 +00:00
grafanalib TODOs are now in issue tracker 2016-12-02 14:59:22 +00:00
.gitignore Supported Python versions 2016-12-02 15:20:08 +00:00
CHANGELOG.rst Switch to rST for better Python site integration 2016-12-02 15:31:07 +00:00
circle.yml Set up CircleCI 2016-12-02 14:47:36 +00:00
dev-requirements.txt Set up CircleCI 2016-12-02 14:47:36 +00:00
LICENSE Initial import 2016-12-01 14:42:05 +00:00
Makefile Set up CircleCI 2016-12-02 14:47:36 +00:00
README.rst Switch to rST for better Python site integration 2016-12-02 15:31:07 +00:00
setup.py Include README on PyPI page 2016-12-02 15:35:10 +00:00

==========
grafanalib
==========

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

Do you like `Grafana <http://grafana.org/>`_ 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:

.. code-block:: python

  import itertools

  from grafanalib.core import *


  GRAPH_ID = itertools.count(1)


  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',
            ),
          ],
          id=next(GRAPH_ID),
          yAxes=[
            YAxis(format=OPS_FORMAT),
            YAxis(format=SHORT_FORMAT),
          ],
        ),
        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',
            ),
          ],
          id=next(GRAPH_ID),
          yAxes=[
            YAxis(
              format=SECONDS_FORMAT,
            ),
            YAxis(
              format=SHORT_FORMAT,
              show=False,
            )
          ],
        ),
      ]),
    ],
  )

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 <grafanalib/weave.py>`_ 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:

.. code-block:: console

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

Installation
============

grafanalib is just a Python package, so:

.. code-block:: console

  $ 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 3.4 and 3.5.