Skip to content

Commit c9cdad5

Browse files
authored
feat: add Span::elapsed() (#172)
* feat: add Span::elapsed() Signed-off-by: Andy Lok <[email protected]> * Update lib.rs Signed-off-by: Andy Lok [email protected] * fix Signed-off-by: Andy Lok <[email protected]> * fix Signed-off-by: Andy Lok <[email protected]> --------- Signed-off-by: Andy Lok <[email protected]> Signed-off-by: Andy Lok [email protected]
1 parent f57f8b9 commit c9cdad5

File tree

6 files changed

+68
-11
lines changed

6 files changed

+68
-11
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
- Macro will use the full path of the function as span name instead of the only function name. You can turn it off by setting `#[trace(short_name = true)]`.
88
- Add utility macros `func_name!()`, `full_name!()`, and `file_location!()` to generate names for use in span.
9+
- Add `Span::elapsed()` that returns the elapsed time since the span is created.
910

1011
## v0.6.0
1112

minitrace/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
//! minitrace = "0.5"
2020
//! ```
2121
//!
22-
//! Libraries can attach their spans to the caller's span (if available) via the API boundary.
22+
//! Libraries can attach their spans to the caller's span (if caller has set [local parent](#local-span))
23+
//! via the API boundary.
2324
//!
2425
//! ```
2526
//! use minitrace::prelude::*;

minitrace/src/local/local_span.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl LocalSpan {
8484
/// use minitrace::prelude::*;
8585
///
8686
/// let span = LocalSpan::enter_with_local_parent("a child span")
87-
/// .with_properties(|| vec![("key1", "value1"), ("key2", "value2")]);
87+
/// .with_properties(|| [("key1", "value1"), ("key2", "value2")]);
8888
/// ```
8989
#[inline]
9090
pub fn with_properties<K, V, I, F>(self, properties: F) -> Self

minitrace/src/span.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::borrow::Cow;
44
use std::cell::RefCell;
55
use std::rc::Rc;
66
use std::sync::Arc;
7+
use std::time::Duration;
78

89
use minstant::Instant;
910

@@ -269,7 +270,7 @@ impl Span {
269270
/// use minitrace::prelude::*;
270271
///
271272
/// let root = Span::root("root", SpanContext::random())
272-
/// .with_properties(|| vec![("key1", "value1"), ("key2", "value2")]);
273+
/// .with_properties(|| [("key1", "value1"), ("key2", "value2")]);
273274
/// ```
274275
#[inline]
275276
pub fn with_properties<K, V, I, F>(mut self, properties: F) -> Self
@@ -323,6 +324,36 @@ impl Span {
323324
}
324325
}
325326

327+
/// Returns the elapsed time since the span was created. If the `Span` is a noop span,
328+
/// this function will return `None`.
329+
///
330+
/// # Examples
331+
///
332+
/// ```
333+
/// use minitrace::prelude::*;
334+
/// use std::time::Duration;
335+
///
336+
/// let mut root = Span::root("root", SpanContext::random());
337+
///
338+
/// // ...
339+
///
340+
/// if root
341+
/// .elapsed()
342+
/// .map(|elapsed| elapsed < Duration::from_secs(1))
343+
/// .unwrap_or(false)
344+
/// {
345+
/// root.cancel();
346+
/// }
347+
#[inline]
348+
pub fn elapsed(&self) -> Option<Duration> {
349+
#[cfg(feature = "enable")]
350+
if let Some(inner) = self.inner.as_ref() {
351+
return Some(inner.raw_span.begin_instant.elapsed());
352+
}
353+
354+
None
355+
}
356+
326357
/// Dismisses the trace, preventing the reporting of any span records associated with it.
327358
///
328359
/// This is particularly useful when focusing on the tail latency of a program. For instant,

minitrace/tests/lib.rs

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
// Copyright 2021 TiKV Project Authors. Licensed under Apache-2.0.
22

3+
use std::time::Duration;
4+
35
use futures::executor::block_on;
46
use minitrace::collector::Config;
7+
use minitrace::collector::ConsoleReporter;
58
use minitrace::collector::TestReporter;
69
use minitrace::local::LocalCollector;
710
use minitrace::prelude::*;
@@ -393,7 +396,7 @@ fn test_macro() {
393396
#[trace(short_name = true, enter_on_poll = true)]
394397
async fn work(millis: &u64) {
395398
let _g = Span::enter_with_local_parent("work-inner");
396-
tokio::time::sleep(std::time::Duration::from_millis(*millis))
399+
tokio::time::sleep(Duration::from_millis(*millis))
397400
.enter_on_poll("sleep")
398401
.await;
399402
}
@@ -402,7 +405,7 @@ fn test_macro() {
402405
#[trace(short_name = true)]
403406
async fn work2(&self, millis: &u64) {
404407
let _g = Span::enter_with_local_parent("work-inner");
405-
tokio::time::sleep(std::time::Duration::from_millis(*millis))
408+
tokio::time::sleep(Duration::from_millis(*millis))
406409
.enter_on_poll("sleep")
407410
.await;
408411
}
@@ -411,10 +414,10 @@ fn test_macro() {
411414
#[trace(short_name = true)]
412415
async fn work3<'a>(millis1: &'a u64, millis2: &u64) {
413416
let _g = Span::enter_with_local_parent("work-inner");
414-
tokio::time::sleep(std::time::Duration::from_millis(*millis1))
417+
tokio::time::sleep(Duration::from_millis(*millis1))
415418
.enter_on_poll("sleep")
416419
.await;
417-
tokio::time::sleep(std::time::Duration::from_millis(*millis2))
420+
tokio::time::sleep(Duration::from_millis(*millis2))
418421
.enter_on_poll("sleep")
419422
.await;
420423
}
@@ -478,22 +481,22 @@ root []
478481
fn macro_example() {
479482
#[trace(short_name = true)]
480483
fn do_something_short_name(i: u64) {
481-
std::thread::sleep(std::time::Duration::from_millis(i));
484+
std::thread::sleep(Duration::from_millis(i));
482485
}
483486

484487
#[trace(short_name = true)]
485488
async fn do_something_async_short_name(i: u64) {
486-
futures_timer::Delay::new(std::time::Duration::from_millis(i)).await;
489+
futures_timer::Delay::new(Duration::from_millis(i)).await;
487490
}
488491

489492
#[trace]
490493
fn do_something(i: u64) {
491-
std::thread::sleep(std::time::Duration::from_millis(i));
494+
std::thread::sleep(Duration::from_millis(i));
492495
}
493496

494497
#[trace]
495498
async fn do_something_async(i: u64) {
496-
futures_timer::Delay::new(std::time::Duration::from_millis(i)).await;
499+
futures_timer::Delay::new(Duration::from_millis(i)).await;
497500
}
498501

499502
let (reporter, collected_spans) = TestReporter::new();
@@ -636,3 +639,22 @@ root []
636639
expected_graph
637640
);
638641
}
642+
643+
#[test]
644+
#[serial]
645+
fn test_elapsed() {
646+
minitrace::set_reporter(ConsoleReporter, Config::default());
647+
648+
{
649+
let root = Span::root("root", SpanContext::random());
650+
651+
std::thread::sleep(Duration::from_millis(50));
652+
653+
let elapsed = root.elapsed().unwrap();
654+
655+
assert!(elapsed >= Duration::from_millis(50));
656+
assert!(elapsed < Duration::from_millis(65));
657+
}
658+
659+
minitrace::flush();
660+
}

test-statically-disable/src/main.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ fn main() {
5353
assert!(SpanContext::current_local_parent().is_none());
5454
assert!(SpanContext::from_span(&span5).is_none());
5555

56+
assert!(root.elapsed().is_none());
57+
5658
root.cancel();
5759

5860
minitrace::flush();

0 commit comments

Comments
 (0)