[Cache | Trunk]: Fix some issues on metrics. Remove encode url

git-svn-id: https://svn.driver.research-infrastructures.eu/driver/dnet40/modules/uoa-services-portal/trunk@58495 d315682c-612b-4755-9ff5-7f18f6832af3
This commit is contained in:
k.triantafyllou 2020-04-15 13:53:37 +00:00
parent eabd920aa2
commit 86c65dbd81
1 changed files with 34 additions and 24 deletions

View File

@ -15,11 +15,11 @@ prom.collectDefaultMetrics({register: register});
const responses = new prom.Counter({ const responses = new prom.Counter({
name: 'cache_http_responses_total', name: 'cache_http_responses_total',
help: 'A counter for cache response codes for every API request.', help: 'A counter for cache response codes for every API request.',
labelNames: ['scheme', 'target', 'code', 'cache'], labelNames: ['scheme', 'target', 'code'],
registers: [register] registers: [register]
}); });
const entries = new prom.Counter({ const entries = new prom.Gauge({
name: 'cache_used_entries', name: 'cache_used_entries',
help: 'A counter to count cache entries', help: 'A counter to count cache entries',
registers: [register] registers: [register]
@ -28,34 +28,44 @@ const entries = new prom.Counter({
const histogram = new prom.Histogram({ const histogram = new prom.Histogram({
name: 'cache_http_request_duration_seconds', name: 'cache_http_request_duration_seconds',
help: 'A Histogram for cache. Providing information about a cache request and load latency in seconds.', help: 'A Histogram for cache. Providing information about a cache request and load latency in seconds.',
labelNames: ['scheme', 'target'], labelNames: ['scheme', 'target', 'cache'],
registers: [register], registers: [register],
buckets: [0.1, 0.2, 0.5, 1, 2] buckets: [0.1, 0.2, 0.5, 1, 2]
}); });
let cache = (duration) => { let cache = (duration) => {
return (req, res, next) => { return (req, res, next) => {
let key = '__express__' + req.originalUrl || req.url; if(req.query.url) {
let cachedBody = mcache.get(key); let key = '__express__' + req.query.url;
const url = new URL(req.query.url); let cachedBody = mcache.get(key);
const target = url.host + '/' + url.pathname.split('/')[1]; const url = new URL(req.query.url);
const scheme = url.protocol.replace(':', ''); const target = url.host + '/' + url.pathname.split('/')[1];
const end = histogram.startTimer({scheme: scheme, target: target}); const scheme = url.protocol.replace(':', '');
entries.inc(); if (cachedBody) {
if (cachedBody) { const end = histogram.startTimer({scheme: scheme, target: target, cache: 'hit'});
res.send(JSON.parse(cachedBody)); res.send(JSON.parse(cachedBody));
responses.inc({scheme: scheme, target: target, code: res.statusCode, cache: 'hit'}); responses.inc({scheme: scheme, target: target, code: res.statusCode});
end(); end();
} else {
const end = histogram.startTimer({scheme: scheme, target: target, cache: 'miss'});
res.sendResponse = res.send;
res.send = (body) => {
let alreadyCached = !!mcache.get(key);
entries.set(mcache.size());
if(!alreadyCached) {
responses.inc({scheme: scheme, target: target, code: res.statusCode});
end();
}
if (res.statusCode === 200) {
mcache.put(key, body, duration * 1000, () => {
entries.set(mcache.size());
});
}
res.sendResponse(body);
};
next();
}
} else { } else {
end();
res.sendResponse = res.send;
res.send = (body) => {
if (res.statusCode === 200) {
mcache.put(key, body, duration * 1000);
}
res.sendResponse(body);
responses.inc({scheme: scheme, target: target, code: res.statusCode, cache: 'miss'});
};
next(); next();
} }
}; };
@ -73,7 +83,7 @@ app.get('/get', cache(30), cors(), (req, res) => {
if (!url) { if (!url) {
res.status(404).send(getResponse(404, "Not Found ")) //not found res.status(404).send(getResponse(404, "Not Found ")) //not found
} else { } else {
request.get(encodeURI(url), function (err, response) { request.get(url, function (err, response) {
// res.header("Access-Control-Allow-Origin", "http://localhost:3000"); // 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-Headers", "Origin, Content-Type, Content-Length");
res.header("Access-Control-Allow-Methods", "GET, OPTIONS"); res.header("Access-Control-Allow-Methods", "GET, OPTIONS");