Skip to content

Commit eb5225f

Browse files
authored
Merge pull request #6940 from dannyzaken/danny-fixes
print log message for long running db queries
2 parents d14bdf6 + c3c8b80 commit eb5225f

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

config.js

+3
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,9 @@ config.on_premise = {
369369
nva_part: "NVA_Upgrade.tgz"
370370
};
371371

372+
// the threshold in ms for logging long running queries
373+
config.LONG_DB_QUERY_THRESHOLD = parseInt(process.env.LONG_DB_QUERY_THRESHOLD, 10) || 5000;
374+
372375
/*
373376
Central Stats Collection & Diagnostics
374377
*/

src/util/postgres_client.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const mongo_functions = require('./mongo_functions');
2525
const { RpcError } = require('../rpc');
2626
const SensitiveString = require('./sensitive_string');
2727
const time_utils = require('./time_utils');
28+
const config = require('../../config');
2829
mongodb.Binary.prototype[util.inspect.custom] = function custom_inspect_binary() {
2930
return `<mongodb.Binary ${this.buffer.toString('base64')} >`;
3031
};
@@ -181,7 +182,7 @@ let query_counter = 0;
181182
async function log_query(pg_client, query, tag, millitook, should_explain) {
182183
const log_obj = {
183184
tag,
184-
took: millitook,
185+
took: millitook.toFixed(1) + 'ms',
185186
query,
186187
clients_pool: { total: pg_client.totalCount, waiting: pg_client.waitingCount, idle: pg_client.idleCount },
187188
stack: (new Error()).stack.split('\n').slice(1),
@@ -198,7 +199,11 @@ async function log_query(pg_client, query, tag, millitook, should_explain) {
198199
}
199200
}
200201

201-
dbg.log0('QUERY_LOG:', JSON.stringify(log_obj));
202+
if (millitook > config.LONG_DB_QUERY_THRESHOLD) {
203+
dbg.warn(`QUERY_LOG: LONG QUERY (OVER ${config.LONG_DB_QUERY_THRESHOLD} ms `, JSON.stringify(log_obj));
204+
} else {
205+
dbg.log0('QUERY_LOG:', JSON.stringify(log_obj));
206+
}
202207
}
203208

204209
function convert_sort(sort) {
@@ -239,8 +244,9 @@ async function _do_query(pg_client, q, transaction_counter) {
239244
// dbg.log0(`postgres_client: ${tag}: ${q.text}`, util.inspect(q.values, { depth: 6 }));
240245
const millistart = time_utils.millistamp();
241246
const res = await pg_client.query(q);
242-
const millitook = time_utils.millitook(millistart);
243-
if (process.env.PG_ENABLE_QUERY_LOG === 'true') {
247+
const milliend = time_utils.millistamp();
248+
const millitook = milliend - millistart;
249+
if (process.env.PG_ENABLE_QUERY_LOG === 'true' || millitook > config.LONG_DB_QUERY_THRESHOLD) {
244250
// noticed that some failures in explain are invalidating the transaction.
245251
// myabe did something wrong but for now don't try to EXPLAIN the query when in transaction.
246252
await log_query(pg_client, q, tag, millitook, /*should_explain*/ transaction_counter === 0);
@@ -762,7 +768,7 @@ class PostgresTable {
762768
* https://github.com/thomas4019/mongo-query-to-postgres-jsonb/blob/e0bb65eafc39458da30e4fc3c5f47ffb5d509fcc/test/filter.js#L229
763769
*/
764770
function calculateOptionsAndArrayFields(q) {
765-
const ops = [ '$all' ];
771+
const ops = ['$all'];
766772
const l = [];
767773
for (const p of Object.keys(q)) {
768774
for (const o of ops) {

0 commit comments

Comments
 (0)