[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:
parent
eabd920aa2
commit
86c65dbd81
|
@ -15,11 +15,11 @@ 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: ['scheme', 'target', 'code', 'cache'],
|
||||
labelNames: ['scheme', 'target', 'code'],
|
||||
registers: [register]
|
||||
});
|
||||
|
||||
const entries = new prom.Counter({
|
||||
const entries = new prom.Gauge({
|
||||
name: 'cache_used_entries',
|
||||
help: 'A counter to count cache entries',
|
||||
registers: [register]
|
||||
|
@ -28,36 +28,46 @@ const entries = new prom.Counter({
|
|||
const histogram = new prom.Histogram({
|
||||
name: 'cache_http_request_duration_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],
|
||||
buckets: [0.1, 0.2, 0.5, 1, 2]
|
||||
});
|
||||
|
||||
let cache = (duration) => {
|
||||
return (req, res, next) => {
|
||||
let key = '__express__' + req.originalUrl || req.url;
|
||||
if(req.query.url) {
|
||||
let key = '__express__' + req.query.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) {
|
||||
const end = histogram.startTimer({scheme: scheme, target: target, cache: 'hit'});
|
||||
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();
|
||||
} else {
|
||||
end();
|
||||
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);
|
||||
mcache.put(key, body, duration * 1000, () => {
|
||||
entries.set(mcache.size());
|
||||
});
|
||||
}
|
||||
res.sendResponse(body);
|
||||
responses.inc({scheme: scheme, target: target, code: res.statusCode, cache: 'miss'});
|
||||
};
|
||||
next();
|
||||
}
|
||||
} else {
|
||||
next();
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
|
@ -73,7 +83,7 @@ app.get('/get', cache(30), cors(), (req, res) => {
|
|||
if (!url) {
|
||||
res.status(404).send(getResponse(404, "Not Found ")) //not found
|
||||
} 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-Headers", "Origin, Content-Type, Content-Length");
|
||||
res.header("Access-Control-Allow-Methods", "GET, OPTIONS");
|
||||
|
|
Loading…
Reference in New Issue