"datasource missing ID" when datasource is not default

Hello,

I had a custom React-based datasource plugin fetching metrics from my company’s servers. Recently, I’ve updated the plugin to use more native Grafana functions. For example, the old code used to look like this:

export class DataSource extends DataSourceApi<...> {
  ...
  async query(options): {
    ...
    return await this.backendSrv.datasourceRequest(options).then(handleTsdbResponse);
  }
}

where I would add some additional information to the targets in the options. Now, I have changed it to look like this:

export class DataSource extends DataSourceWithBackend<...> {
  ...
  async query(options): {
    ...
    return super.query(options);
  }
}

The problem is as follows: When the datasource is not marked as “default” in its config page, once I send the query I get an error “datasource missing ID” in my chart’s upper left corner. Indeed, I can see that the “query” function gets the datasource name passed as an argument, but:

  • when the DS is default, the payload sent over the network includes a “datasourceId” in its queries along with the custom parameters I add.
  • otherwise, the queries only have the custom params.

My guess is that the datasource name gets mapped to an ID somewhere in Grafana when it’s default, but the mapping fails when it isn’t. I have tried manually adding the “datasourceId” field, but it gets deleted before the request is sent over the network.

Can someone assist me on this? I tested it with Grafana 7.0.0 and 7.0.5.

Best regards,
A.

I have found a fix for that:

The options passed to

super.query(options)

need to contain the datasource name in each target, like this:

{
  datasource: target.datasouce,
  customOption1: value1,
  ...
}

for a target type that extends DataQuery from @grafana/data with custom options.

As per Grafana 7.1.0-beta2, this has been fixed in the native Grafana code as well (see this commit).