stephenyeargin / hubot-grafana

📈🤖 Query Grafana dashboards
http://docs.grafana.org/tutorials/hubot_howto/
MIT License
154 stars 48 forks source link

Download method needs API key in header #168

Closed stephenyeargin closed 11 months ago

stephenyeargin commented 11 months ago

From original reported issue:


I am getting these snippets even though grafana is able to render the dashboards. I only get the error when using hubot-grafana

Any ideas how I could debug this?

Everything seems to be ok, I get the correct URL and statusText from Grafana is OK but it returns an HTML instead of the PNG

I tried the same request with CURL and it worked fine

I think I found the problem.

The API key is not being passed to grafana in the header

  /**
   * Downloads the given URL.
   * @param {string} url The URL.
   * @returns {Promise<{ body: Buffer, contentType: string}>}
   */
  async download(url) {

    console.log(grafanaHeaders(null, null))
    return await fetch(url, {
      method: 'GET',
      headers: grafanaHeaders(null, null, "PUT_YOR_TOKEN_HERE), // By default the token is not set in there
    }).then(async (res) => {
      const contentType = res.headers.get('content-type');

      console.log(res.statusText)

      const body = await res.arrayBuffer();

      return {
        body: Buffer.from(body),
        contentType: contentType,
      };
    });
  }

After adding the key there it worked, however by default it is not passed. Is this a bug on your end?

Originally posted by @gkaskonas in https://github.com/stephenyeargin/hubot-grafana/issues/105#issuecomment-1719319806

stephenyeargin commented 11 months ago

That appears to be the case. @KeesCBakker did some much-needed refactoring recently and it may not be passing the API key down where it needs to.

https://github.com/stephenyeargin/hubot-grafana/blob/916417f53a79202b8f8fc41a27af07b3d507f6be/src/grafana-client.js#L103-L120

Adding this.grafana_api_key as the third argument on L110 will likely resolve it.

KeesCBakker commented 11 months ago

We might need to add a unit test so it won't get lost again.

stephenyeargin commented 11 months ago

We might need to add a unit test so it won't get lost again.

Added via 5742eaf3de3fba8fdedb1872cbd035052d86cbe3