Grafana as code provisioned dashboard do not recognize datasource

Hello, I’m using Grafana OSS 9.2.4 with the Prometheus plugin 5.0.0 that ships with Grafana, on Linux.

I’m trying to provision my data sources and dashboards as code and I did the following:

  1. From a working instance, exported the Dashboard json (marked the option ‘Export for sharing externally’) and save it to a file
  2. On a new instance defined the data source as a YAML file, confirmed the datasource works on the new instance. it has the same name as the data source originally created by hand on the working instance.
  3. Deployed the dashboard JSON on the provisioning directory and see than the new dashboard is accepted by Grafana (all panels are there) but every single one of them is empty.

If I edit the new dashboard on the new server and change the data source by hand (and put back the query) then the panel works.

Somehow I think I missed an identifier that links the exported JSON with the new YAML data source definition.

My new Data source looks like this:

apiVersion: 1
datasources:
  - name: Prometheus
    type: prometheus
    url: http://server.example.com:9090
    access: proxy
    editable: false
    version: 1
    jsonData:
      manageAlerts: true
      # prometheusType: Prometheus
      # prometheusVersion: 2.40.0
      httpMethod: POST

And the Dashboard JSON “seems” to have the same name:

{
  "__inputs": [
    {
      "name": "DS_PROMETHEUS",
      "label": "Prometheus",
      "description": "",
      "type": "datasource",
      "pluginId": "prometheus",
      "pluginName": "Prometheus"
    }
  ],
  "__elements": {},
  "__requires": [
    {
      "type": "panel",
      "id": "bargauge",
      "name": "Bar gauge",
      "version": ""
    },
    {
      "type": "grafana",
      "id": "grafana",
      "name": "Grafana",
      "version": "9.2.4"
    },
    {
      "type": "panel",
      "id": "piechart",
      "name": "Pie chart",
      "version": ""
    },
    {
      "type": "datasource",
      "id": "prometheus",
      "name": "Prometheus",
      "version": "1.0.0"
    },
...

I don’t see any errors on the logs, did I missed any steps from the administration provisioning guide?

Thanks in advance.

1 Like

I did the following on the Grafana Dashboard JSON file:

  • Replace all the occurrences of DS_PROMETHEUS and DS_PROMETHEUS with the name I have on the provisioning file: Prometheus

The original JSON file looks like this:

{
  "__inputs": [
    {
      "name": "DS_PROMETHEUS",
      "name": "Prometheus",
      "label": "Prometheus",
      "description": "",
      "type": "datasource",
      "pluginId": "prometheus",
      "pluginName": "Prometheus"
    }
  ],
  "__elements": {},
  "__requires": [
    {
          },
          "refId": "A"
        }
      ],
      "title": "Quick CPU / Mem / Disk",
      "type": "row"
    },
    {
      "datasource": {
        "type": "prometheus",
        "uid": "${DS_PROMETHEUS}"
        "uid": "Prometheus"
      },

After that change, Grafana increased the dashboard version automatically and I was able to see the data.

Is this the recommended approach?

More problems: This issue also affects Dashboard constants, they do loose their variable and only the placeholder is kept:

    {
      "name": "VAR_DAY_IN_MILLIS",
      "type": "constant",
      "label": "Day in milliseconds",
      "value": "${VAR_DAY_IN_MILLIS}",
      "description": ""
    }

The solution to this problem is a 2 step process:

  1. Make sure the provisioned dashboard is exported without the ‘export externally’ option toggled on. That wipes out all UID information and makes it harder to relink to your provisioned data source
  2. Your data source YAML file must have an uid, same value used on the dashboard:
apiversion: 1
datasources:
  - name: Dist
    type: mysql
    orgId: 1
    url: host.example.com:3306
    database: dist
    user: XXX
    editable: false
    version: 5
    uid: "8rTXXfF4z"
    secureJsonData:
      password: YYYY
    jsonData:
      sslmode: 'disable'
      maxOpenConns: 0
      maxIdleConns: 2
      connMaxLifetime: 14400
      timescaledb: false
1 Like