grafana / grafonnet-lib

Jsonnet library for generating Grafana dashboard files.
https://grafana.github.io/grafonnet-lib/
Apache License 2.0
1.08k stars 217 forks source link

tablePanel generates bad target refIds #161

Open adam-j-turner opened 4 years ago

adam-j-turner commented 4 years ago

The first target refId generated by tablePanel is 'A' but the rest are unexpected unicode characters.

Example jsonnet -

local grafana = import 'grafonnet/grafana.libsonnet';
local cloudwatch = grafana.cloudwatch;
local dashboard = grafana.dashboard;
local tablePanel = grafana.tablePanel;

local functionNames = ['function-' + i for i in std.range(1,5)];

local targets = [
  cloudwatch.target(
    region     = 'us-west-1',
    namespace  = 'AWS/Lambda',
    metric     = 'Duration',
    statistic  = 'Average',
    dimensions = {'FunctionName': f}
  ) for f in functionNames
];

tablePanel.new(
  title = 'Test',
).addTargets(targets)

Output -

{
   "columns": [ ],
   "datasource": null,
   "styles": [ ],
   "targets": [
      {
         "dimensions": {
            "FunctionName": "function-1"
         },
         "highResolution": false,
         "metricName": "Duration",
         "namespace": "AWS/Lambda",
         "period": "1m",
         "refId": "A",
         "region": "us-west-1",
         "statistics": [
            "Average"
         ]
      },
      {
         "dimensions": {
            "FunctionName": "function-2"
         },
         "highResolution": false,
         "metricName": "Duration",
         "namespace": "AWS/Lambda",
         "period": "1m",
         "refId": "\u0083",
         "region": "us-west-1",
         "statistics": [
            "Average"
         ]
      },
      {
         "dimensions": {
            "FunctionName": "function-3"
         },
         "highResolution": false,
         "metricName": "Duration",
         "namespace": "AWS/Lambda",
         "period": "1m",
         "refId": "Å",
         "region": "us-west-1",
         "statistics": [
            "Average"
         ]
      },
      {
         "dimensions": {
            "FunctionName": "function-4"
         },
         "highResolution": false,
         "metricName": "Duration",
         "namespace": "AWS/Lambda",
         "period": "1m",
         "refId": "ć",
         "region": "us-west-1",
         "statistics": [
            "Average"
         ]
      },
      {
         "dimensions": {
            "FunctionName": "function-5"
         },
         "highResolution": false,
         "metricName": "Duration",
         "namespace": "AWS/Lambda",
         "period": "1m",
         "refId": "ʼn",
         "region": "us-west-1",
         "statistics": [
            "Average"
         ]
      }
   ],
   "timeFrom": null,
   "timeShift": null,
   "title": "Test",
   "type": "table"
}
les commented 3 years ago

+1

This bug is related to the following line of code, which is repeated for all panel types:

targets+: [target { refId: std.char(std.codepoint('A') + nextTarget) }],

Once the list of targets exceeds 26 the refId no longer validates against ^[a-z][a-zA-Z0-9_]*$, which results in the following exception:

"ValidationError: The value query<char> for parameter MetricDataQueries.member.5.Id is not matching the expected pattern ^[a-z][a-zA-Z0-9_]*$.
The value query<char> for parameter MetricDataQueries.member.6.Id is not matching the expected pattern ^[a-z][a-zA-Z0-9_]*$.

My quick 'n dirty patch goes something like this:

     // targets
-    _nextTarget:: 0,
+    _nextTarget:: 1,
     addTarget(target):: self {
       local nextTarget = super._nextTarget,
       _nextTarget: nextTarget + 1,
-      targets+: [target { refId: std.char(std.codepoint('A') + nextTarget) }],
+      targets+: [target { refId: 'query_' + nextTarget }],
     },
     addTargets(targets):: std.foldl(function(p, t) p.addTarget(t), targets, self),