Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Grafonnet, grafana dashboards as code

1,428 views

Published on

This is a talk about Grafonnet, a Jsonnet library to design grafana dashboards. The talk was given at GrafanaCon 2018 in Amsterdam.

Published in: Technology
  • Be the first to comment

Grafonnet, grafana dashboards as code

  1. 1. Grafana + Jsonnet Julien Pivotto (@roidelapluie) GrafanaCon 2018
  2. 2. {     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,         },     }, }
  3. 3. {    "company": "Inuits",    "grafana": {       "first_issue": {          "date": "Apr 14, 2014",          "id": 311       },       "first_pull": {          "date": "Apr 14, 2014",          "id": 310       }    },    "name": "Julien Pivotto" }
  4. 4. Grafana Dashboards JSON Templates Annotations Panels Links
  5. 5. Grafana at scale Plenty of dashboards Consistency of templates Same links Same annotations Same panels
  6. 6. Panels Consistency Same color for given error code Same color for given server Same rules (stacked, lines, bars, width. datasource)
  7. 7. 2 ways of solving that Spending way to much time building and correcting dashboards in the grafana UI Dashboards as code
  8. 8. Dashboards as Code https://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/stat es/all/salt.states.grafana4_dashboard.html
  9. 9. https://xkcd.com/927/ Creative Commons Attribution-NonCommercial 2.5 License
  10. 10. {     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/go­jsonnet",     ],     Implementations: ["Golang", "C++"], }
  11. 11. Jsonnet Jsonnet is a domain specific configuration language that helps you define JSON data. http://jsonnet.org/ Superset of JSON Functional language
  12. 12. Jsonnet, input // Jsonnet Example {     person1: {         name: "Alice",         welcome: "Hello " + self.name + "!",     },     person2: self.person1 { name: "Bob" }, } http://jsonnet.org/
  13. 13. Jsonnet, output {    "person1": {       "name": "Alice",       "welcome": "Hello Alice!"    },    "person2": {       "name": "Bob",       "welcome": "Hello Bob!"    } } http://jsonnet.org/
  14. 14. Comments Comments do not exist in JSON. In Jsonnet: // This. /* And this. */
  15. 15. Rules for Humans In JSON, commas are not allowed at the end of arrays In Jsonnet: [ 'commas', 'are', 'allowed',   'at', 'the', 'end', 'of', 'arrays', ]
  16. 16. Simplicity Json: {"foo": "bar"} Jsonnet: {foo: "bar"}
  17. 17. Variables local tool_name = "grafana"; local modules_total = 10; {     tool: tool_name,     modules: modules_total,  }
  18. 18. Functions {   new(name, kind):: {     oname: name,     kind: kind,     max: 10   },   foo: $.new("foo", "bar") }
  19. 19. Imports local grafana = import "grafana.libsonnet"; local dashboard = grafana.dashboard; dashboard.new(     "Device USE by slot",     tags=["Technical", "Overview"], )
  20. 20. stdlib Strings join Replace Maps Inserts Loops ...
  21. 21. Usage jsonnet  foo.jsonnet > foo.json Multi files: jsonnet ­m dashboards dashboards.jsonnet
  22. 22. Style enforcing jsonnet fmt
  23. 23. Grafonnet Jsonnet library to build Grafana dashboards https://github.com/grafana/grafonnet-lib Same license & rules as Grafana We have tests! (Not enough docs yet) Store dashboards in SCM, but easy to diff
  24. 24. Building dashboards with Grafonnet local grafana = import 'grafonnet/grafana.libsonnet'; grafana.dashboard.new(     'JVM',     refresh='1m',     time_from='now­1h',     tags=['java'] ) .addTemplate(     template.new(         'env',         'Prometheus',         'label_values(jvm_threads_current, env)',         label='Environment',         refresh='time',     ) )
  25. 25. // network_group.jsonnet {     "network.json":         import "net/general.jsonnet",     "by­family.json":         import "net/by­family.jsonnet",     "by­slot.json":         import "net/by­slot.jsonnet",     "by­subslot.json":         import "net/by­subslot.jsonnet", }
  26. 26. Example https://github.com/grafana/grafonnet- lib/blob/master/examples/jvm.jsonnet
  27. 27. Human readable values Grafana expects: { sort: 0 } In Grafonnet, you write: { sort: "decreasing" }
  28. 28. Going further Put 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 Customer 1",                 hide="value",             )         ), }
  29. 29. Folders std.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.*/"),             } )
  30. 30. Reusing panels from existing dashboards local upstreamMysql = import "mysql.json"; grafana.dashboard.new("My Dashboard") .addPanels(upstreamMysql.panels)
  31. 31. Standardize colors graphPanel.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
  32. 32. {     http::         {             seriesOverrides: [                 {                     alias: "/400$/",                     color: "#629e51",                 },             ]         } }
  33. 33. What if not implemented? template.new(     "instance",     "$PROMETHEUS_DS",     "label_values(up{job='$job'}, instance)",     label="instance",     multi=true,     includeAll=true,     current="all",     refresh="load", ) + { sort: 1 }
  34. 34. Integration with Grafana We support Grafana 5 Grafana 5 implements provisioning from files To be used with jsonnet -m (multi)
  35. 35. Roadmap Listen to feedback Implement more feature Implement a layer abstraction (define dashboards without knowing internals) (opiniated) Find a way to document it
  36. 36. Thanks! https://github.com/grafana/grafonnet-lib

×