brancz / kubernetes-grafana

The future of Grafana on Kubernetes with Prometheus.
Other
236 stars 51 forks source link

Simple dashboard sanity checks #96

Open maxbrunet opened 4 years ago

maxbrunet commented 4 years ago

Not sure if this can be part of this project, I just want to share this somewhere.

It can be quiet cumbersome to check for things like unique UID and titles, so I integrated the logic in Jsonnet, and then generating the deployment simply fails if the condition are not met.

local folderDashboards = {
  // Example
  AWS: import 'aws/folder.libsonnet',
  Datastores: import 'datastores/folder.libsonnet',
  Kubernetes: import 'kubernetes/folder.libsonnet',
  myServices: import 'myServices/folder.libsonnet',
  System: import 'system/folder.libsonnet',
};

local titles = [
  folderDashboards[folder][filename].title
  for folder in std.objectFields(folderDashboards)
  for filename in std.objectFields(folderDashboards[folder])
];

local filenames = [
  filename
  for folder in std.objectFields(folderDashboards)
  for filename in std.objectFields(folderDashboards[folder])
];

local isTitleUnique(title) =
  if std.count(titles, title) > 1 then
    error 'Title "%s" is used more than once' % [title]
  else true;

local isFilenameUnique(filename) =  // Means UID will be unique
  if std.count(filenames, filename) > 1 then
    error 'Filename "%s" is used more than once' % [filename]
  else true;

{
  [folder]: {
    [filename]: folderDashboards[folder][filename] {
      uid:
        // Checks need to be included into a non-hidden field
        // else they would never be evaluated
        if isFilenameUnique(filename) && isTitleUnique(self.title) then
          // We could hash folder+filename, but this would break
          // the dashboard URL if it is moved
          std.md5(filename),
    }
    for filename in std.objectFields(folderDashboards[folder])
  }
  for folder in std.objectFields(folderDashboards)
}

Every dashboard needs a UID, so the checks are called while setting it.

and then, pass the output to _config.grafana.folderDashboards.

brancz commented 4 years ago

I think this is really amazing yes! I feel maybe it could be more suited as part of the grafonnet project, but if they don't want to accept this I think I'd be willing to have it here! :)