Files
cloudron-box/src/routes/graphs.js
T

39 lines
1.5 KiB
JavaScript
Raw Normal View History

'use strict';
exports = module.exports = {
2021-01-21 12:53:38 -08:00
getGraphs
};
var middleware = require('../middleware/index.js'),
2019-09-05 12:01:25 -07:00
HttpError = require('connect-lastmile').HttpError,
url = require('url');
2019-08-20 15:26:00 -07:00
// for testing locally: curl 'http://127.0.0.1:8417/graphite-web/render?format=json&from=-1min&target=absolute(collectd.localhost.du-docker.capacity-usage)'
2019-08-20 19:47:24 -07:00
// the datapoint is (value, timestamp) https://buildmedia.readthedocs.org/media/pdf/graphite/0.9.16/graphite.pdf
2019-08-20 15:26:00 -07:00
const graphiteProxy = middleware.proxy(url.parse('http://127.0.0.1:8417'));
function getGraphs(req, res, next) {
var parsedUrl = url.parse(req.url, true /* parseQueryString */);
delete parsedUrl.query['access_token'];
delete req.headers['authorization'];
delete req.headers['cookies'];
2019-08-12 11:01:12 -07:00
// 'graphite-web' is the URL_PREFIX in docker-graphite
req.url = url.format({ pathname: 'graphite-web/render', query: parsedUrl.query });
2019-02-18 13:13:29 +01:00
// graphs may take very long to respond so we run into headers already sent issues quite often
// nginx still has a request timeout which can deal with this then.
req.clearTimeout();
2019-09-05 12:01:25 -07:00
graphiteProxy(req, res, function (error) {
if (!error) return next();
if (error.code === 'ECONNREFUSED') return next(new HttpError(424, 'Unable to connect to graphite'));
// ECONNRESET here is most likely because of a bug in the query or the uwsgi buffer size is too small
if (error.code === 'ECONNRESET') return next(new HttpError(424, 'Unable to query graphite'));
next(new HttpError(500, error));
});
}