mirror of
https://github.com/postgresml/pgcat.git
synced 2026-03-27 18:56:30 +00:00
Added a Plugin trait (#536)
* Improved logging * Improved logging for more `Address` usages * Fixed lint issues. * Reverted the `Address` logging changes. * Applied the PR comment by @levkk. * Applied the PR comment by @levkk. * Applied the PR comment by @levkk. * Applied the PR comment by @levkk.
This commit is contained in:
@@ -867,15 +867,26 @@ pub struct Plugins {
|
|||||||
pub prewarmer: Option<Prewarmer>,
|
pub prewarmer: Option<Prewarmer>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub trait Plugin {
|
||||||
|
fn is_enabled(&self) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for Plugins {
|
impl std::fmt::Display for Plugins {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
||||||
|
fn is_enabled<T: Plugin>(arg: Option<&T>) -> bool {
|
||||||
|
if let Some(ref arg) = arg {
|
||||||
|
arg.is_enabled()
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"interceptor: {}, table_access: {}, query_logger: {}, prewarmer: {}",
|
"interceptor: {}, table_access: {}, query_logger: {}, prewarmer: {}",
|
||||||
self.intercept.is_some(),
|
is_enabled(self.intercept.as_ref()),
|
||||||
self.table_access.is_some(),
|
is_enabled(self.table_access.as_ref()),
|
||||||
self.query_logger.is_some(),
|
is_enabled(self.query_logger.as_ref()),
|
||||||
self.prewarmer.is_some(),
|
is_enabled(self.prewarmer.as_ref()),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -886,23 +897,47 @@ pub struct Intercept {
|
|||||||
pub queries: BTreeMap<String, Query>,
|
pub queries: BTreeMap<String, Query>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Plugin for Intercept {
|
||||||
|
fn is_enabled(&self) -> bool {
|
||||||
|
self.enabled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, Hash, Eq)]
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, Hash, Eq)]
|
||||||
pub struct TableAccess {
|
pub struct TableAccess {
|
||||||
pub enabled: bool,
|
pub enabled: bool,
|
||||||
pub tables: Vec<String>,
|
pub tables: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Plugin for TableAccess {
|
||||||
|
fn is_enabled(&self) -> bool {
|
||||||
|
self.enabled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, Hash, Eq)]
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, Hash, Eq)]
|
||||||
pub struct QueryLogger {
|
pub struct QueryLogger {
|
||||||
pub enabled: bool,
|
pub enabled: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Plugin for QueryLogger {
|
||||||
|
fn is_enabled(&self) -> bool {
|
||||||
|
self.enabled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, Hash, Eq)]
|
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Default, Hash, Eq)]
|
||||||
pub struct Prewarmer {
|
pub struct Prewarmer {
|
||||||
pub enabled: bool,
|
pub enabled: bool,
|
||||||
pub queries: Vec<String>,
|
pub queries: Vec<String>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Plugin for Prewarmer {
|
||||||
|
fn is_enabled(&self) -> bool {
|
||||||
|
self.enabled
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl Intercept {
|
impl Intercept {
|
||||||
pub fn substitute(&mut self, db: &str, user: &str) {
|
pub fn substitute(&mut self, db: &str, user: &str) {
|
||||||
for (_, query) in self.queries.iter_mut() {
|
for (_, query) in self.queries.iter_mut() {
|
||||||
|
|||||||
Reference in New Issue
Block a user