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

metrics: Implement FmtMetrics for Option #1302

Merged
merged 2 commits into from
Oct 7, 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions linkerd/app/core/src/metrics/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,15 @@ impl Metrics {
};

let report = endpoint_report
.and_then(route_report)
.and_then(retry_report)
.and_then(actual_report)
.and_then(control_report)
.and_then(transport_report)
.and_then(opencensus_report)
.and_then(stack)
.and_then(process)
.and_then(build_info);
.and_report(route_report)
.and_report(retry_report)
.and_report(actual_report)
.and_report(control_report)
.and_report(transport_report)
.and_report(opencensus_report)
.and_report(stack)
.and_report(process)
.and_report(build_info);

(metrics, report)
}
Expand Down
6 changes: 3 additions & 3 deletions linkerd/app/src/identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ impl Identity {
}
}

pub fn metrics(&self) -> metrics::Report {
pub fn metrics(&self) -> Option<metrics::Report> {
match self {
Identity::Disabled => metrics::Report::disabled(),
Identity::Enabled { ref local, .. } => local.metrics(),
Identity::Disabled => None,
Identity::Enabled { ref local, .. } => Some(local.metrics()),
}
}

Expand Down
6 changes: 3 additions & 3 deletions linkerd/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ impl Config {
let identity = info_span!("identity")
.in_scope(|| identity.build(dns.resolver.clone(), metrics.control.clone()))?;

let report = identity.metrics().and_then(report);
let report = identity.metrics().and_report(report);

let (drain_tx, drain_rx) = drain::channel();

Expand Down Expand Up @@ -163,8 +163,8 @@ impl Config {
let policy = inbound_policies.clone();
let report = inbound
.metrics()
.and_then(outbound.metrics())
.and_then(report);
.and_report(outbound.metrics())
.and_report(report);
info_span!("admin").in_scope(move || {
admin.build(
bind_admin,
Expand Down
14 changes: 13 additions & 1 deletion linkerd/metrics/src/prom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub trait FmtMetrics {
DisplayMetrics(self)
}

fn and_then<N>(self, next: N) -> AndThen<Self, N>
fn and_report<N>(self, next: N) -> AndThen<Self, N>
where
N: FmtMetrics,
Self: Sized,
Expand Down Expand Up @@ -191,12 +191,24 @@ impl<A: FmtLabels, B: FmtLabels> FmtLabels for (Option<A>, B) {
// ===== impl FmtMetrics =====

impl<'a, A: FmtMetrics + 'a> FmtMetrics for &'a A {
#[inline]
fn fmt_metrics(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
(*self).fmt_metrics(f)
}
}

impl<M: FmtMetrics> FmtMetrics for Option<M> {
#[inline]
fn fmt_metrics(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let Some(m) = self.as_ref() {
m.fmt_metrics(f)?;
}
Ok(())
}
}

impl<A: FmtMetrics, B: FmtMetrics> FmtMetrics for AndThen<A, B> {
#[inline]
fn fmt_metrics(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt_metrics(f)?;
self.1.fmt_metrics(f)?;
Expand Down
28 changes: 6 additions & 22 deletions linkerd/proxy/identity/src/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use tokio::sync::watch;

#[derive(Debug, Clone)]
pub struct Report {
inner: Option<Inner>,
crt_key_watch: watch::Receiver<Option<CrtKey>>,
refreshes: Arc<Counter>,
}

metrics! {
Expand All @@ -24,32 +25,15 @@ impl Report {
refreshes: Arc<Counter>,
) -> Self {
Self {
inner: Some(Inner {
crt_key_watch,
refreshes,
}),
crt_key_watch,
refreshes,
}
}

pub fn disabled() -> Self {
Self { inner: None }
}
}

#[derive(Debug, Clone)]
struct Inner {
crt_key_watch: watch::Receiver<Option<CrtKey>>,
refreshes: Arc<Counter>,
}

impl FmtMetrics for Report {
fn fmt_metrics(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let this = match self.inner.as_ref() {
Some(inner) => inner,
None => return Ok(()),
};

if let Some(ref crt_key) = *(this.crt_key_watch.borrow()) {
if let Some(ref crt_key) = *(self.crt_key_watch.borrow()) {
let dur = crt_key
.expiry()
.duration_since(UNIX_EPOCH)
Expand All @@ -63,7 +47,7 @@ impl FmtMetrics for Report {
}

identity_cert_refresh_count.fmt_help(f)?;
identity_cert_refresh_count.fmt_metric(f, &this.refreshes)?;
identity_cert_refresh_count.fmt_metric(f, &self.refreshes)?;

Ok(())
}
Expand Down