grafonnet icon indicating copy to clipboard operation
grafonnet copied to clipboard

lack of properties in dashboard.annotation

Open prudnitskiy opened this issue 5 months ago • 0 comments

Hello Grafana team,

we're trying to implement grafana annotations with grafonnet using google BigQuery as a data source. Looks the annotation parts lacks of critical properties/fields needed for annotations to be populated. Here an example:

local g = import '../g.libsonnet';

local datasource_type = 'grafana-bigquery-datasource';
local datasource_uid = std.extVar('datasource_uid');
local annotations_sql = importstr './sql/annotations-events.sql';

// datasource
local grafana_annotation_datasource = (
  datasource.withType(datasource_type)
  + datasource.withUid(datasource_uid)
);

//dashboard
g.dashboard.new('HTTP Service Monitoring%s' % title_suffix)
// [non-related part skipped]
+ g.dashboard.withAnnotations([
  g.dashboard.annotation.withName('Events')
  + g.dashboard.annotation.withBuiltIn(value=0)
  + g.dashboard.annotation.withIconColor(value='blue')
  + g.dashboard.annotation.withEnable(value=true)
  + g.dashboard.annotation.withHide(value=false)
  + grafana_annotation_datasource
  + g.dashboard.annotation.withExpr(annotations_sql)
  + g.dashboard.annotation.withTarget()
])

The resulting code will be rendered and applied without issues. However, there is no mapping field in the document. It would also not have a target object. Without the target object:

  • There is no way to edit annotations in the editor, the query returns an empty result, and the code editor is blank
  • Events will not be displayed in the dashboard

This can be fixed by providing a target object manually in JSON Model. Here's an example:

"target":
{
    "editorMode": "code",
    "format": 1,
    "location": "XXXX",
    "project": "YYY",
    "rawQuery": true,
    "rawSql": "-- same query as for .expr field in the annotation definition",
    "refId": "Anno",
    "sql":
    {
        "columns":
        [
            {
                "parameters":
                [],
                "type": "function"
            }
        ],
        "groupBy":
        [
            {
                "property":
                {
                    "type": "string"
                },
                "type": "groupBy"
            }
        ],
        "limit": 50
    }
}

That's the only change required in JSON model.

Is there any fix already exists or is it a known limitation?

prudnitskiy avatar May 07 '25 15:05 prudnitskiy