Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chrono->time #3

Merged
merged 2 commits into from Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 3 additions & 43 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Expand Up @@ -24,14 +24,14 @@ maintenance = {status = "actively-developed"}

[dependencies]
color-eyre = { version = "0.5.11", default-features = false }
chrono = "0.4.19"
clap = "3.0.0-beta.5"
prometheus = "0.13.0"
prometheus-hyper = "0.1.3"
tokio = { version = "1.5.0", features = ["full"] }
tracing = "0.1.25"
tracing-subscriber = "0.3.1"
base64 = "0.13.0"
time = { version = "0.3.5", features = ["local-offset"] }

[build-dependencies]
clap = "3.0.0-beta.5"
Expand Down
13 changes: 7 additions & 6 deletions src/metrics.rs
@@ -1,6 +1,6 @@
use chrono::{DateTime, Local, TimeZone};
use color_eyre::eyre::Result;
use prometheus::{IntCounterVec, IntGaugeVec, Opts, Registry};
use time::OffsetDateTime;
use tracing::{debug, trace};

use crate::wireguard::WireguardState;
Expand Down Expand Up @@ -49,7 +49,7 @@ impl Metrics {
let duration_since_latest_handshake = IntGaugeVec::new(
Opts::new(
"wireguard_duration_since_latest_handshake",
"During since latest handshake for a peer",
"Duration in milliseconds since latest handshake for a peer",
),
&["interface", "peer", "alias"],
)?;
Expand Down Expand Up @@ -133,10 +133,11 @@ impl Metrics {
pbtr.inc_by(diff);

if let Some(latest_hs_ts) = p.handshake_timestamp {
let now: DateTime<Local> = Local::now();
let hs_ts: DateTime<Local> = Local.timestamp(latest_hs_ts as i64, 0);
let now = OffsetDateTime::now_local()
.expect("Failed to get local offset time")
.unix_timestamp();

let elapsed = now.signed_duration_since(hs_ts);
let elapsed = now - latest_hs_ts as i64;

self.duration_since_latest_handshake
.with_label_values(&[
Expand All @@ -147,7 +148,7 @@ impl Metrics {
.map(ToOwned::to_owned)
.unwrap_or_else(String::new),
])
.set(elapsed.num_milliseconds());
.set(elapsed * 1000);
}
}
}
Expand Down