[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:
parent
41dc9ca092
commit
eabd920aa2
|
@ -10,37 +10,51 @@ let cors = require('cors');
|
||||||
app.use(cors());
|
app.use(cors());
|
||||||
|
|
||||||
const register = new prom.Registry();
|
const register = new prom.Registry();
|
||||||
|
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: ['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]
|
registers: [register]
|
||||||
});
|
});
|
||||||
|
|
||||||
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 API request and load latency in seconds.',
|
help: 'A Histogram for cache. Providing information about a cache request and load latency in seconds.',
|
||||||
labelNames: ['url'],
|
labelNames: ['scheme', 'target'],
|
||||||
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
|
let key = '__express__' + req.originalUrl || req.url;
|
||||||
console.log(req.originalUrl || req.url);
|
|
||||||
let cachedBody = mcache.get(key);
|
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) {
|
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 {
|
} else {
|
||||||
|
end();
|
||||||
res.sendResponse = res.send;
|
res.sendResponse = res.send;
|
||||||
res.send = (body) => {
|
res.send = (body) => {
|
||||||
if (res.statusCode === 200) {
|
if (res.statusCode === 200) {
|
||||||
mcache.put(key, body, duration * 1000);
|
mcache.put(key, body, duration * 1000);
|
||||||
}
|
}
|
||||||
res.sendResponse(body)
|
res.sendResponse(body);
|
||||||
|
responses.inc({scheme: scheme, target: target, code: res.statusCode, cache: 'miss'});
|
||||||
};
|
};
|
||||||
next();
|
next();
|
||||||
}
|
}
|
||||||
|
@ -57,10 +71,9 @@ app.get('/get', cache(30), cors(), (req, res) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
const url = (req.query) ? req.query.url : null;
|
const url = (req.query) ? req.query.url : null;
|
||||||
if (!url) {
|
if (!url) {
|
||||||
responses.inc({url: null, code: 404});
|
|
||||||
res.status(404).send(getResponse(404, "Not Found ")) //not found
|
res.status(404).send(getResponse(404, "Not Found ")) //not found
|
||||||
} else {
|
} 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-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");
|
||||||
|
@ -69,12 +82,7 @@ app.get('/get', cache(30), cors(), (req, res) => {
|
||||||
if (!response && err) {
|
if (!response && err) {
|
||||||
res.status(500).send(getResponse(500, "An error occurred for " + url))
|
res.status(500).send(getResponse(500, "An error occurred for " + url))
|
||||||
} else {
|
} else {
|
||||||
const end = histogram.startTimer({url: url});
|
|
||||||
res.status(response.status).send(response.body);
|
res.status(response.status).send(response.body);
|
||||||
res.on('finish', function () {
|
|
||||||
responses.inc({url: url, code: res.statusCode});
|
|
||||||
end();
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue