Skip to content

Commit

Permalink
fix uses of Instant::sub
Browse files Browse the repository at this point in the history
It looks like clippy's disallowed methods can't detect the use of
disallowed *operators*, so #1456 misses a bunch of uses of the `-`
operator on `Instant`s, mostly in metrics, tap, and the log timestamp
formatter.

This fixes all of them that I could find.
  • Loading branch information
hawkw committed Feb 1, 2022
1 parent 9772a89 commit 1447915
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 12 deletions.
2 changes: 1 addition & 1 deletion linkerd/http-metrics/src/requests/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ where
metric.fmt_help(f)?;
Self::fmt_by_class(&registry, f, metric, |s| &s.total)?;

registry.retain_since(Instant::now() - self.retain_idle);
registry.retain_since(Instant::now().saturating_duration_since(self.retain_idle));

Ok(())
}
Expand Down
3 changes: 2 additions & 1 deletion linkerd/http-metrics/src/requests/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ where
.entry(Some(*this.status))
.or_insert_with(StatusMetrics::default);

status_metrics.latency.add(now - *this.stream_open_at);
let elapsed = now.saturating_duration_since(*this.stream_open_at);
status_metrics.latency.add(elapsed);

*this.latency_recorded = true;
}
Expand Down
14 changes: 9 additions & 5 deletions linkerd/proxy/tap/src/grpc/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -364,10 +364,10 @@ impl iface::TapResponse for TapResponse {
} else {
None
};

let since_request_init = response_init_at.saturating_duration_since(self.request_init_at);
let init = api::tap_event::http::Event::ResponseInit(api::tap_event::http::ResponseInit {
id: Some(self.tap.id.clone()),
since_request_init: Some((response_init_at - self.request_init_at).into()),
since_request_init: Some(since_request_init.into()),
http_status: rsp.status().as_u16().into(),
headers,
});
Expand Down Expand Up @@ -398,9 +398,10 @@ impl iface::TapResponse for TapResponse {
fn fail<E: HasH2Reason>(self, err: &E) {
let response_end_at = Instant::now();
let reason = err.h2_reason();
let since_request_init = response_end_at.saturating_duration_since(self.request_init_at);
let end = api::tap_event::http::Event::ResponseEnd(api::tap_event::http::ResponseEnd {
id: Some(self.tap.id.clone()),
since_request_init: Some((response_end_at - self.request_init_at).into()),
since_request_init: Some(since_request_init.into()),
since_response_init: None,
response_bytes: 0,
eos: Some(api::Eos {
Expand Down Expand Up @@ -464,10 +465,13 @@ impl TapResponsePayload {
} else {
None
};

let since_request_init = response_end_at.saturating_duration_since(self.request_init_at);
let since_response_init = response_end_at.saturating_duration_since(self.response_init_at);
let end = api::tap_event::http::ResponseEnd {
id: Some(self.tap.id),
since_request_init: Some((response_end_at - self.request_init_at).into()),
since_response_init: Some((response_end_at - self.response_init_at).into()),
since_request_init: Some(since_request_init.into()),
since_response_init: Some(since_response_init.into()),
response_bytes: self.response_bytes as u64,
eos: Some(api::Eos { end }),
trailers,
Expand Down
6 changes: 3 additions & 3 deletions linkerd/stack/metrics/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ where
// updated even when we're "stuck" in pending.
let now = Instant::now();
if let Some(t0) = self.blocked_since.take() {
let not_ready = now - t0;
let not_ready = now.saturating_duration_since(t0);
self.metrics.poll_millis.add(not_ready.as_millis() as u64);
}
self.blocked_since = Some(now);
Expand All @@ -48,15 +48,15 @@ where
Poll::Ready(Ok(())) => {
self.metrics.ready_total.incr();
if let Some(t0) = self.blocked_since.take() {
let not_ready = Instant::now() - t0;
let not_ready = Instant::now().saturating_duration_since(t0);
self.metrics.poll_millis.add(not_ready.as_millis() as u64);
}
Poll::Ready(Ok(()))
}
Poll::Ready(Err(e)) => {
self.metrics.error_total.incr();
if let Some(t0) = self.blocked_since.take() {
let not_ready = Instant::now() - t0;
let not_ready = Instant::now().saturating_duration_since(t0);
self.metrics.poll_millis.add(not_ready.as_millis() as u64);
}
Poll::Ready(Err(e))
Expand Down
2 changes: 1 addition & 1 deletion linkerd/tracing/src/uptime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ impl Uptime {

impl FormatTime for Uptime {
fn format_time(&self, w: &mut format::Writer<'_>) -> fmt::Result {
Self::format(Instant::now() - self.start_time, w)
Self::format(Instant::now().saturating_duration_since(self.start_time), w)
}
}

Expand Down
2 changes: 1 addition & 1 deletion linkerd/transport-metrics/src/report.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl<K: Eq + Hash + FmtLabels + 'static> FmtMetrics for Report<K> {
tcp_close_total.fmt_help(f)?;
Self::fmt_eos_by(&*metrics, f, tcp_close_total, |e| &e.close_total)?;

metrics.retain_since(Instant::now() - self.retain_idle);
metrics.retain_since(Instant::now().saturating_duration_since(self.retain_idle));

Ok(())
}
Expand Down

0 comments on commit 1447915

Please sign in to comment.