1.0.15 • Published 7 years ago

grafana-dsl v1.0.15

Weekly downloads
2
License
ISC
Repository
github
Last release
7 years ago

Grafana-DSL

A DSL for programmatically generating Grafana dashboards. Currently, this module does not interface with Grafana's API. Instead, write the configurations to disk and configure Grafana to load the dashboards.

[dashboards.json]
enabled = true
path = /var/lib/dashboards

Installation

npm install -s grafana-dsl

Usage

import dashboard from 'grafana-dsl';

const backendDashboard = dashboard()
  .title('Backend Dashboard')
  .row(row => row()
    .graohPanel(panel => panel()
      .title('CPU')
      .span(12)
      .target(target => target()
        .expr('node_cpu')
        .datasource('Prometheus'))
      .alert(alert=> alert()
        .name('High CPU')
        .notifications([1])
        .condition(condition => condition()
          .when('max')
          .of(1)
          .isAbove(50)))));

// output
require('fs').writeFileSync(__dirname + '/backendDashboard.json', JSON.stringify(backendDashboard.serialize()));

In addition to the above syntax, you can also create objects like rows, panels, alerts, and more directly. This pattern enables reusable configuration components.

import dashboard, { graohPanel } from 'grafana-dsl';

const cpuGraph = graphPanel()
  .title('CPU')
  .span(12)
  .target(target => target()
    .expr('')
    .datasource('Prometheus'))
  .alert(alert => alert()
    .name('High CPU')
    .notifications([1])
    .condition(condition => condition()
      .when('avg')
      .of(1)
      .isAbove(50)));

const backendDashboard = dashboard()
  .row(row => row()
    .graphPanel(cpuGraph));

// output
require('fs').writeFileSync(__dirname + '/backendDashboard.json', JSON.stringify(backendDashboard.serialize()));

DSL

All top level DSL methods accept a config object. Use this parameter to directly pass raw Grafana configuration to accomodate cases where the DSL doesn't provide a required method.

Dashboard

methoddescriptiondefault
titleSet dashboard title''
tagsSet dashboard tags[]
refreshSet refresh rate1m
time(from,to='now')Set time rangefrom=now-1h to=now
templateAdd template variable. Accepts either a callback function receiving template DSL function, or a pre-configured template object
rowAdd row. Accepts either a callback function receiving row DSL function, or a pre-built row object

Template

methoddescriptiondefault
nameSet template variable name''
typeSet template variable type (e.g: query,custom)''
multi(val=true)Toggle multi-selectfalse
includeAll(val=true)Include all optionfalse
queryTemplate variable query. For type=custom, a comma delimited list of options
datasourceFor type=query, set the datasource
regexFor type=query, apply a regex against each return value
refreshOnLoadSets refresh=1 to enable variable refresh on dashboard load
refreshOnTimeChangeSets refresh=2 to enable variable refresh on time change

Row

methoddescriptiondefault
titleSet row title''
singleStatPanelAccepts either a callback function receiving singleStatPanel DSL function, or a pre-configured singleStatPanel object
graphPanelAccepts either a callback function receiving graphPanel DSL function, or a pre-configured graphPanel object

graphPanel

methoddescriptiondefault
titleSet panel title''
spanSet panel span (Out of 12 columns)12
datasourceSet panel datasource''
linesToggle lines draw modetrue
pointsToggle lines draw modefalse
barsToggle lines draw modefalse
formatShortcut for setting yaxes.format
fillSet fill transparency 0-101
yaxesConfigure yaxes for graph. Accepts either a callback function receiving yaxes DSL function, or a pre-configured yaxes object
targetAdd target to panel. Accepts either a callback function receiving target DSL function, or a pre-configured target object
alertConfigure alerting for panel. Accepts either a callback function receiving an alert DSL function, or a pre-configured alert object

singleStatPanel

methoddescriptiondefault
titleSet panel title''
spanSet panel span (Out of 12 columns)12
formatSet data format (e.g short, percent)'short'
datasourceSet panel datasource''
targetAdd target to panel. Accepts either a callback function receiving target DSL function, or a pre-configured target object
sparklineEnable sparklines. Accepts either a callback function receiving a sparkline DSL function, or a pre-configured sparkline object
alertConfigure alerting for panel. Accepts either a callback function receiving an alert DSL function, or a pre-configured alert object
valueNameSet value name property for panel'avg'

Target

Configure a panel target.

methoddescriptiondefault
exprThe query to execute''
legendFormatConfigure target's legend format

sparkline

Configure singlestat panel sparkline.

methoddescriptiondefault
show(val=true)Show sparklinefalse
full(val=true)Set sparkline to full background modefalse
fillColor({rgba(,,,)})Set sparkline fill color'rgba(31, 118, 189, 0.18)'
lineColor({rgb(,,,)})Set sparkline line color'rgb(31, 120, 193)'

yaxes

Configure panel yaxes for graphPanel |method|description|default| |---|---|---| |format|Set data format|'short'| |show|Show y axes|true| |max|Set max value|| |`min`|Set min value||

Alert

Configure a panel alert. Currently only for graphPanel

methoddescriptiondefault
nameSet alert name''
frequencySet alert check frequency''
notifications([{0-9},])Set alert notifications. Pass in the notification ID. You can find this from the notification edit page (e.g /alerting/notification/1/edit -> .notifications([1]))[]
conditionConfigure an alert condition. Accepts either a callback function receiving a condition DSL function, or a pre-configured condition object

Condition

Configure an alert condition.

methoddescriptiondefault
whene.g: avg,min,max,sum,median,count,last'avg'
of(index,start='5m',end='now')Set the alert query target and window. The first param is the id of a configured panel target. If your panel has a single target, you would pass 1index=null start='5m' end='now'
isAbove(val)Set condition evaluator to gt
isBelow(val)Set alert condition evaluator to lt
isOutsideRange(from, to)Set alert condition evaluator to outside_range
isWithintRange(from, to)Set alert condition evaluator to within_range
hasNoValueSet alert condition evaluator to no_value

Contributors