Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build a Grafana dashboard using Grafonnet #18

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
local grafana = import 'grafonnet/grafana.libsonnet';
local dashboard = grafana.dashboard;
local annotation = grafana.annotation;
local template = grafana.template;
local graphPanel = grafana.graphPanel;
local prometheus = grafana.prometheus;

dashboard.new(
'title',
editable=true,
refresh='5s',
time_from='now-6h',
time_to='now',
timepicker={},
schemaVersion=27,
uid='uid',
)
.addAnnotation(annotation.default)
.addTemplate(
template.datasource(
name='cluster',
query='prometheus',
current='Thanos',
hide='',
)
)
.addTemplate(
template.new(
name='namespace',
datasource='${cluster}',
query={
query: 'label_values(grpc_io_server_completed_rpcs, namespace)',
refId: 'StandardVariableQuery'
},
label='Namespace',
hide='',
refresh='load',
definition='label_values(grpc_io_server_completed_rpcs, namespace)'
)
)
.addPanel(
graphPanel.new(
title='Number of requests',
datasource='${cluster}',
fill=1,
legend_show=true,
lines=true,
linewidth=1,
pointradius=2,
stack=true,
shared_tooltip=true,
value_type='individual',
).resetYaxes().
addYaxis(
format='none',
).addYaxis(
format='short',
).addTarget(
prometheus.custom_target(
expr='expr',
legendFormat='legendFormat',
)
), gridPos={
x: 0,
y: 1,
w: 18,
h: 10,
}
)
15 changes: 15 additions & 0 deletions content/posts/build-a-grafana-dashboard-generator/examples/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/manabie-com/manabie-com.github.io/content/posts/build-a-grafana-dashboard-generator/examples

go 1.18

require (
github.com/google/go-jsonnet v0.18.0
github.com/stretchr/testify v1.4.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v2 v2.2.7 // indirect
sigs.k8s.io/yaml v1.1.0 // indirect
)
31 changes: 31 additions & 0 deletions content/posts/build-a-grafana-dashboard-generator/examples/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM=
github.com/google/go-jsonnet v0.18.0 h1:/6pTy6g+Jh1a1I2UMoAODkqELFiVIdOxbNwv0DDzoOg=
github.com/google/go-jsonnet v0.18.0/go.mod h1:C3fTzyVJDslXdiTqw/bTFk7vSGyCtH3MGRbDfvEwGd0=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc=
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.7 h1:VUgggvou5XRW9mHwD/yXxIYSMtY0zoKQf/v226p2nyo=
gopkg.in/yaml.v2 v2.2.7/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
sigs.k8s.io/yaml v1.1.0 h1:4A07+ZFc2wgJwo8YNlQpr1rVlgUDlxXHhPJciaPY5gs=
sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
default::
{
builtIn: 1,
datasource: '-- Grafana --',
enable: true,
hide: true,
iconColor: 'rgba(0, 211, 255, 1)',
name: 'Annotations & Alerts',
type: 'dashboard',
},

/**
* @name annotation.datasource
*/

