Select Page
Jsonnet Libraries for deploying Grafana, Prometheus and Alert Manager

For a long time it has been possible to deploy a ‘stateless’ Grafana instance using Jsonnet using the prometheus-ksonnet library. This gives lots of power – it deploys both Grafana and Prometheus, and alongside these it helps you install your Grafana dashboards and datasources and your prometheus recording rules and alerts. However, this library is opinionated, and complex.

The core of the prometheus-ksonnet library has now been extracted into separate libraries, one each for Grafana, Prometheus and Prometheus Alert Manager. With these libraries, we can now install each application (and their associated resources, e.g. dashboards or alerts) with a more intuitive Jsonnet API that is readily extensible.

For example, to install a Grafana system, point it at a Prometheus backend and install a simple dashboard, this is all that is needed now:

local k = import 'k.libsonnet';
local grafana = import 'grafana/grafana.libsonnet';
{
config+:: {
  prometheus_url: 'http://prometheus',
},

namespace: k.core.v1.namespace.new('grafana'),

prometheus_datasource:: grafana.datasource.new('prometheus', $.config.prometheus_url, type='prometheus', default=true),

grafana: grafana
         + grafana.withAnonymous()
         + grafana.addFolder('Example')
         + grafana.addDashboard('simple', (import 'dashboard.json'), folder='Example')
         + grafana.addDatasource('prometheus', $.prometheus_datasource),
}

We load the Grafana library, create a namespace, create a Prometheus datasource for Grafana, and then we are ready to create our Grafana instance.

To do this, we just reference the Grafana lib, which creates a basic instance, then we tell it we want this to be anonymous (no authentication, it will be in a secure environment), we add a folder, then we add a dashboard to that folder, and finally we add our Prometheus datasource to Grafana.

Now, in 16 lines of code, we have deployed an entire Grafana system, connected it to Prometheus and wired in a dashboard. That is, we’ve built an actually useful system.