grafana + jsonnet
TRANSCRIPT
![Page 1: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/1.jpg)
Grafana + Jsonnet
Julien Pivotto (@roidelapluie)
GrafanaCon 2018
![Page 2: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/2.jpg)
name: "Julien Pivotto", company: "Inuits", grafana: first_issue: id: $.grafana.first_pull.id + 1, date: "Apr 14, 2014", , first_pull: // My first PR was somehow // related to dashboards as code // already id: 310, date: $.grafana.first_issue.date, , ,
![Page 3: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/3.jpg)
"company": "Inuits", "grafana": "first_issue": "date": "Apr 14, 2014", "id": 311 , "first_pull": "date": "Apr 14, 2014", "id": 310 , "name": "Julien Pivotto"
![Page 4: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/4.jpg)
Grafana DashboardsJSON
Templates
Annotations
Panels
Links
![Page 5: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/5.jpg)
Grafana at scalePlenty of dashboards
Consistency of templates
Same links
Same annotations
Same panels
![Page 6: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/6.jpg)
Panels ConsistencySame color for given error code
Same color for given server
Same rules (stacked, lines, bars, width.datasource)
![Page 7: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/7.jpg)
2 ways of solving thatSpending way to much time building andcorrecting dashboards in the grafana UI
Dashboards as code
![Page 8: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/8.jpg)
Dashboards as Codehttps://github.com/uber/grafana-dash-gen
https://github.com/weaveworks/grafanalib
https://github.com/jakubplichta/grafana-dashboard-builder
https://github.com/Showmax/grafana-dashboards-generator
https://docs.openstack.org/infra/grafyaml
https://docs.saltstack.com/en/latest/ref/states/all/salt.states.grafana4_dashboard.html
![Page 9: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/9.jpg)
https://xkcd.com/927/ Creative Commons Attribution-NonCommercial 2.5 License
![Page 10: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/10.jpg)
Name: "jsonnet", "Open Source": true, License: "Apache License 2.0", Origin: "Google", URL: [ "http://jsonnet.org/", "https://github.com/google/jsonnet", "https://github.com/google/gojsonnet", ], Implementations: ["Golang", "C++"],
![Page 11: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/11.jpg)
JsonnetJsonnet is a domain specific configurationlanguage that helps you define JSON data.
http://jsonnet.org/
Superset of JSON
Functionnal language
![Page 12: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/12.jpg)
Jsonnet, input// Jsonnet Example person1: name: "Alice", welcome: "Hello " + self.name + "!", , person2: self.person1 name: "Bob" ,
http://jsonnet.org/
![Page 13: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/13.jpg)
Jsonnet, output "person1": "name": "Alice", "welcome": "Hello Alice!" , "person2": "name": "Bob", "welcome": "Hello Bob!"
http://jsonnet.org/
![Page 14: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/14.jpg)
CommentsComments do not exist in JSON.
In Jsonnet:
// This./* And this. */
![Page 15: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/15.jpg)
Rules for HumansIn JSON, commas are not allowed at the end ofarrays
In Jsonnet:
[ 'commas', 'are', 'allowed', 'at', 'the', 'end', 'of', 'arrays', ]
![Page 16: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/16.jpg)
SimplicityJson:
"foo": "bar"
Jsonnet:
foo: "bar"
![Page 17: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/17.jpg)
Variableslocal tool_name = "grafana";local modules_total = 10;
tool: tool_name, modules: modules_total,
![Page 18: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/18.jpg)
Functions new(name, kind):: oname: name, kind: kind, max: 10 , foo: $.new("foo", "bar")
![Page 19: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/19.jpg)
Importslocal grafana = import "grafana.libsonnet";local dashboard = grafana.dashboard;
dashboard.new( "Device USE by slot", tags=["Technical", "Overview"],)
![Page 20: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/20.jpg)
stdlibStrings join
Replace
Maps
Inserts
Loops
...
![Page 21: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/21.jpg)
Usagejsonnet foo.jsonnet > foo.json
Multi files:
jsonnet m dashboards dashboards.jsonnet
![Page 22: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/22.jpg)
Style enforcingjsonnet fmt
![Page 23: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/23.jpg)
GrafonnetJsonnet library to build Grafana dashboards
https://github.com/grafana/grafonnet-lib
Same license & rules as Grafana
We have tests! (Not enough docs yet)
![Page 24: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/24.jpg)
Building dashboards withGrafonnet
local grafana = import 'grafonnet/grafana.libsonnet';grafana.dashboard.new( 'JVM', refresh='1m', time_from='now1h', tags=['java']).addTemplate( template.new( 'env', 'Prometheus', 'label_values(jvm_threads_current, env)', label='Environment', refresh='time', ))
![Page 25: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/25.jpg)
// network_group.jsonnet "network.json": import "net/general.jsonnet", "byfamily.json": import "net/byfamily.jsonnet", "byslot.json": import "net/byslot.jsonnet", "bysubslot.json": import "net/bysubslot.jsonnet",
![Page 26: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/26.jpg)
Examplehttps://github.com/grafana/grafonnet-lib/blob/master/examples/jvm.jsonnet
![Page 27: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/27.jpg)
Human readable valuesGrafana expects:
sort: 0
In Grafonnet, you write:
sort: "decreasing"
![Page 28: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/28.jpg)
Going furtherPut YOUR standards on top of Grafonnet:
new(title, uid, tags=[], refresh="1m"):: self + grafana.dashboard.new( title, uid=uid, refresh=refresh, tags=tags ) .addTemplate( grafana.template.datasource( "PROMETHEUS_DS", "prometheus", "Prometheus MyCarenet 1", hide="value", ) ),
![Page 29: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/29.jpg)
Foldersstd.mapWithKey( function(k, v) v // Workaround for grafana/grafana#10895 title: "Customer 1 " + v.title, uid: "cust1" + v.uid, , "jvm.json": local jvm = import "shared/jvm.libsonnet"; jvm.new(datasource_regex="/.*Cust1.*/"), )
![Page 30: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/30.jpg)
Reusing panels from existingdashboards
local existing="existing.json"
grafana.dashboard.new("My Dashboard").addPanels(existing.panels)
![Page 31: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/31.jpg)
Standardize colorsgraphPanel.new( "Frontend Error Rate", fill=8, legend_show=false, min=0, max=0.1, format="percentunit", show_xaxis=false, linewidth=0, decimals=2, datasource=" Mixed ", nullPointMode="null as zero", legend_hideEmpty=true, stack=true, legend_hideZero=true,)+ colors.http
![Page 32: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/32.jpg)
http:: seriesOverrides: [ alias: "/400$/", color: "#629e51", , ]
![Page 33: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/33.jpg)
What if not implemented?template.new( "instance", "$PROMETHEUS_DS", "label_values(upjob='$job', instance)", label="instance", multi=true, includeAll=true, current="all", refresh="load",) + sort: 1
![Page 34: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/34.jpg)
Integration with GrafanaGrafana 5 implements provisioning from files
To be used with jonnet -m (multi)
![Page 35: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/35.jpg)
RoadmapListen to feedback
Implement more feature
Implement a layer abstraction (definedashboards without knowing internals)(opiniated)
Find a way to document it
![Page 36: Grafana + Jsonnet](https://reader031.vdocument.in/reader031/viewer/2022013001/61ca892bf3b4fb0a661cdcba/html5/thumbnails/36.jpg)
Thanks!https://github.com/grafana/grafonnet-lib