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

feat: make background interval config public #225

Merged
merged 4 commits into from
Jun 23, 2024
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
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@

## Unreleased

## v0.6.7

- Add `Config::report_interval`: The background collector working interval.
- Deprecate `Config::batch_report_interval` and `Config::batch_report_max_spans`.
- Fix a performance issue in object-pool which was causing lock racing.

## v0.6.6

- Update to opentelemetry, opentelemetry_sdk, and opentelemetry-otlp.

## v0.6.5

- Update to opentelemetry 0.22, opentelemetry_sdk 0.22.1, and opentelemetry-otlp: 0.15.
Expand Down
4 changes: 2 additions & 2 deletions minitrace-datadog/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "minitrace-datadog"
version = "0.6.6"
version = "0.6.7"
authors = ["The TiKV Project Authors"]
license = "Apache-2.0"
edition = "2021"
Expand All @@ -14,7 +14,7 @@ keywords = ["tracing", "span", "datadog", "jaeger", "opentelemetry"]

[dependencies]
log = "0.4"
minitrace = { version = "0.6.6", path = "../minitrace" }
minitrace = { version = "0.6.7", path = "../minitrace" }
reqwest = { version = "0.11", features = ["blocking"] }
rmp-serde = "1"
serde = { version = "1", features = ["derive"] }
Expand Down
4 changes: 2 additions & 2 deletions minitrace-futures/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "minitrace-futures"
version = "0.6.6"
version = "0.6.7"
authors = ["The TiKV Project Authors"]
license = "Apache-2.0"
edition = "2021"
Expand All @@ -14,7 +14,7 @@ keywords = ["tracing", "span", "futures", "jaeger", "opentelemetry"]

[dependencies]
futures = "0.3"
minitrace = { version = "0.6.6", path = "../minitrace" }
minitrace = { version = "0.6.7", path = "../minitrace" }
pin-project-lite = "0.2.13"

[dev-dependencies]
Expand Down
15 changes: 8 additions & 7 deletions minitrace-futures/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ use pin_project_lite::pin_project;

