[Cache | Trunk]: Add new metrics, encode url

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-portal/trunk@58478 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
k.triantafyllou 2020-04-10 14:29:46 +00:00
parent 41dc9ca092
commit eabd920aa2
1 changed files with 23 additions and 15 deletions

View File

@ -10,37 +10,51 @@ let cors = require('cors');
app.use(cors());
const register = new prom.Registry();
prom.collectDefaultMetrics({register: register});
const responses = new prom.Counter({
name: 'cache_http_responses_total',
help: 'A counter for cache response codes for every API request.',
labelNames: ['url', 'code'],
labelNames: ['scheme', 'target', 'code', 'cache'],
registers: [register]
});
const entries = new prom.Counter({
name: 'cache_used_entries',
help: 'A counter to count cache entries',
registers: [register]
});
const histogram = new prom.Histogram({
name: 'cache_http_request_duration_seconds',
help: 'A Histogram for cache. Providing information about API request and load latency in seconds.',
labelNames: ['url'],
help: 'A Histogram for cache. Providing information about a cache request and load latency in seconds.',
labelNames: ['scheme', 'target'],
registers: [register],
buckets: [0.1, 0.2, 0.5, 1, 2]
});
let cache = (duration) => {
return (req, res, next) => {
let key = '__express__' + req.originalUrl || req.url
console.log(req.originalUrl || req.url);
let key = '__express__' + req.originalUrl || req.url;
let cachedBody = mcache.get(key);
const url = new URL(req.query.url);
const target = url.host + '/' + url.pathname.split('/')[1];
const scheme = url.protocol.replace(':', '');
const end = histogram.startTimer({scheme: scheme, target: target});
entries.inc();
if (cachedBody) {
res.send(JSON.parse(cachedBody))
res.send(JSON.parse(cachedBody));
responses.inc({scheme: scheme, target: target, code: res.statusCode, cache: 'hit'});
end();
} else {
end();
res.sendResponse = res.send;
res.send = (body) => {
if (res.statusCode === 200) {
mcache.put(key, body, duration * 1000);
}
res.sendResponse(body)
res.sendResponse(body);
responses.inc({scheme: scheme, target: target, code: res.statusCode, cache: 'miss'});
};
next();
}
@ -57,10 +71,9 @@ app.get('/get', cache(30), cors(), (req, res) => {
setTimeout(() => {
const url = (req.query) ? req.query.url : null;
if (!url) {
responses.inc({url: null, code: 404});
res.status(404).send(getResponse(404, "Not Found ")) //not found
} else {
request.get(url, function (err, response) {
request.get(encodeURI(url), function (err, response) {
// res.header("Access-Control-Allow-Origin", "http://localhost:3000");
res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Content-Length");
res.header("Access-Control-Allow-Methods", "GET, OPTIONS");
@ -69,12 +82,7 @@ app.get('/get', cache(30), cors(), (req, res) => {
if (!response && err) {
res.status(500).send(getResponse(500, "An error occurred for " + url))
} else {
const end = histogram.startTimer({url: url});
res.status(response.status).send(response.body);
res.on('finish', function () {
responses.inc({url: url, code: res.statusCode});
end();
});
}
})
}