Skip to content

Commit

Permalink
feat: add Span::elapsed() (#172)
Browse files Browse the repository at this point in the history
* 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]
  • Loading branch information
andylokandy authored Oct 28, 2023
1 parent f57f8b9 commit c9cdad5
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 11 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

- 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)]`.
- Add utility macros `func_name!()`, `full_name!()`, and `file_location!()` to generate names for use in span.
- Add `Span::elapsed()` that returns the elapsed time since the span is created.

## v0.6.0

Expand Down
3 changes: 2 additions & 1 deletion minitrace/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
//! minitrace = "0.5"
//! ```
//!
//! Libraries can attach their spans to the caller's span (if available) via the API boundary.
//! Libraries can attach their spans to the caller's span (if caller has set [local parent](#local-span))
//! via the API boundary.
//!
//! ```
//! use minitrace::prelude::*;
Expand Down
2 changes: 1 addition & 1 deletion minitrace/src/local/local_span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl LocalSpan {
/// use minitrace::prelude::*;
///
/// let span = LocalSpan::enter_with_local_parent("a child span")
/// .with_properties(|| vec![("key1", "value1"), ("key2", "value2")]);
/// .with_properties(|| [("key1", "value1"), ("key2", "value2")]);
/// ```
#[inline]
pub fn with_properties<K, V, I, F>(self, properties: F) -> Self
Expand Down
33 changes: 32 additions & 1 deletion minitrace/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::borrow::Cow;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::Arc;
use std::time::Duration;

use minstant::Instant;

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

/// Returns the elapsed time since the span was created. If the `Span` is a noop span,
/// this function will return `None`.
///
/// # Examples
///
/// ```
/// use minitrace::prelude::*;
/// use std::time::Duration;
///
/// let mut root = Span::root("root", SpanContext::random());
///
/// // ...
///
/// if root
/// .elapsed()
/// .map(|elapsed| elapsed < Duration::from_secs(1))
/// .unwrap_or(false)
/// {
/// root.cancel();
/// }
#[inline]
pub fn elapsed(&self) -> Option<Duration> {
#[cfg(feature = "enable")]
if let Some(inner) = self.inner.as_ref() {
return Some(inner.raw_span.begin_instant.elapsed());
}

None
}

/// Dismisses the trace, preventing the reporting of any span records associated with it.
///
/// This is particularly useful when focusing on the tail latency of a program. For instant,
Expand Down
38 changes: 30 additions & 8 deletions minitrace/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright 2021 TiKV Project Authors. Licensed under Apache-2.0.

use std::time::Duration;

use futures::executor::block_on;
use minitrace::collector::Config;
use minitrace::collector::ConsoleReporter;
use minitrace::collector::TestReporter;
use minitrace::local::LocalCollector;
use minitrace::prelude::*;
Expand Down Expand Up @@ -393,7 +396,7 @@ fn test_macro() {
#[trace(short_name = true, enter_on_poll = true)]
async fn work(millis: &u64) {
let _g = Span::enter_with_local_parent("work-inner");
tokio::time::sleep(std::time::Duration::from_millis(*millis))
tokio::time::sleep(Duration::from_millis(*millis))
.enter_on_poll("sleep")
.await;
}
Expand All @@ -402,7 +405,7 @@ fn test_macro() {
#[trace(short_name = true)]
async fn work2(&self, millis: &u64) {
let _g = Span::enter_with_local_parent("work-inner");
tokio::time::sleep(std::time::Duration::from_millis(*millis))
tokio::time::sleep(Duration::from_millis(*millis))
.enter_on_poll("sleep")
.await;
}
Expand All @@ -411,10 +414,10 @@ fn test_macro() {
#[trace(short_name = true)]
async fn work3<'a>(millis1: &'a u64, millis2: &u64) {
let _g = Span::enter_with_local_parent("work-inner");
tokio::time::sleep(std::time::Duration::from_millis(*millis1))
tokio::time::sleep(Duration::from_millis(*millis1))
.enter_on_poll("sleep")
.await;
tokio::time::sleep(std::time::Duration::from_millis(*millis2))
tokio::time::sleep(Duration::from_millis(*millis2))
.enter_on_poll("sleep")
.await;
}
Expand Down Expand Up @@ -478,22 +481,22 @@ root []
fn macro_example() {
#[trace(short_name = true)]
fn do_something_short_name(i: u64) {
std::thread::sleep(std::time::Duration::from_millis(i));
std::thread::sleep(Duration::from_millis(i));
}

#[trace(short_name = true)]
async fn do_something_async_short_name(i: u64) {
futures_timer::Delay::new(std::time::Duration::from_millis(i)).await;
futures_timer::Delay::new(Duration::from_millis(i)).await;
}

#[trace]
fn do_something(i: u64) {
std::thread::sleep(std::time::Duration::from_millis(i));
std::thread::sleep(Duration::from_millis(i));
}

#[trace]
async fn do_something_async(i: u64) {
futures_timer::Delay::new(std::time::Duration::from_millis(i)).await;
futures_timer::Delay::new(Duration::from_millis(i)).await;
}

let (reporter, collected_spans) = TestReporter::new();
Expand Down Expand Up @@ -636,3 +639,22 @@ root []
expected_graph
);
}

#[test]
#[serial]
fn test_elapsed() {
minitrace::set_reporter(ConsoleReporter, Config::default());

{
let root = Span::root("root", SpanContext::random());

std::thread::sleep(Duration::from_millis(50));

let elapsed = root.elapsed().unwrap();

assert!(elapsed >= Duration::from_millis(50));
assert!(elapsed < Duration::from_millis(65));
}

minitrace::flush();
}
2 changes: 2 additions & 0 deletions test-statically-disable/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ fn main() {
assert!(SpanContext::current_local_parent().is_none());
assert!(SpanContext::from_span(&span5).is_none());

assert!(root.elapsed().is_none());

root.cancel();

minitrace::flush();
Expand Down

0 comments on commit c9cdad5

Please sign in to comment.