Add DB query error count metric

This commit is contained in:
Calvin Montgomery 2017-12-06 22:13:07 -08:00
parent 9886f648f2
commit 39587a8448
2 changed files with 12 additions and 4 deletions

View File

@ -2,7 +2,7 @@
"author": "Calvin Montgomery", "author": "Calvin Montgomery",
"name": "CyTube", "name": "CyTube",
"description": "Online media synchronizer and chat", "description": "Online media synchronizer and chat",
"version": "3.51.6", "version": "3.51.7",
"repository": { "repository": {
"url": "http://github.com/calzoneman/sync" "url": "http://github.com/calzoneman/sync"
}, },

View File

@ -18,6 +18,10 @@ const queryCount = new Counter({
name: 'cytube_db_queries_total', name: 'cytube_db_queries_total',
help: 'DB query count' help: 'DB query count'
}); });
const queryErrorCount = new Counter({
name: 'cytube_db_query_errors_total',
help: 'DB query error count'
});
setInterval(() => { setInterval(() => {
queryLatency.reset(); queryLatency.reset();
@ -55,10 +59,13 @@ class Database {
runTransaction(fn) { runTransaction(fn) {
const timer = Metrics.startTimer('db:queryTime'); const timer = Metrics.startTimer('db:queryTime');
const end = queryLatency.startTimer(); const end = queryLatency.startTimer();
return this.knex.transaction(fn).finally(() => { return this.knex.transaction(fn).catch(error => {
queryErrorCount.inc(1);
throw error;
}).finally(() => {
end(); end();
Metrics.stopTimer(timer); Metrics.stopTimer(timer);
queryCount.inc(1, new Date()); queryCount.inc(1);
}); });
} }
} }
@ -129,12 +136,13 @@ module.exports.query = function (query, sub, callback) {
.then(res => { .then(res => {
process.nextTick(callback, null, res[0]); process.nextTick(callback, null, res[0]);
}).catch(error => { }).catch(error => {
queryErrorCount.inc(1);
LOGGER.error('Legacy DB query failed. Query: %s, Substitutions: %j, Error: %s', query, sub, error); LOGGER.error('Legacy DB query failed. Query: %s, Substitutions: %j, Error: %s', query, sub, error);
process.nextTick(callback, 'Database failure', null); process.nextTick(callback, 'Database failure', null);
}).finally(() => { }).finally(() => {
end(); end();
Metrics.stopTimer(timer); Metrics.stopTimer(timer);
queryCount.inc(1, new Date()); queryCount.inc(1);
}); });
}; };