Skip to content

Commit

Permalink
Merge pull request #187 from stephenyeargin/fix-infinite-loop
Browse files Browse the repository at this point in the history
Fix various errors
  • Loading branch information
stephenyeargin authored Jun 28, 2024
2 parents 8c0661e + 1d1cdcb commit 5ec4dd1
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 16 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "hubot-grafana",
"description": "Query Grafana dashboards",
"version": "7.0.1",
"version": "7.0.2",
"author": "Stephen Yeargin <[email protected]>",
"license": "MIT",
"keywords": [
Expand Down
56 changes: 44 additions & 12 deletions src/grafana-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,33 @@ const { URL, URLSearchParams } = require('url');

/// <reference path="../types.d.ts"/>

/**
* 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.
Expand Down Expand Up @@ -38,12 +65,7 @@ class GrafanaClient {
* @returns {Promise<unknown>} 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),
Expand All @@ -63,8 +85,7 @@ class GrafanaClient {
* @returns {Promise<unknown>}
*/
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),
Expand All @@ -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;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/service/GrafanaService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -249,6 +249,7 @@ class GrafanaService {
page++;
} catch (err) {
this.logger.error(err, `Error while getting dashboard on URL: ${url}`);
return null;
}
}

Expand Down

0 comments on commit 5ec4dd1

Please sign in to comment.