export docs fix
parent
c028699bab
commit
4c828c968e
|
|
@ -38,6 +38,11 @@ pub struct Exporter {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Exporter {
|
impl Exporter {
|
||||||
|
/// Fills `deadpool_postgres::Config` object with values from ENV VARS:
|
||||||
|
/// - `DB_HOST`
|
||||||
|
/// - `DB_DBNAME`
|
||||||
|
/// - `DB_USER`
|
||||||
|
/// - `DB_PASSWORD`
|
||||||
fn config_construct() -> Result<Config> {
|
fn config_construct() -> Result<Config> {
|
||||||
let mut cfg = Config::new();
|
let mut cfg = Config::new();
|
||||||
cfg.host = Some(env::var("DB_HOST")?);
|
cfg.host = Some(env::var("DB_HOST")?);
|
||||||
|
|
@ -46,6 +51,9 @@ impl Exporter {
|
||||||
cfg.password = Some(env::var("DB_PASSWORD")?);
|
cfg.password = Some(env::var("DB_PASSWORD")?);
|
||||||
Ok(cfg)
|
Ok(cfg)
|
||||||
}
|
}
|
||||||
|
/// Uses `deadpool_postgres::Config` object to create DB connections
|
||||||
|
/// pool to share between async tasks and to restrict a count of parallel
|
||||||
|
/// connections
|
||||||
fn pool_construct() -> Option<Pool> {
|
fn pool_construct() -> Option<Pool> {
|
||||||
return match Self::config_construct() {
|
return match Self::config_construct() {
|
||||||
Ok(config) => {
|
Ok(config) => {
|
||||||
|
|
@ -61,6 +69,7 @@ impl Exporter {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// Checks if DB connections pool is empty
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub fn is_no_connection(&self) -> bool { self.pool.is_none() }
|
pub fn is_no_connection(&self) -> bool { self.pool.is_none() }
|
||||||
pub fn init() -> Self {
|
pub fn init() -> Self {
|
||||||
|
|
@ -68,6 +77,9 @@ impl Exporter {
|
||||||
pool : Self::pool_construct(),
|
pool : Self::pool_construct(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
/// Shares a connection `deadpool_postgres::Client as PgClient`
|
||||||
|
///
|
||||||
|
/// Function awaits til the moment it can return `Option<deadpool_postgres::Client as PgClient>`
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub async fn get_connection_from_pool(&self) -> Option<PgClient> {
|
pub async fn get_connection_from_pool(&self) -> Option<PgClient> {
|
||||||
if let Some(pool) = &self.pool {
|
if let Some(pool) = &self.pool {
|
||||||
|
|
@ -75,12 +87,15 @@ impl Exporter {
|
||||||
}
|
}
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
|
/// Exports data in `&str` jsonb format to DB using connection from the pool
|
||||||
#[allow(unused)]
|
#[allow(unused)]
|
||||||
pub async fn export_data(client: PgClient, metrics: &str) -> Result<()> {
|
pub async fn export_data(client: PgClient, metrics: &str) -> Result<()> {
|
||||||
let query = client.prepare_cached("INSERT INTO metrics (body) VALUES ($1);").await?;
|
let query = client.prepare_cached("INSERT INTO metrics (body) VALUES ($1);").await?;
|
||||||
let _ = client.query(&query, &[&metrics]).await?;
|
let _ = client.query(&query, &[&metrics]).await?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
/// Exports metrics in `PrometheusMetrics` format to Exporter defined
|
||||||
|
/// as env var $EXORPTER_URL
|
||||||
pub async fn export_metrics(metrics: PrometheusMetrics) -> Result<usize> {
|
pub async fn export_metrics(metrics: PrometheusMetrics) -> Result<usize> {
|
||||||
let url = env::var("EXPORTER_URL")?;
|
let url = env::var("EXPORTER_URL")?;
|
||||||
|
|
||||||
|
|
@ -92,6 +107,8 @@ impl Exporter {
|
||||||
req?;
|
req?;
|
||||||
Ok(metrics.get_bytes_len())
|
Ok(metrics.get_bytes_len())
|
||||||
}
|
}
|
||||||
|
/// Exports metrics in `PrometheusMetricsExtended` format to Exporter defined
|
||||||
|
/// as env var $EXORPTER_URL
|
||||||
pub async fn export_extended_metrics(metrics: PrometheusMetricsExtended) -> Result<usize> {
|
pub async fn export_extended_metrics(metrics: PrometheusMetricsExtended) -> Result<usize> {
|
||||||
let url = env::var("EXPORTER_URL")?;
|
let url = env::var("EXPORTER_URL")?;
|
||||||
let req = Client::new()
|
let req = Client::new()
|
||||||
|
|
@ -105,6 +122,7 @@ impl Exporter {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for Exporter {
|
impl Drop for Exporter {
|
||||||
|
// Custom destructor to log deinitializing of the `Exporter`
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
info!("Deinitializng Exporter and DB connection pool ...")
|
info!("Deinitializng Exporter and DB connection pool ...")
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue