chore: cargo-fmt

This commit is contained in:
Evelyn Alicke 2024-03-27 11:52:08 +01:00 committed by Evelyn Alicke
parent bf9549d801
commit dcdee5ffaa
Signed by: evlli
GPG key ID: 8092413A3F6DD75F
4 changed files with 66 additions and 34 deletions

View file

@ -1 +0,0 @@
max_width = 120

View file

@ -28,14 +28,17 @@ pub struct Configuration {
impl Configuration { impl Configuration {
pub async fn load() -> Result<Configuration> { pub async fn load() -> Result<Configuration> {
let mut s = Config::builder(); let settings = Config::builder()
if std::fs::metadata("config").is_ok(){ .set_default("vici.socket", "/var/run/charon.vici")?
s = s.add_source(config::File::with_name("config")); .set_default("vici.interval", 10)?
} else { println!("config file not found. continuing with env vars... ") }; .set_default("server.address", "0.0.0.0")?
.set_default("server.port", 8000)?
s = s.add_source(config::Environment::with_prefix("VICI_EXPORTER").separator("_")); .add_source(config::File::with_name("config"))
// s.build().unwrap(); .add_source(config::Environment::with_prefix("VICI_EXPORTER").separator("_"))
let conf: Configuration = s.build().unwrap().try_deserialize().unwrap(); .build();
Ok(conf) match settings {
Ok(body) => Ok(body.try_deserialize().unwrap()),
Err(err) => Err(err.into()),
}
} }
} }

View file

@ -1,10 +1,4 @@
use metrics::{ use metrics::{counter, describe_counter, describe_gauge, gauge, IntoLabels, Unit};
describe_gauge,
gauge,
describe_counter,
counter,
IntoLabels,
Unit};
use metrics_exporter_prometheus::PrometheusBuilder; use metrics_exporter_prometheus::PrometheusBuilder;
use tokio::time::{interval, Duration, MissedTickBehavior}; use tokio::time::{interval, Duration, MissedTickBehavior};
@ -44,11 +38,26 @@ async fn main() -> anyhow::Result<()> {
let mut child_labels = sa_child_values.into_labels(); let mut child_labels = sa_child_values.into_labels();
child_labels.push((&("sa_name", sa_name.clone())).into()); child_labels.push((&("sa_name", sa_name.clone())).into());
child_labels.push((&("sa_child_name", sa_child_name)).into()); child_labels.push((&("sa_child_name", sa_child_name)).into());
counter!("sa_child_bytes_in", sa_child_values.bytes_in, child_labels.clone()); counter!(
counter!("sa_child_bytes_out", sa_child_values.bytes_out, child_labels.clone()); "sa_child_bytes_in",
counter!("sa_child_packets_in", sa_child_values.packets_in, child_labels.clone()); sa_child_values.bytes_in,
counter!("sa_child_packets_out", sa_child_values.packets_out, child_labels.clone()); child_labels.clone()
);
counter!(
"sa_child_bytes_out",
sa_child_values.bytes_out,
child_labels.clone()
);
counter!(
"sa_child_packets_in",
sa_child_values.packets_in,
child_labels.clone()
);
counter!(
"sa_child_packets_out",
sa_child_values.packets_out,
child_labels.clone()
);
} }
} }
interval.tick().await; interval.tick().await;

View file

@ -1,12 +1,12 @@
#![allow(dead_code)] #![allow(dead_code)]
use serde::Deserialize; use serde::{de::value::BoolDeserializer, Deserialize};
use std::collections::HashMap; use std::collections::HashMap;
use futures_util::stream::StreamExt; use futures_util::stream::StreamExt;
use anyhow::Result; use anyhow::Result;
use metrics::{IntoLabels,Label}; use metrics::{IntoLabels, Label};
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct VICIState { pub struct VICIState {
@ -25,22 +25,45 @@ impl VICIState {
Ok(VICIState { Ok(VICIState {
version: client.request("version", ()).await?, version: client.request("version", ()).await?,
statistics: client.request("statistics", ()).await?, statistics: client.request("statistics", ()).await?,
policies: collected_stream::<NamedPolicy, Policies>(client, "list-policies", "list-policy").await?, policies: collected_stream::<NamedPolicy, Policies>(
connections: collected_stream::<NamedConnection, Connections>(client, "list-connections", "list-conn") client,
"list-policies",
"list-policy",
)
.await?, .await?,
security_associations: collected_stream::<NamedSecurityAssociation, SecurityAssociations>( connections: collected_stream::<NamedConnection, Connections>(
client,
"list-connections",
"list-conn",
)
.await?,
security_associations:
collected_stream::<NamedSecurityAssociation, SecurityAssociations>(
client, "list-sas", "list-sa", client, "list-sas", "list-sa",
) )
.await?, .await?,
certificates: collected_stream::<NamedCertificate, Certificates>(client, "list-certs", "list-cert").await?, certificates: collected_stream::<NamedCertificate, Certificates>(
authorities: collected_stream::<NamedAuthority, Authorities>(client, "list-authorities", "list-authority") client,
"list-certs",
"list-cert",
)
.await?,
authorities: collected_stream::<NamedAuthority, Authorities>(
client,
"list-authorities",
"list-authority",
)
.await?, .await?,
pools: collected_stream::<NamedPool, Pools>(client, "list-pools", "list-pool").await?, pools: collected_stream::<NamedPool, Pools>(client, "list-pools", "list-pool").await?,
}) })
} }
} }
async fn collected_stream<N, C>(client: &mut rsvici::Client, command: &str, event: &str) -> Result<C> async fn collected_stream<N, C>(
client: &mut rsvici::Client,
command: &str,
event: &str,
) -> Result<C>
where where
N: for<'de> serde::Deserialize<'de>, N: for<'de> serde::Deserialize<'de>,
C: std::iter::Extend<N> + Default, C: std::iter::Extend<N> + Default,
@ -243,7 +266,6 @@ pub struct SecurityAssociation {
pub child_security_associations: HashMap<String, SecurityAssociationChild>, pub child_security_associations: HashMap<String, SecurityAssociationChild>,
} }
impl IntoLabels for &SecurityAssociationChild { impl IntoLabels for &SecurityAssociationChild {
fn into_labels(self) -> Vec<Label> { fn into_labels(self) -> Vec<Label> {
vec![ vec![
@ -253,7 +275,6 @@ impl IntoLabels for &SecurityAssociationChild {
} }
} }
#[derive(Debug, Deserialize)] #[derive(Debug, Deserialize)]
pub struct SecurityAssociationChild { pub struct SecurityAssociationChild {
pub name: String, pub name: String,