/// An extension trait for [`futures::Stream`] that provides tracing instrument adapters.
pub trait StreamExt: futures::Stream + Sized {
/// Binds a [`Span`] to the [`Stream`] that continues to record until the stream is **finished**.
/// Binds a [`Span`] to the [`Stream`] that continues to record until the stream is
/// **finished**.
///
/// In addition, it sets the span as the local parent at every poll so that [`minitrace::local::LocalSpan`]
/// becomes available within the future. Internally, it calls [`Span::set_local_parent`] when
/// the executor polls it.
/// In addition, it sets the span as the local parent at every poll so that
/// [`minitrace::local::LocalSpan`] becomes available within the future. Internally, it
/// calls [`Span::set_local_parent`] when the executor polls it.
///
/// # Examples:
///
Expand Down Expand Up @@ -59,9 +60,9 @@ impl<T> StreamExt for T where T: futures::Stream {}
pub trait SinkExt<Item>: futures::Sink<Item> + Sized {
/// Binds a [`Span`] to the [`Sink`] that continues to record until the sink is **closed**.
///
/// In addition, it sets the span as the local parent at every poll so that [`minitrace::local::LocalSpan`]
/// becomes available within the future. Internally, it calls [`Span::set_local_parent`] when
/// the executor polls it.
/// In addition, it sets the span as the local parent at every poll so that
/// [`minitrace::local::LocalSpan`] becomes available within the future. Internally, it
/// calls [`Span::set_local_parent`] when the executor polls it.
///
/// # Examples:
///
Expand Down
4 changes: 2 additions & 2 deletions minitrace-jaeger/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "minitrace-jaeger"
version = "0.6.6"
version = "0.6.7"
authors = ["The TiKV Project Authors"]
license = "Apache-2.0"
edition = "2021"
Expand All @@ -14,7 +14,7 @@ keywords = ["tracing", "span", "datadog", "jaeger", "opentelemetry"]

[dependencies]
log = "0.4"
minitrace = { version = "0.6.6", path = "../minitrace" }
minitrace = { version = "0.6.7", path = "../minitrace" }
thrift_codec = "0.3"

[dev-dependencies]
Expand Down
4 changes: 2 additions & 2 deletions minitrace-macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "minitrace-macro"
version = "0.6.6"
version = "0.6.7"
authors = ["The TiKV Project Authors"]
license = "Apache-2.0"
edition = "2021"
Expand All @@ -24,7 +24,7 @@ syn = { version = "1.0.84", features = ["full", "parsing", "extra-traits", "proc

[dev-dependencies]
logcall = "0.1.4"
minitrace = { version = "0.6.6", path = "../minitrace" }
minitrace = { version = "0.6.7", path = "../minitrace" }
tokio = { version = "1", features = ["full"] }
trybuild = "1"
# The procedural macro `trace` only supports async-trait higher than 0.1.52
Expand Down
26 changes: 14 additions & 12 deletions minitrace-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
//! An attribute macro designed to eliminate boilerplate code for [`minitrace`](https://crates.io/crates/minitrace).

#![recursion_limit = "256"]
// Instrumenting the async fn is not as straight forward as expected because `async_trait` rewrites `async fn`
// into a normal fn which returns `Box<impl Future>`, and this stops the macro from distinguishing `async fn` from `fn`.
// The following code reused the `async_trait` probes from [tokio-tracing](https://github.com/tokio-rs/tracing/blob/6a61897a5e834988ad9ac709e28c93c4dbf29116/tracing-attributes/src/expand.rs).
// Instrumenting the async fn is not as straight forward as expected because `async_trait` rewrites
// `async fn` into a normal fn which returns `Box<impl Future>`, and this stops the macro from
// distinguishing `async fn` from `fn`. The following code reused the `async_trait` probes from [tokio-tracing](https://github.com/tokio-rs/tracing/blob/6a61897a5e834988ad9ac709e28c93c4dbf29116/tracing-attributes/src/expand.rs).

extern crate proc_macro;

Expand Down Expand Up @@ -107,21 +107,23 @@ impl Parse for Args {

/// An attribute macro designed to eliminate boilerplate code.
///
/// This macro automatically creates a span for the annotated function. The span name defaults to the function
/// name but can be customized by passing a string literal as an argument using the `name` parameter.
/// This macro automatically creates a span for the annotated function. The span name defaults to
/// the function name but can be customized by passing a string literal as an argument using the
/// `name` parameter.
///
/// The `#[trace]` attribute requires a local parent context to function correctly. Ensure that
/// the function annotated with `#[trace]` is called within __a local context of a `Span`__, which is
/// established by invoking the `Span::set_local_parent()` method.
/// the function annotated with `#[trace]` is called within __a local context of a `Span`__, which
/// is established by invoking the `Span::set_local_parent()` method.
///
/// ## Arguments
///
/// * `name` - The name of the span. Defaults to the full path of the function.
/// * `short_name` - Whether to use the function name without path as the span name. Defaults to `false`.
/// * `enter_on_poll` - Whether to enter the span on poll. If set to `false`, `in_span` will be used.
/// Only available for `async fn`. Defaults to `false`.
/// * `properties` - A list of key-value pairs to be added as properties to the span. The value can be
/// a format string, where the function arguments are accessible. Defaults to `{}`.
/// * `short_name` - Whether to use the function name without path as the span name. Defaults to
/// `false`.
/// * `enter_on_poll` - Whether to enter the span on poll. If set to `false`, `in_span` will be
/// used. Only available for `async fn`. Defaults to `false`.
/// * `properties` - A list of key-value pairs to be added as properties to the span. The value can
/// be a format string, where the function arguments are accessible. Defaults to `{}`.
///
/// # Examples
///
Expand Down
25 changes: 10 additions & 15 deletions minitrace-macro/tests/ui/ok/async-in-trait.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
#![cfg_attr(all(feature = "nightly", test), feature(async_fn_in_trait))]
trait MyTrait {
async fn work(&self) -> usize;
}

#[cfg(all(feature = "nightly", test))]
mod tests {
trait MyTrait {
async fn work(&self) -> usize;
}

struct MyStruct;

impl MyTrait for MyStruct {
// #[logcall::logcall("info")]
#[minitrace::trace]
async fn work(&self) -> usize {
todo!()
}
struct MyStruct;

impl MyTrait for MyStruct {
// #[logcall::logcall("info")]
#[minitrace::trace]
async fn work(&self) -> usize {
todo!()
}
}

Expand Down
4 changes: 2 additions & 2 deletions minitrace-opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "minitrace-opentelemetry"
version = "0.6.6"
version = "0.6.7"
authors = ["The TiKV Project Authors"]
license = "Apache-2.0"
edition = "2021"
Expand All @@ -15,7 +15,7 @@ keywords = ["tracing", "span", "datadog", "jaeger", "opentelemetry"]
[dependencies]
futures = { version = "0.3", features = ["executor"] }
log = "0.4"
minitrace = { version = "0.6.6", path = "../minitrace" }
minitrace = { version = "0.6.7", path = "../minitrace" }
opentelemetry = { version = "0.23", features = ["trace"] }
opentelemetry_sdk = { version = "0.23", features = ["trace"] }

Expand Down
15 changes: 7 additions & 8 deletions minitrace/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "minitrace"
version = "0.6.6"
version = "0.6.7"
authors = ["The TiKV Project Authors"]
license = "Apache-2.0"
edition = "2021"
Expand All @@ -16,7 +16,7 @@ keywords = ["tracing", "span", "datadog", "jaeger", "opentelemetry"]
enable = []

[dependencies]
minitrace-macro = { version = "0.6.6", path = "../minitrace-macro" }
minitrace-macro = { version = "0.6.7", path = "../minitrace-macro" }
minstant = "0.1.7"
parking_lot = "0.12"
pin-project = "1"
Expand All @@ -26,25 +26,24 @@ rand = "0.8"
rtrb = "0.2"

[dev-dependencies]
# The procedural macro `trace` only supports async-trait higher than 0.1.52
async-trait = "0.1.52"
criterion = { version = "0.5", features = ["html_reports"] }
crossbeam = "0.8"
env_logger = "0.11"
flume = "0.11.0"
futures = "0.3"
futures-timer = "3"
log = "0.4"
logcall = "0.1.4"
minitrace = { path = ".", features = ["enable"] }
minitrace-datadog = { version = "0.6.6", path = "../minitrace-datadog" }
minitrace-jaeger = { version = "0.6.6", path = "../minitrace-jaeger" }
minitrace-opentelemetry = { version = "0.6.6", path = "../minitrace-opentelemetry" }
minitrace-datadog = { version = "0.6.7", path = "../minitrace-datadog" }
minitrace-jaeger = { version = "0.6.7", path = "../minitrace-jaeger" }
minitrace-opentelemetry = { version = "0.6.7", path = "../minitrace-opentelemetry" }
mockall = "0.12"
once_cell = "1"
opentelemetry-otlp = { version = "0.16", features = ["trace"] }
opentelemetry = { version = "0.23", features = ["trace"] }
opentelemetry-otlp = { version = "0.16", features = ["trace"] }
opentelemetry_sdk = { version = "0.23", features = ["trace"] }

rand = "0.8"
rustracing = "0.6"
serial_test = "3"
Expand Down
40 changes: 40 additions & 0 deletions minitrace/benches/spsc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,29 @@ fn spsc_comparison(c: &mut Criterion) {
total_time
})
});
bgroup.bench_function(format!("flume/{}", len), |b| {
b.iter_custom(|iters| {
let mut total_time = Duration::default();
for _ in 0..iters {
let (tx, rx) = flume::bounded(10240);

let start = Instant::now();

std::thread::spawn(move || {
for i in 0..len {
while tx.send(i).is_err() {}
}
});

for _ in 0..len {
while rx.recv().is_err() {}
}

total_time += start.elapsed();
}
total_time
})
});
bgroup.bench_function(format!("minitrace/{}", len), |b| {
b.iter_custom(|iters| {
let mut total_time = Duration::default();
Expand Down Expand Up @@ -154,6 +177,23 @@ fn spsc_send_only_comparison(c: &mut Criterion) {
total_time
})
});
bgroup.bench_function(format!("flume/{}", len), |b| {
b.iter_custom(|iters| {
let mut total_time = Duration::default();
for _ in 0..iters {
let (tx, _rx) = flume::bounded(10240);

let start = Instant::now();

for i in 0..len {
tx.send(i).unwrap();
}

total_time += start.elapsed();
}
total_time
})
});
bgroup.bench_function(format!("minitrace/{}", len), |b| {
b.iter_custom(|iters| {
let mut total_time = Duration::default();
Expand Down
42 changes: 42 additions & 0 deletions minitrace/examples/tail_sampling.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use std::time::Duration;

use minitrace::collector::Config;
use minitrace::collector::ConsoleReporter;
use minitrace::prelude::*;

fn main() {
minitrace::set_reporter(ConsoleReporter, Config::default());

{
let parent = SpanContext::random();
let mut root = Span::root("light work", parent);
let _span_guard = root.set_local_parent();

expensive_work(Duration::from_millis(50));

// Cancel the trace to avoid reporting if it's too short.
if root.elapsed() < Some(Duration::from_millis(100)) {
root.cancel();
}
};

{
let parent = SpanContext::random();
let mut root = Span::root("heavy work", parent);
let _span_guard = root.set_local_parent();

expensive_work(Duration::from_millis(200));

// This trace will be reported.
if root.elapsed() < Some(Duration::from_millis(100)) {
root.cancel();
}
};

minitrace::flush();
}

#[trace]
fn expensive_work(time: Duration) {
std::thread::sleep(time);
}
Loading
Loading