Skip to content

Commit

Permalink
chore: cargo-fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
evlli committed Mar 27, 2024
1 parent 40e5fbe commit 4018e55
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 34 deletions.
1 change: 0 additions & 1 deletion rustfmt.toml

This file was deleted.

21 changes: 12 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,17 @@ pub struct Configuration {

impl Configuration {
pub async fn load() -> Result<Configuration> {
let mut s = Config::builder();
if std::fs::metadata("config").is_ok(){
s = s.add_source(config::File::with_name("config"));
} else { println!("config file not found. continuing with env vars... ") };

s = s.add_source(config::Environment::with_prefix("VICI_EXPORTER").separator("_"));
// s.build().unwrap();
let conf: Configuration = s.build().unwrap().try_deserialize().unwrap();
Ok(conf)
let settings = Config::builder()
.set_default("vici.socket", "/var/run/charon.vici")?
.set_default("vici.interval", 10)?
.set_default("server.address", "0.0.0.0")?
.set_default("server.port", 8000)?
.add_source(config::File::with_name("config"))
.add_source(config::Environment::with_prefix("VICI_EXPORTER").separator("_"))
.build();
match settings {
Ok(body) => Ok(body.try_deserialize().unwrap()),
Err(err) => Err(err.into()),
}
}
}
33 changes: 21 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
use metrics::{
describe_gauge,
gauge,
describe_counter,
counter,
IntoLabels,
Unit};
use metrics::{counter, describe_counter, describe_gauge, gauge, IntoLabels, Unit};
use metrics_exporter_prometheus::PrometheusBuilder;
use tokio::time::{interval, Duration, MissedTickBehavior};

Expand Down Expand Up @@ -44,11 +38,26 @@ async fn main() -> anyhow::Result<()> {
let mut child_labels = sa_child_values.into_labels();
child_labels.push((&("sa_name", sa_name.clone())).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!("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());

counter!(
"sa_child_bytes_in",
sa_child_values.bytes_in,
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;
Expand Down
45 changes: 33 additions & 12 deletions src/vici.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#![allow(dead_code)]

use serde::Deserialize;
use serde::{de::value::BoolDeserializer, Deserialize};
use std::collections::HashMap;

use futures_util::stream::StreamExt;

use anyhow::Result;
use metrics::{IntoLabels,Label};
use metrics::{IntoLabels, Label};

#[derive(Debug, Deserialize)]
pub struct VICIState {
Expand All @@ -25,22 +25,45 @@ impl VICIState {
Ok(VICIState {
version: client.request("version", ()).await?,
statistics: client.request("statistics", ()).await?,
policies: collected_stream::<NamedPolicy, Policies>(client, "list-policies", "list-policy").await?,
connections: collected_stream::<NamedConnection, Connections>(client, "list-connections", "list-conn")
.await?,
security_associations: collected_stream::<NamedSecurityAssociation, SecurityAssociations>(
client, "list-sas", "list-sa",
policies: collected_stream::<NamedPolicy, Policies>(
client,
"list-policies",
"list-policy",
)
.await?,
connections: collected_stream::<NamedConnection, Connections>(
client,
"list-connections",
"list-conn",
)
.await?,
certificates: collected_stream::<NamedCertificate, Certificates>(client, "list-certs", "list-cert").await?,
authorities: collected_stream::<NamedAuthority, Authorities>(client, "list-authorities", "list-authority")
security_associations:
collected_stream::<NamedSecurityAssociation, SecurityAssociations>(
client, "list-sas", "list-sa",
)
.await?,
certificates: collected_stream::<NamedCertificate, Certificates>(
client,
"list-certs",
"list-cert",
)
.await?,
authorities: collected_stream::<NamedAuthority, Authorities>(
client,
"list-authorities",
"list-authority",
)
.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
N: for<'de> serde::Deserialize<'de>,
C: std::iter::Extend<N> + Default,
Expand Down Expand Up @@ -243,7 +266,6 @@ pub struct SecurityAssociation {
pub child_security_associations: HashMap<String, SecurityAssociationChild>,
}


impl IntoLabels for &SecurityAssociationChild {
fn into_labels(self) -> Vec<Label> {
vec![
Expand All @@ -253,7 +275,6 @@ impl IntoLabels for &SecurityAssociationChild {
}
}


#[derive(Debug, Deserialize)]
pub struct SecurityAssociationChild {
pub name: String,
Expand Down

0 comments on commit 4018e55

Please sign in to comment.