1

I'm trying to write a custom data source plugin for Grafana that would request Azure AD authentication tokens and send them along with the queries to my database which will accept the token and return the response to the query.

I've noticed that the Azure Monitor Plugin for Grafana does the same by asking the user to enter their client id, client secret and tenant id and using it via the routes{} part of their plugin.json file.

I have followed this method but I get an error:

502 bad gateway error.

My files are hosted here

The essential part of my datasource.js that makes the HTTP call is

query(options) {

    const csl = document.getElementById("csl").value;
    var queries = _.filter(options.targets, item => {
        return item.hide !== true;
      }).map(item => {
        return {
          refId: item.refId,
          intervalMs: options.intervalMs,
          maxDataPoints: options.maxDataPoints,
          format: item.format,
        };
      });
    if (queries.length <= 0) {
      return this.$q.when({data: []});
    }
    return this.backendSrv.datasourceRequest({
        url: `api/datasources/proxy/${this.id}/kusto/query`,
        method: 'POST',
        headers: this.headers,
        data: {
            db: this.database,
            csl: csl,
            from: options.range.from,
            to: options.range.to,
            queries: queries,
        }
    });
}

Where kusto is the routes path defined in my plugin.json.

What is causing this error? Is there a mistake in my datasource.js or my plugin.json? Is the error happening client side or server side?

Prashant Pimpale
  • 10,349
  • 9
  • 44
  • 84

1 Answers1

1

The first thing is that there is now a Kusto (now renamed to Azure Data Explorer) Datasource for Grafana. So not sure you need your plugin anymore.

The reason for your error is that your route in the plugin.json file is not matching the calls you are making. You are making the ping request is a HTTP GET but the route matches POST requests.

The error handling in Grafana could definitely be better here - in the logs you will see an error that says:

http: proxy error: unsupported protocol scheme ""

The URL field gets set to be an empty string due to not finding a match in the plugin routes. Then when the datasource proxy in Grafana tries to create a url to send to Azure, it fails as there is no protocol (http or https) specified.

Documentation for Plugin Routes and Authentication: http://docs.grafana.org/plugins/developing/auth-for-datasources/

Daniel Lee
  • 7,709
  • 2
  • 48
  • 57