feat: add query_log_stdout config for pgcat-style query logging#1015
feat: add query_log_stdout config for pgcat-style query logging#1015marcomicera wants to merge 1 commit into
query_log_stdout config for pgcat-style query logging#1015Conversation
Log every query to stdout when enabled via pgdog.toml or PGDOG_QUERY_LOG_STDOUT, using format [pool: db][user: user] query
query_log_stdout config for pgcat-style query logging
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
|
Adding tests to cover these 6 missing lines... 👨💻 |
| #[serde(default)] | ||
| pub query_log: Option<PathBuf>, | ||
|
|
||
| /// Log queries to stdout (pgcat-style). Format: `[pool: db][user: user] query` |
There was a problem hiding this comment.
I would put the db/user at the end to match our existing log format. Not sure if it's right, but at least it's consistent.
SELECT * FROM users WHERE id = $1 [database: oranges, user: apples]
|
|
||
| /// Parse a query and return a command. | ||
| pub fn parse(&mut self, context: RouterContext) -> Result<Command, Error> { | ||
| if config().config.general.query_log_stdout { |
There was a problem hiding this comment.
I would put this somewhere at the top of QueryEngine::handle, in its own function. The parser is not guaranteed to be used if, for example, query_parser = "off" in the config.
The query will be available as:
if context.query_log_stdout {
if let Ok(Some(query)) = context.client_request.query() {
info!("{} [database: {}, user: {}]", query.query(), ...);
}
}Also, try to follow the same pattern as other settings, e.g. load them at request start only here:
pgdog/pgdog/src/frontend/client/mod.rs
Line 573 in beabd7d
Loading the config (ArcSwap + Arc clone) is effectively free, but not entirely, so we try to do it once per query at most.
Summary
Adds a
query_log_stdoutgeneral config option that logs every query to stdout in pgcat-style format:[pool: db][user: user] query.pgdog.tomlorPGDOG_QUERY_LOG_STDOUTfalseTest plan
query_log_stdout = trueand confirm queries appear in logs