Skip to content

Commit

Permalink
store maxminddb_reader in the Metrics struct
Browse files Browse the repository at this point in the history
  • Loading branch information
furmur committed Apr 8, 2024
1 parent 121669d commit 736f1ae
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ async fn try_main(args: Args) -> Result<()> {

info!("Registering metrics...");
let registry = Arc::new(Registry::new());
let mut metrics = unwrap_or_exit!(Metrics::new(&registry, &maxminddb_reader));
let mut metrics = unwrap_or_exit!(Metrics::new(&registry, maxminddb_reader));
let scrape_duration = unwrap_or_exit!(IntGauge::new(
"wireguard_scrape_duration_milliseconds",
"Duration in milliseconds of the scrape",
Expand Down Expand Up @@ -94,7 +94,7 @@ async fn try_main(args: Args) -> Result<()> {

debug!("Updating metrics...");
metrics
.update(&WireguardState::scrape(&aliases).await?, &maxminddb_reader)
.update(&WireguardState::scrape(&aliases).await?)
.await;
let after = Instant::now();

Expand Down
9 changes: 5 additions & 4 deletions src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ pub struct Metrics {
bytes_total: IntCounterVec,
peer_bytes_total: IntCounterVec,
duration_since_latest_handshake: IntGaugeVec,
maxminddb_reader: Option<maxminddb::Reader<Vec<u8>>>
}

impl Metrics {
pub fn new(
r: &Registry,
maxminddb_reader: &Option<maxminddb::Reader<Vec<u8>>>,
maxminddb_reader: Option<maxminddb::Reader<Vec<u8>>>,
) -> Result<Self> {
trace!("Metrics::new");

Expand Down Expand Up @@ -88,13 +89,13 @@ impl Metrics {
peer_endpoint,
peer_bytes_total,
duration_since_latest_handshake,
maxminddb_reader
})
}

pub async fn update(
&mut self,
state: &WireguardState,
maxminddb_reader: &Option<maxminddb::Reader<Vec<u8>>>,
state: &WireguardState
) {
let it = self.interfaces_total.with_label_values(&[]);
it.set(state.interfaces.len() as i64);
Expand Down Expand Up @@ -134,7 +135,7 @@ impl Metrics {
assert!(p.interface < state.interfaces.len());

if let Some(endpoint) = p.endpoint {
match maxminddb_reader {
match &self.maxminddb_reader {
Some(reader) => {
let endpoint_country = match reader.lookup::<geoip2::Country>(endpoint.ip())
{
Expand Down

0 comments on commit 736f1ae

Please sign in to comment.