Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix various errors #187

Merged
merged 4 commits into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading