Skip to content

Commit

Permalink
Merge pull request #673 from input-output-hk/feat/log-all-queries
Browse files Browse the repository at this point in the history
feat: log all queries
  • Loading branch information
rhyslbw authored Apr 18, 2023
2 parents 26d366a + 3876b7f commit 67bbe44
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
36 changes: 32 additions & 4 deletions packages/cardano-services/src/Program/services/postgres.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ import { URL } from 'url';
import { isConnectionError } from '@cardano-sdk/util';
import fs from 'fs';

const timedQuery = (pool: Pool, logger: Logger) => async (args: string | QueryConfig, values?: any) => {
const startTime = Date.now();
const result = await pool.query(args, values);
logger.debug(`Query\n${args}\ntook ${Date.now() - startTime} milliseconds`);
return result;
};

/**
* Creates a extended Pool client :
* - use passed srv service name in order to resolve the port
Expand All @@ -32,12 +39,12 @@ export const getPoolWithServiceDiscovery = async (
if (prop === 'then') return;
if (prop === 'query') {
return (args: string | QueryConfig, values?: any) =>
pool.query(args, values).catch(async (error) => {
timedQuery(pool, logger)(args, values).catch(async (error) => {
if (isConnectionError(error)) {
const record = await dnsResolver(host!);
logger.info(`DNS resolution for Postgres service, resolved with record: ${JSON.stringify(record)}`);
pool = new Pool({ database, host: record.name, max, password, port: record.port, ssl, user });
return await pool.query(args, values);
return timedQuery(pool, logger)(args, values);
}
throw error;
});
Expand All @@ -47,6 +54,8 @@ export const getPoolWithServiceDiscovery = async (
const method = pool[prop as keyof Pool] as any;
return method.bind(pool);
}

return pool[prop as keyof Pool];
}
});
};
Expand All @@ -59,8 +68,27 @@ export const getPool = async (
options?: PosgresProgramOptions
): Promise<Pool | undefined> => {
const ssl = options?.postgresSslCaFile ? { ca: loadSecret(options.postgresSslCaFile) } : undefined;
if (options?.postgresConnectionString)
return new Pool({ connectionString: options.postgresConnectionString, max: options.postgresPoolMax, ssl });

if (options?.postgresConnectionString) {
const pool = new Pool({ connectionString: options.postgresConnectionString, max: options.postgresPoolMax, ssl });

return new Proxy<Pool>({} as Pool, {
get(_, prop) {
if (prop === 'then') return;
if (prop === 'query') {
return timedQuery(pool, logger);
}
// Bind if it is a function, no intercept operations
if (typeof pool[prop as keyof Pool] === 'function') {
const method = pool[prop as keyof Pool] as any;
return method.bind(pool);
}

return pool[prop as keyof Pool];
}
});
}

if (options?.postgresSrvServiceName && options.postgresUser && options.postgresDb && options.postgresPassword) {
return getPoolWithServiceDiscovery(dnsResolver, logger, {
database: options.postgresDb,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,6 @@ describe('Service dependency abstractions', () => {
jest.clearAllTimers();
});

it('db should not be instance a of Proxy ', () => {
expect(types.isProxy(dbPools.main!)).toEqual(false);
});

it('forwards the db health response', async () => {
const res = await axios.post(`${apiUrlBase}/health`, {
headers: { 'Content-Type': APPLICATION_JSON }
Expand Down

0 comments on commit 67bbe44

Please sign in to comment.