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

Simple dashboard sanity checks #96

Open
maxbrunet opened this issue Aug 10, 2020 · 1 comment
Open

Simple dashboard sanity checks #96

maxbrunet opened this issue Aug 10, 2020 · 1 comment

Comments

@maxbrunet
Copy link
Contributor

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
Copy link
Owner

brancz commented Aug 10, 2020

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! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants