Skip to content

Commit c6ad4ff

Browse files
authored
[chore] Use elapsed() if possible when calculating durations (#750)
1 parent 1b70e31 commit c6ad4ff

File tree

17 files changed

+36
-68
lines changed

17 files changed

+36
-68
lines changed

bin_tests/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ macro_rules! timeit {
157157
($op_name:literal, $op:block) => {{
158158
let start = std::time::Instant::now();
159159
let res = $op;
160-
let delta = std::time::Instant::now().duration_since(start);
160+
let delta = start.elapsed();
161161
println!(
162162
concat!($op_name, " took {} ms"),
163163
delta.as_secs_f64() * 1000.0

crashtracker/src/collector/crash_handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ fn reap_child_non_blocking(pid: Pid, timeout_ms: u32) -> anyhow::Result<bool> {
155155
loop {
156156
match waitpid(pid, Some(WaitPidFlag::WNOHANG)) {
157157
Ok(WaitStatus::StillAlive) => {
158-
if Instant::now().duration_since(start_time) > timeout {
158+
if start_time.elapsed() > timeout {
159159
return Err(anyhow::anyhow!("Timeout waiting for child process to exit"));
160160
}
161161
}

crashtracker/src/crash_info/telemetry.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ impl TelemetryCrashUploader {
136136

137137
let tracer_time = match &crash_info.timestamp {
138138
Some(ts) => ts.timestamp() as u64,
139-
None => SystemTime::now()
140-
.duration_since(SystemTime::UNIX_EPOCH)
139+
None => SystemTime::UNIX_EPOCH
140+
.elapsed()
141141
.map(|d| d.as_secs())
142142
.unwrap_or(0),
143143
};

data-pipeline/examples/send-traces-with-stats.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use data_pipeline::trace_exporter::{
77
use datadog_trace_protobuf::pb;
88
use std::{
99
collections::HashMap,
10-
time::{Duration, SystemTime, UNIX_EPOCH},
10+
time::{Duration, UNIX_EPOCH},
1111
};
1212

1313
fn get_span(now: i64, trace_id: u64, span_id: u64) -> pb::Span {
@@ -44,10 +44,7 @@ fn main() {
4444
.enable_stats(Duration::from_secs(10))
4545
.build()
4646
.unwrap();
47-
let now = SystemTime::now()
48-
.duration_since(UNIX_EPOCH)
49-
.unwrap()
50-
.as_nanos() as i64;
47+
let now = UNIX_EPOCH.elapsed().unwrap().as_nanos() as i64;
5148

5249
let mut traces = Vec::new();
5350
for trace_id in 1..=100 {

data-pipeline/src/span_concentrator/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,8 @@ mod aggregation;
1414
/// Return a Duration between t and the unix epoch
1515
/// If t is before the unix epoch return 0
1616
fn system_time_to_unix_duration(t: SystemTime) -> Duration {
17-
match t.duration_since(time::UNIX_EPOCH) {
18-
Err(_) => Duration::from_nanos(0),
19-
Ok(d) => d,
20-
}
17+
t.duration_since(time::UNIX_EPOCH)
18+
.unwrap_or(Duration::from_nanos(0))
2119
}
2220

2321
/// Align a timestamp on the start of a bucket

ddtelemetry/examples/tm-metrics-worker-test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ macro_rules! timeit {
1010
($op_name:literal, $op:block) => {{
1111
let start = std::time::Instant::now();
1212
let res = $op;
13-
let delta = std::time::Instant::now().duration_since(start);
13+
let delta = start.elapsed();
1414
println!(
1515
concat!($op_name, " took {} ms"),
1616
delta.as_secs_f64() * 1000.0

ddtelemetry/examples/tm-ping.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ fn build_request<'a>(
3232
) -> data::Telemetry<'a> {
3333
data::Telemetry {
3434
api_version: data::ApiVersion::V1,
35-
tracer_time: SystemTime::now()
36-
.duration_since(SystemTime::UNIX_EPOCH)
37-
.map(|d| d.as_secs())
38-
.unwrap_or(0),
35+
tracer_time: SystemTime::UNIX_EPOCH.elapsed().map_or(0, |d| d.as_secs()),
3936
runtime_id: "runtime_id",
4037
seq_id: seq_id(),
4138
application,

ddtelemetry/examples/tm-send-sketch.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@ fn build_request<'a>(
3030
) -> data::Telemetry<'a> {
3131
data::Telemetry {
3232
api_version: data::ApiVersion::V1,
33-
tracer_time: SystemTime::now()
34-
.duration_since(SystemTime::UNIX_EPOCH)
35-
.map(|d| d.as_secs())
36-
.unwrap_or(0),
33+
tracer_time: SystemTime::UNIX_EPOCH.elapsed().map_or(0, |d| d.as_secs()),
3734
runtime_id: "runtime_id",
3835
seq_id: seq_id(),
3936
application,

ddtelemetry/examples/tm-worker-test.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ macro_rules! timeit {
1313
($op_name:literal, $op:block) => {{
1414
let start = std::time::Instant::now();
1515
let res = $op;
16-
let delta = std::time::Instant::now().duration_since(start);
16+
let delta = start.elapsed();
1717
println!(
1818
concat!($op_name, " took {} ms"),
1919
delta.as_secs_f64() * 1000.0

ddtelemetry/src/metrics.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ use serde::{Deserialize, Serialize};
1414
use crate::data::{self, metrics};
1515

1616
fn unix_timestamp_now() -> u64 {
17-
time::SystemTime::now()
18-
.duration_since(time::SystemTime::UNIX_EPOCH)
19-
.map(|d| d.as_secs())
20-
.unwrap_or(0)
17+
time::SystemTime::UNIX_EPOCH
18+
.elapsed()
19+
.map_or(0, |d| d.as_secs())
2120
}
2221

2322
#[derive(Debug)]

0 commit comments

Comments
 (0)