bbgobie
bbgobie

Reputation: 31

No Host in request URL for Grafana datasource plugin tutorial - Add authentication

I'm trying to follow the example for developing a datasource plugin from Grafana. Ultimately I want my plugin to use Oauth, but even with just the basic Grafana datasource proxy example I seem to be having issues. I have updated my plugin.json, class and constructor.

I have setup this hard coded example.

in plugin.json

      {
        "path": "grafana",
        "url": "https://github.com"
      }
    ],

And a sample testDataSource()

  async testDatasource() {
    return getBackendSrv()
      .datasourceRequest({
        url: this.url + '/grafana/grafana',
        method: 'GET',
      })
      .then(response => {
        if (response.status === 200) {
          return { status: 'success', message: 'Data source is working', title: 'Success' };
        } else {
          return { status: 'failure', message: 'Data source is not working: ' + response.status, title: 'Failure' };
        }
      });
  }

When I try and save/test this datasource to call that method, I get in the frontend a HTTP Error Bad Gateway And in the logs

t=2021-09-17T14:31:22+0000 lvl=eror msg="Data proxy error" logger=data-proxy-log userId=1 orgId=1 uname=admin path=/api/datasources/proxy/9/grafana/grafana remote_addr=172.17.0.1 referer=http://localhost:3000/datasources/edit/9/ error="http: proxy error: http: no Host in request URL"

I would've expected the request to be routed to the datasource proxy and for that to make the request to github but it seems Grafana is making a request to /api/datasources/proxy/9/grafana/grafana and nothing is picking it up?

Upvotes: 1

Views: 1486

Answers (1)

bbgobie
bbgobie

Reputation: 31

Looking up my datasource via API, there's nothing listed for URL. You will need to render this in your ConfigEditor.tsx

<DataSourceHttpSettings
  defaultUrl="http://localhost:8080"
  dataSourceConfig={options}
  onChange={onOptionsChange}
/>

Which will give you the basic form with URL, whitelist, auth options that you see on most plugins. The URL there I guess should match what you have in your routes.

Upvotes: 0

Related Questions