weaveworks / grafanalib

Python library for building Grafana dashboards
Apache License 2.0
1.86k stars 309 forks source link

AlertList compatibility with grafana 9.x #585

Open tporeba opened 1 year ago

tporeba commented 1 year ago

I am trying to use grafanalib with grafana 9.4 and I noticed that grafanalib.core.AlertList component produces definition of alertlist panel in old json format. The panel gets created, but some properties end up with wrong values and are ignored, for example : stateFilter or onlyAlertsOnDashboard.

If I understand this note correctly, grafanalib assumes that grafana will migrate from those old json formats to newest formats when dashboard is being imported, but this does not happen for me. I'm not sure if this is some configuration issue or if the new grafana no longer does the migration.

What you expected to happen?

grafanalib.core.AlertList.to_json_data should produce a json compatible with recent grafana versions.

What happened?

For

AlertList(title="Panel Title",
             onlyAlertsOnDashboard=True,
             stateFilter=[grafanalib.core.ALERTLIST_STATE_PENDING],
             gridPos=GridPos(h=8, w=24, x=0, y=0),
             )

Grafanalib generates following AlertList a json with AlertList panel, it generates a json like this one:

{
      "dashboardTags": [],
      "description": "",
      "gridPos": {
        "h": 8,
        "w": 24,
        "x": 0,
        "y": 0
      },
      "id": 2,
      "limit": 10,
      "links": [],
      "nameFilter": "",
      "onlyAlertsOnDashboard": true,
      "options": {
        "alertName": ""
      },
      "show": "current",
      "sortOrder": 1,
      "span": 6,
      "stateFilter": [
        "pending"
      ],
      "title": "Panel Title",
      "type": "alertlist"
    }

but after importing this in grafana, panel has default settings in 'Alerts from this dashboard' and 'Alert state filter'. image image

If following json was generated, the import would work ok:

    {
      "gridPos": {
        "h": 8,
        "w": 24
        "x": 0,
        "y": 0
      },
      "id": 2,
      "options": {
        "alertInstanceLabelFilter": "",
        "alertName": "",
        "dashboardAlerts": true,
        "groupBy": [],
        "groupMode": "default",
        "maxItems": 20,
        "sortOrder": 1,
        "stateFilter": {
          "error": false,
          "firing": false,
          "noData": false,
          "normal": false,
          "pending": true
        },
        "viewMode": "list"
      },
      "title": "Panel Title",
      "type": "alertlist"
    }

image image

How to reproduce it?

Generate a dashboard with AlertList panel from grafanalib and install it in grafana

AlertList(title="Panel Title",
             onlyAlertsOnDashboard=True,
             stateFilter=[grafanalib.core.ALERTLIST_STATE_PENDING],
             gridPos=GridPos(h=8, w=24, x=0, y=0),
             )

or use one of the following example dashboard jsons for manual test:

steps: 1) In grafana 9.4 import them manually: Dashboards -> Import -> upload json -> Import 2) Go to imported dashboard and click on panel title -> edit -> scroll down to 'Alerts from this dashboard' and 'Alert state filter' options

tporeba commented 1 year ago

It is quite easy to reproduce using grafana docker image + those two json files provided above in the description:

docker run --rm -p 3000:3000 --name grafana grafana/grafana-oss:<version>
grafana image version old Format new format
6.0.0
7.0.0
8.0.0
9.0.0
9.4.0