datasource(
name,
datasource,
expr=null,
enable=true,
hide=false,
iconColor='rgba(255, 96, 96, 1)',
tags=[],
type='tags',
builtIn=null,
)::
{
datasource: datasource,
enable: enable,
[if expr != null then 'expr']: expr,
hide: hide,
iconColor: iconColor,
name: name,
showIn: 0,
tags: tags,
type: type,
[if builtIn != null then 'builtIn']: builtIn,
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
local timepickerlib = import 'timepicker.libsonnet';

{
/**
* Creates a [dashboard](https://grafana.com/docs/grafana/latest/features/dashboard/dashboards/)
*
* @name dashboard.new
*
* @param title The title of the dashboard
* @param editable (default: `false`) Whether the dashboard is editable via Grafana UI.
* @param style (default: `'dark'`) Theme of dashboard, `'dark'` or `'light'`
* @param tags (optional) Array of tags associated to the dashboard, e.g.`['tag1','tag2']`
* @param time_from (default: `'now-6h'`)
* @param time_to (default: `'now'`)
* @param timezone (default: `'browser'`) Timezone of the dashboard, `'utc'` or `'browser'`
* @param refresh (default: `''`) Auto-refresh interval, e.g. `'30s'`
* @param timepicker (optional) See timepicker API
* @param graphTooltip (default: `'default'`) `'default'` : no shared crosshair or tooltip (0), `'shared_crosshair'`: shared crosshair (1), `'shared_tooltip'`: shared crosshair AND shared tooltip (2)
* @param hideControls (default: `false`)
* @param schemaVersion (default: `14`) Version of the Grafana JSON schema, incremented each time an update brings changes. `26` for Grafana 7.1.5, `22` for Grafana 6.7.4, `16` for Grafana 5.4.5, `14` for Grafana 4.6.3. etc.
* @param uid (default: `''`) Unique dashboard identifier as a string (8-40), that can be chosen by users. Used to identify a dashboard to update when using Grafana REST API.
* @param description (optional)
*
* @method addTemplate(template) Add a template variable
* @method addTemplates(templates) Adds an array of template variables
* @method addAnnotation(annotation) Add an [annotation](https://grafana.com/docs/grafana/latest/dashboards/annotations/)
* @method addPanel(panel,gridPos) Appends a panel, with an optional grid position in grid coordinates, e.g. `gridPos={'x':0, 'y':0, 'w':12, 'h': 9}`
* @method addPanels(panels) Appends an array of panels
* @method addLink(link) Adds a [dashboard link](https://grafana.com/docs/grafana/latest/linking/dashboard-links/)
* @method addLinks(dashboardLink) Adds an array of [dashboard links](https://grafana.com/docs/grafana/latest/linking/dashboard-links/)
* @method addRequired(type, name, id, version)
* @method addInput(name, label, type, pluginId, pluginName, description, value)
* @method addRow(row) Adds a row. This is the legacy row concept from Grafana < 5, when rows were needed for layout. Rows should now be added via `addPanel`.
*/
new(
title,
editable=false,
style='dark',
tags=[],
time_from='now-6h',
time_to='now',
timezone='browser',
refresh='',
timepicker=timepickerlib.new(),
graphTooltip='default',
hideControls=false,
schemaVersion=14,
uid='',
description=null,
):: {
local it = self,
_annotations:: [],
[if uid != '' then 'uid']: uid,
editable: editable,
[if description != null then 'description']: description,
gnetId: null,
graphTooltip:
if graphTooltip == 'shared_tooltip' then 2
else if graphTooltip == 'shared_crosshair' then 1
else if graphTooltip == 'default' then 0
else graphTooltip,
hideControls: hideControls,
id: null,
links: [],
panels:: [],
refresh: refresh,
rows: [],
schemaVersion: schemaVersion,
style: style,
tags: tags,
time: {
from: time_from,
to: time_to,
},
timezone: timezone,
timepicker: timepicker,
title: title,
version: 0,
addAnnotations(annotations):: self {
_annotations+:: annotations,
},
addAnnotation(a):: self.addAnnotations([a]),
addTemplates(templates):: self {
templates+: templates,
},
addTemplate(t):: self.addTemplates([t]),
templates:: [],
annotations: { list: it._annotations },
templating: { list: it.templates },
_nextPanel:: 2,
addRow(row)::
self {
// automatically number panels in added rows.
// https://github.com/kausalco/public/blob/master/klumps/grafana.libsonnet
local n = std.length(row.panels),
local nextPanel = super._nextPanel,
local panels = std.makeArray(n, function(i)
row.panels[i] { id: nextPanel + i }),

_nextPanel: nextPanel + n,
rows+: [row { panels: panels }],
},
addPanels(newpanels)::
self {
// automatically number panels in added rows.
// https://github.com/kausalco/public/blob/master/klumps/grafana.libsonnet
local n = std.foldl(function(numOfPanels, p)
(if 'panels' in p then
numOfPanels + 1 + std.length(p.panels)
else
numOfPanels + 1), newpanels, 0),
local nextPanel = super._nextPanel,
local _panels = std.makeArray(
std.length(newpanels), function(i)
newpanels[i] {
id: nextPanel + (
if i == 0 then
0
else
if 'panels' in _panels[i - 1] then
(_panels[i - 1].id - nextPanel) + 1 + std.length(_panels[i - 1].panels)
else
(_panels[i - 1].id - nextPanel) + 1

),
[if 'panels' in newpanels[i] then 'panels']: std.makeArray(
std.length(newpanels[i].panels), function(j)
newpanels[i].panels[j] {
id: 1 + j +
nextPanel + (
if i == 0 then
0
else
if 'panels' in _panels[i - 1] then
(_panels[i - 1].id - nextPanel) + 1 + std.length(_panels[i - 1].panels)
else
(_panels[i - 1].id - nextPanel) + 1

),
}
),
}
),

_nextPanel: nextPanel + n,
panels+::: _panels,
},
addPanel(panel, gridPos):: self.addPanels([panel { gridPos: gridPos }]),
addRows(rows):: std.foldl(function(d, row) d.addRow(row), rows, self),
addLink(link):: self {
links+: [link],
},
addLinks(dashboardLinks):: std.foldl(function(d, t) d.addLink(t), dashboardLinks, self),
required:: [],
__requires: it.required,
addRequired(type, name, id, version):: self {
required+: [{ type: type, name: name, id: id, version: version }],
},
inputs:: [],
__inputs: it.inputs,
addInput(
name,
label,
type,
pluginId=null,
pluginName=null,
description='',
value=null,
):: self {
inputs+: [{
name: name,
label: label,
type: type,
[if pluginId != null then 'pluginId']: pluginId,
[if pluginName != null then 'pluginName']: pluginName,
[if value != null then 'value']: value,
description: description,
}],
},
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
dashboard:: import 'dashboard.libsonnet',
template:: import 'template.libsonnet',
annotation:: import 'annotation.libsonnet',
graphPanel:: import 'graph_panel.libsonnet',
prometheus:: import 'prometheus.libsonnet',
}
Loading