mirror of
https://github.com/mastodon/mastodon.git
synced 2024-11-21 21:57:19 +00:00
Streaming: add development logging of database queries (#32945)
This commit is contained in:
parent
03ee08c2da
commit
9a46329fbd
|
@ -116,13 +116,44 @@ let pool;
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param {pg.PoolConfig} config
|
* @param {pg.PoolConfig} config
|
||||||
|
* @param {string} environment
|
||||||
|
* @param {import('pino').Logger} logger
|
||||||
* @returns {pg.Pool}
|
* @returns {pg.Pool}
|
||||||
*/
|
*/
|
||||||
export function getPool(config) {
|
export function getPool(config, environment, logger) {
|
||||||
if (pool) {
|
if (pool) {
|
||||||
return pool;
|
return pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
pool = new pg.Pool(config);
|
pool = new pg.Pool(config);
|
||||||
|
|
||||||
|
// Setup logging on pool.query and client.query for checked out clients:
|
||||||
|
// This is taken from: https://node-postgres.com/guides/project-structure
|
||||||
|
if (environment === 'development') {
|
||||||
|
const logQuery = (originalQuery) => {
|
||||||
|
return async (queryTextOrConfig, values, ...rest) => {
|
||||||
|
const start = process.hrtime();
|
||||||
|
|
||||||
|
const result = await originalQuery.apply(pool, [queryTextOrConfig, values, ...rest]);
|
||||||
|
|
||||||
|
const duration = process.hrtime(start);
|
||||||
|
const durationInMs = (duration[0] * 1000000000 + duration[1]) / 1000000;
|
||||||
|
|
||||||
|
logger.debug({
|
||||||
|
query: queryTextOrConfig,
|
||||||
|
values,
|
||||||
|
duration: durationInMs
|
||||||
|
}, 'Executed database query');
|
||||||
|
|
||||||
|
return result;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
pool.on('connect', (client) => {
|
||||||
|
const originalQuery = client.query.bind(client);
|
||||||
|
client.query = logQuery(originalQuery);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
return pool;
|
return pool;
|
||||||
}
|
}
|
||||||
|
|
|
@ -101,7 +101,8 @@ const CHANNEL_NAMES = [
|
||||||
];
|
];
|
||||||
|
|
||||||
const startServer = async () => {
|
const startServer = async () => {
|
||||||
const pgPool = Database.getPool(Database.configFromEnv(process.env, environment));
|
const pgConfig = Database.configFromEnv(process.env, environment);
|
||||||
|
const pgPool = Database.getPool(pgConfig, environment, logger);
|
||||||
|
|
||||||
const metrics = setupMetrics(CHANNEL_NAMES, pgPool);
|
const metrics = setupMetrics(CHANNEL_NAMES, pgPool);
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue