From 497aa77e16005eeda35fea1d75d1c22d7f6ebde1 Mon Sep 17 00:00:00 2001 From: "Kees C. Bakker" Date: Mon, 24 Jun 2024 15:40:18 +0200 Subject: [PATCH 1/4] Fix paging and infinite loop when erroring out, --- src/service/GrafanaService.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/service/GrafanaService.js b/src/service/GrafanaService.js index ad95065..c00df8a 100644 --- a/src/service/GrafanaService.js +++ b/src/service/GrafanaService.js @@ -230,7 +230,7 @@ class GrafanaService { let page = 1; while (true) { - const url = `search?limit=${pageSize}&page=${encodeURIComponent(slug)}`; + const url = `search?limit=${pageSize}&page=${encodeURIComponent(page)}`; try { const items = await client.get(url); @@ -249,6 +249,7 @@ class GrafanaService { page++; } catch (err) { this.logger.error(err, `Error while getting dashboard on URL: ${url}`); + return null; } } From 90d8b810defe5234c93f302007e7b60e5fa08c97 Mon Sep 17 00:00:00 2001 From: "Kees C. Bakker" Date: Mon, 24 Jun 2024 15:49:01 +0200 Subject: [PATCH 2/4] Reuse expandUrl for both GET and POST --- src/grafana-client.js | 56 +++++++++++++++++++++++++++++++++---------- 1 file changed, 44 insertions(+), 12 deletions(-) diff --git a/src/grafana-client.js b/src/grafana-client.js index 2695a93..5fb633a 100644 --- a/src/grafana-client.js +++ b/src/grafana-client.js @@ -4,6 +4,33 @@ const { URL, URLSearchParams } = require('url'); /// + /** + * If the given url does not have a host, it will add it to the + * url and return it. + * @param {string} url the url + * @returns {string} the expanded URL. + */ +function expandUrl(url, host) { + + if (url.startsWith('http://') || url.startsWith('https://')) { + return url; + } + + if (!host) { + throw new Error('No Grafana endpoint configured.'); + } + + let apiUrl = host; + if (!apiUrl.endsWith('/')) { + apiUrl += '/'; + } + + apiUrl += 'api/'; + apiUrl += url; + + return apiUrl; +} + class GrafanaClient { /** * Creates a new instance. @@ -38,12 +65,7 @@ class GrafanaClient { * @returns {Promise} the response data */ async get(url) { - if (!url.startsWith('http://') && !url.startsWith('https://') && !this.host) { - throw new Error('No Grafana endpoint configured.'); - } - - const fullUrl = url.startsWith('http://') || url.startsWith('https://') ? url : `${this.host}/api/${url}`; - + const fullUrl = expandUrl(url, this.host); const response = await fetch(fullUrl, { method: 'GET', headers: grafanaHeaders(null, false, this.apiKey), @@ -63,8 +85,7 @@ class GrafanaClient { * @returns {Promise} */ async post(url, data) { - const fullUrl = url.startsWith('http://') || url.startsWith('https://') ? url : `${this.host}/api/${url}`; - + const fullUrl = expandUrl(url, this.host); const response = await fetch(fullUrl, { method: 'POST', headers: grafanaHeaders('application/json', false, this.apiKey), @@ -87,16 +108,27 @@ class GrafanaClient { return; } - if (response.headers.get('content-type') == 'application/json') { - const json = await response.json(); + let contentType = null; + if (response.headers.has('content-type')) { + contentType = response.headers.get('content-type'); + if (contentType.includes(';')) { + contentType = contentType.split(';')[0]; + } + } + if (contentType == 'application/json') { + const json = await response.json(); const error = new Error(json.message || 'Error while fetching data from Grafana.'); error.data = json; throw error; } - const text = await response.text(); - throw new Error(text); + let error = new Error('Error while fetching data from Grafana.'); + if (contentType != 'text/html') { + error.data = await response.text(); + } + + throw error; } /** From ce9e59cc3c166e0c87cb687d2c9676f8c6b5b8be Mon Sep 17 00:00:00 2001 From: "Kees C. Bakker" Date: Mon, 24 Jun 2024 15:57:50 +0200 Subject: [PATCH 3/4] Bump version --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index 04eeb09..1480161 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "hubot-grafana", "description": "Query Grafana dashboards", - "version": "7.0.1", + "version": "7.0.2", "author": "Stephen Yeargin ", "license": "MIT", "keywords": [ From 1d1cdcb338d495949eef6b023819e8af5d942802 Mon Sep 17 00:00:00 2001 From: "Kees C. Bakker" Date: Mon, 24 Jun 2024 16:01:33 +0200 Subject: [PATCH 4/4] Install package: add lock --- package-lock.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package-lock.json b/package-lock.json index 6081a4b..2bd4b64 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "hubot-grafana", - "version": "7.0.1", + "version": "7.0.2", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "hubot-grafana", - "version": "7.0.1", + "version": "7.0.2", "license": "MIT", "dependencies": { "@aws-sdk/client-s3": "^3.565.0",