Skip to content
This repository was archived by the owner on Nov 24, 2025. It is now read-only.

Commit 2dd03af

Browse files
tqwewepawurb
andauthored
feat: accept expressions for labels in instrument macro (#15)
* feat: accept expressions for labels in instrument macro * Add support and tests for dynamic labels --------- Co-authored-by: pawurb <[email protected]>
1 parent 15acbd6 commit 2dd03af

File tree

15 files changed

+195
-112
lines changed

15 files changed

+195
-112
lines changed

crates/channels-console-futures-test/examples/basic_futures.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,28 @@ use futures_util::stream::StreamExt;
22
use smol::Timer;
33
use std::time::Duration;
44

5+
struct Actor {
6+
name: String,
7+
}
8+
59
#[allow(unused_mut)]
610
fn main() {
711
smol::block_on(async {
12+
let actor1 = Actor {
13+
name: "Actor 1".to_string(),
14+
};
15+
816
#[cfg(feature = "channels-console")]
917
let _channels_guard = channels_console::ChannelsGuard::new();
1018

1119
let (txa, mut _rxa) = futures_channel::mpsc::unbounded::<i32>();
1220
#[cfg(feature = "channels-console")]
13-
let (txa, mut _rxa) = channels_console::instrument!((txa, _rxa));
21+
let (txa, mut _rxa) = channels_console::instrument!((txa, _rxa), label = actor1.name);
1422

1523
let (mut txb, mut rxb) = futures_channel::mpsc::channel::<i32>(10);
1624
#[cfg(feature = "channels-console")]
17-
let (mut txb, mut rxb) = channels_console::instrument!((txb, rxb), capacity = 10);
25+
let (mut txb, mut rxb) =
26+
channels_console::instrument!((txb, rxb), capacity = 10, label = "bounded-channel");
1827

1928
let (txc, rxc) = futures_channel::oneshot::channel::<String>();
2029
#[cfg(feature = "channels-console")]

crates/channels-console-futures-test/examples/iter_futures.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
use smol::Timer;
22
use std::time::Duration;
33

4+
struct Actor {
5+
name: String,
6+
}
7+
48
#[allow(unused_mut)]
59
fn main() {
610
smol::block_on(async {
11+
let actor1 = Actor {
12+
name: "Actor 1".to_string(),
13+
};
14+
715
#[cfg(feature = "channels-console")]
816
let _channels_guard = channels_console::ChannelsGuard::new();
917

@@ -14,7 +22,7 @@ fn main() {
1422
let (tx, mut rx) = futures_channel::mpsc::unbounded::<i32>();
1523

1624
#[cfg(feature = "channels-console")]
17-
let (tx, mut rx) = channels_console::instrument!((tx, rx));
25+
let (tx, mut rx) = channels_console::instrument!((tx, rx), label = actor1.name.clone());
1826

1927
println!(" - Created unbounded channel {}", i);
2028

crates/channels-console-std-test/examples/basic_std.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
use std::thread;
22
use std::time::Duration;
33

4+
struct Actor {
5+
name: String,
6+
}
7+
48
fn main() {
9+
let actor1 = Actor {
10+
name: "Actor 1".to_string(),
11+
};
12+
513
#[cfg(feature = "channels-console")]
614
let _channels_guard = channels_console::ChannelsGuard::new();
715

816
let (txa, _rxa) = std::sync::mpsc::channel::<i32>();
917
#[cfg(feature = "channels-console")]
10-
let (txa, _rxa) = channels_console::instrument!((txa, _rxa));
18+
let (txa, _rxa) = channels_console::instrument!((txa, _rxa), label = "unbounded-channel");
1119

1220
let (txb, rxb) = std::sync::mpsc::sync_channel::<i32>(10);
1321
#[cfg(feature = "channels-console")]
14-
let (txb, rxb) = channels_console::instrument!((txb, rxb), capacity = 10, label = "zzz");
22+
let (txb, rxb) = channels_console::instrument!((txb, rxb), capacity = 10, label = actor1.name);
1523

1624
let sender_handle = thread::spawn(move || {
1725
for i in 1..=3 {

crates/channels-console-std-test/examples/iter_std.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@ use std::sync::mpsc;
22
use std::thread;
33
use std::time::Duration;
44

5+
struct Actor {
6+
name: String,
7+
}
8+
59
fn main() {
10+
let actor1 = Actor {
11+
name: "Actor 1".to_string(),
12+
};
13+
614
#[cfg(feature = "channels-console")]
715
let _channels_guard = channels_console::ChannelsGuard::new();
816

@@ -14,7 +22,7 @@ fn main() {
1422
let (tx, rx) = mpsc::channel::<i32>();
1523

1624
#[cfg(feature = "channels-console")]
17-
let (tx, rx) = channels_console::instrument!((tx, rx));
25+
let (tx, rx) = channels_console::instrument!((tx, rx), label = actor1.name.clone());
1826

1927
println!(" - Created unbounded channel {}", i);
2028

crates/channels-console-tokio-test/examples/basic_tokio.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
1+
struct Actor {
2+
name: String,
3+
}
4+
15
#[allow(unused_mut)]
26
#[tokio::main]
37
async fn main() {
8+
let actor1 = Actor {
9+
name: "Actor 1".to_string(),
10+
};
11+
412
#[cfg(feature = "channels-console")]
513
let _channels_guard = channels_console::ChannelsGuard::new();
614

715
let (txa, mut _rxa) = tokio::sync::mpsc::unbounded_channel::<i32>();
816

917
#[cfg(feature = "channels-console")]
10-
let (txa, _rxa) = channels_console::instrument!((txa, _rxa), log = true);
18+
let (txa, _rxa) = channels_console::instrument!((txa, _rxa), log = true, label = actor1.name);
1119

1220
let (txb, mut rxb) = tokio::sync::mpsc::channel::<i32>(10);
1321
#[cfg(feature = "channels-console")]
14-
let (txb, mut rxb) = channels_console::instrument!((txb, rxb));
22+
let (txb, mut rxb) = channels_console::instrument!((txb, rxb), label = "bounded-channel");
1523

1624
let (txc, rxc) = tokio::sync::oneshot::channel::<String>();
1725
#[cfg(feature = "channels-console")]

crates/channels-console-tokio-test/examples/iter_tokio.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
1+
struct Actor {
2+
name: String,
3+
}
4+
15
#[allow(unused_mut)]
26
#[tokio::main]
37
async fn main() {
8+
let actor1 = Actor {
9+
name: "Actor 1".to_string(),
10+
};
11+
412
#[cfg(feature = "channels-console")]
513
let _channels_guard = channels_console::ChannelsGuard::new();
614

@@ -11,7 +19,7 @@ async fn main() {
1119
let (tx, mut rx) = tokio::sync::mpsc::channel::<i32>(10);
1220

1321
#[cfg(feature = "channels-console")]
14-
let (tx, mut rx) = channels_console::instrument!((tx, rx), label = "bounded");
22+
let (tx, mut rx) = channels_console::instrument!((tx, rx), label = actor1.name.clone());
1523

1624
println!(" - Created bounded channel {}", i);
1725

crates/channels-console/src/channels_guard.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ impl Drop for ChannelsGuard {
140140
for channel_stats in stats {
141141
let label = resolve_label(
142142
channel_stats.source,
143-
channel_stats.label,
143+
channel_stats.label.as_deref(),
144144
channel_stats.iter,
145145
);
146146
table.add_row(Row::new(vec![

crates/channels-console/src/lib.rs

Lines changed: 61 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ impl<'de> Deserialize<'de> for ChannelState {
150150
pub(crate) struct ChannelStats {
151151
pub(crate) id: u64,
152152
pub(crate) source: &'static str,
153-
pub(crate) label: Option<&'static str>,
153+
pub(crate) label: Option<String>,
154154
pub(crate) channel_type: ChannelType,
155155
pub(crate) state: ChannelState,
156156
pub(crate) sent_count: u64,
@@ -203,7 +203,7 @@ pub struct SerializableChannelStats {
203203

204204
impl From<&ChannelStats> for SerializableChannelStats {
205205
fn from(stats: &ChannelStats) -> Self {
206-
let label = resolve_label(stats.source, stats.label, stats.iter);
206+
let label = resolve_label(stats.source, stats.label.as_deref(), stats.iter);
207207

208208
Self {
209209
id: stats.id,
@@ -227,7 +227,7 @@ impl ChannelStats {
227227
fn new(
228228
id: u64,
229229
source: &'static str,
230-
label: Option<&'static str>,
230+
label: Option<String>,
231231
channel_type: ChannelType,
232232
type_name: &'static str,
233233
type_size: usize,
@@ -275,7 +275,7 @@ pub(crate) enum StatsEvent {
275275
Created {
276276
id: u64,
277277
source: &'static str,
278-
display_label: Option<&'static str>,
278+
display_label: Option<String>,
279279
channel_type: ChannelType,
280280
type_name: &'static str,
281281
type_size: usize,
@@ -424,7 +424,7 @@ fn init_stats_state() -> &'static StatsState {
424424
})
425425
}
426426

427-
fn resolve_label(id: &'static str, provided: Option<&'static str>, iter: u32) -> String {
427+
fn resolve_label(id: &'static str, provided: Option<&str>, iter: u32) -> String {
428428
let base_label = if let Some(l) = provided {
429429
l.to_string()
430430
} else if let Some(pos) = id.rfind(':') {
@@ -486,7 +486,7 @@ pub trait Instrument {
486486
fn instrument(
487487
self,
488488
source: &'static str,
489-
label: Option<&'static str>,
489+
label: Option<String>,
490490
capacity: Option<usize>,
491491
) -> Self::Output;
492492
}
@@ -500,7 +500,7 @@ pub trait InstrumentLog {
500500
fn instrument_log(
501501
self,
502502
source: &'static str,
503-
label: Option<&'static str>,
503+
label: Option<String>,
504504
capacity: Option<usize>,
505505
) -> Self::Output;
506506
}
@@ -593,9 +593,9 @@ macro_rules! instrument {
593593
$crate::Instrument::instrument($expr, CHANNEL_ID, None, None)
594594
}};
595595

596-
($expr:expr, label = $label:literal) => {{
596+
($expr:expr, label = $label:expr) => {{
597597
const CHANNEL_ID: &'static str = concat!(file!(), ":", line!());
598-
$crate::Instrument::instrument($expr, CHANNEL_ID, Some($label), None)
598+
$crate::Instrument::instrument($expr, CHANNEL_ID, Some($label.to_string()), None)
599599
}};
600600

601601
($expr:expr, capacity = $capacity:expr) => {{
@@ -604,16 +604,16 @@ macro_rules! instrument {
604604
$crate::Instrument::instrument($expr, CHANNEL_ID, None, Some($capacity))
605605
}};
606606

607-
($expr:expr, label = $label:literal, capacity = $capacity:expr) => {{
607+
($expr:expr, label = $label:expr, capacity = $capacity:expr) => {{
608608
const CHANNEL_ID: &'static str = concat!(file!(), ":", line!());
609609
const _: usize = $capacity;
610-
$crate::Instrument::instrument($expr, CHANNEL_ID, Some($label), Some($capacity))
610+
$crate::Instrument::instrument($expr, CHANNEL_ID, Some($label.to_string()), Some($capacity))
611611
}};
612612

613-
($expr:expr, capacity = $capacity:expr, label = $label:literal) => {{
613+
($expr:expr, capacity = $capacity:expr, label = $label:expr) => {{
614614
const CHANNEL_ID: &'static str = concat!(file!(), ":", line!());
615615
const _: usize = $capacity;
616-
$crate::Instrument::instrument($expr, CHANNEL_ID, Some($label), Some($capacity))
616+
$crate::Instrument::instrument($expr, CHANNEL_ID, Some($label.to_string()), Some($capacity))
617617
}};
618618

619619
// Variants with log = true
@@ -622,14 +622,14 @@ macro_rules! instrument {
622622
$crate::InstrumentLog::instrument_log($expr, CHANNEL_ID, None, None)
623623
}};
624624

625-
($expr:expr, label = $label:literal, log = true) => {{
625+
($expr:expr, label = $label:expr, log = true) => {{
626626
const CHANNEL_ID: &'static str = concat!(file!(), ":", line!());
627-
$crate::InstrumentLog::instrument_log($expr, CHANNEL_ID, Some($label), None)
627+
$crate::InstrumentLog::instrument_log($expr, CHANNEL_ID, Some($label.to_string()), None)
628628
}};
629629

630-
($expr:expr, log = true, label = $label:literal) => {{
630+
($expr:expr, log = true, label = $label:expr) => {{
631631
const CHANNEL_ID: &'static str = concat!(file!(), ":", line!());
632-
$crate::InstrumentLog::instrument_log($expr, CHANNEL_ID, Some($label), None)
632+
$crate::InstrumentLog::instrument_log($expr, CHANNEL_ID, Some($label.to_string()), None)
633633
}};
634634

635635
($expr:expr, capacity = $capacity:expr, log = true) => {{
@@ -644,40 +644,70 @@ macro_rules! instrument {
644644
$crate::InstrumentLog::instrument_log($expr, CHANNEL_ID, None, Some($capacity))
645645
}};
646646

647-
($expr:expr, label = $label:literal, capacity = $capacity:expr, log = true) => {{
647+
($expr:expr, label = $label:expr, capacity = $capacity:expr, log = true) => {{
648648
const CHANNEL_ID: &'static str = concat!(file!(), ":", line!());
649649
const _: usize = $capacity;
650-
$crate::InstrumentLog::instrument_log($expr, CHANNEL_ID, Some($label), Some($capacity))
650+
$crate::InstrumentLog::instrument_log(
651+
$expr,
652+
CHANNEL_ID,
653+
Some($label.to_string()),
654+
Some($capacity),
655+
)
651656
}};
652657

653-
($expr:expr, label = $label:literal, log = true, capacity = $capacity:expr) => {{
658+
($expr:expr, label = $label:expr, log = true, capacity = $capacity:expr) => {{
654659
const CHANNEL_ID: &'static str = concat!(file!(), ":", line!());
655660
const _: usize = $capacity;
656-
$crate::InstrumentLog::instrument_log($expr, CHANNEL_ID, Some($label), Some($capacity))
661+
$crate::InstrumentLog::instrument_log(
662+
$expr,
663+
CHANNEL_ID,
664+
Some($label.to_string()),
665+
Some($capacity),
666+
)
657667
}};
658668

659-
($expr:expr, capacity = $capacity:expr, label = $label:literal, log = true) => {{
669+
($expr:expr, capacity = $capacity:expr, label = $label:expr, log = true) => {{
660670
const CHANNEL_ID: &'static str = concat!(file!(), ":", line!());
661671
const _: usize = $capacity;
662-
$crate::InstrumentLog::instrument_log($expr, CHANNEL_ID, Some($label), Some($capacity))
672+
$crate::InstrumentLog::instrument_log(
673+
$expr,
674+
CHANNEL_ID,
675+
Some($label.to_string()),
676+
Some($capacity),
677+
)
663678
}};
664679

665-
($expr:expr, capacity = $capacity:expr, log = true, label = $label:literal) => {{
680+
($expr:expr, capacity = $capacity:expr, log = true, label = $label:expr) => {{
666681
const CHANNEL_ID: &'static str = concat!(file!(), ":", line!());
667682
const _: usize = $capacity;
668-
$crate::InstrumentLog::instrument_log($expr, CHANNEL_ID, Some($label), Some($capacity))
683+
$crate::InstrumentLog::instrument_log(
684+
$expr,
685+
CHANNEL_ID,
686+
Some($label.to_string()),
687+
Some($capacity),
688+
)
669689
}};
670690

671-
($expr:expr, log = true, label = $label:literal, capacity = $capacity:expr) => {{
691+
($expr:expr, log = true, label = $label:expr, capacity = $capacity:expr) => {{
672692
const CHANNEL_ID: &'static str = concat!(file!(), ":", line!());
673693
const _: usize = $capacity;
674-
$crate::InstrumentLog::instrument_log($expr, CHANNEL_ID, Some($label), Some($capacity))
694+
$crate::InstrumentLog::instrument_log(
695+
$expr,
696+
CHANNEL_ID,
697+
Some($label.to_string()),
698+
Some($capacity),
699+
)
675700
}};
676701

677-
($expr:expr, log = true, capacity = $capacity:expr, label = $label:literal) => {{
702+
($expr:expr, log = true, capacity = $capacity:expr, label = $label:expr) => {{
678703
const CHANNEL_ID: &'static str = concat!(file!(), ":", line!());
679704
const _: usize = $capacity;
680-
$crate::InstrumentLog::instrument_log($expr, CHANNEL_ID, Some($label), Some($capacity))
705+
$crate::InstrumentLog::instrument_log(
706+
$expr,
707+
CHANNEL_ID,
708+
Some($label.to_string()),
709+
Some($capacity),
710+
)
681711
}};
682712
}
683713

@@ -697,8 +727,9 @@ fn compare_channel_stats(a: &ChannelStats, b: &ChannelStats) -> std::cmp::Orderi
697727
(false, true) => std::cmp::Ordering::Greater,
698728
(true, true) => a
699729
.label
730+
.as_ref()
700731
.unwrap()
701-
.cmp(b.label.unwrap())
732+
.cmp(b.label.as_ref().unwrap())
702733
.then_with(|| a.iter.cmp(&b.iter)),
703734
(false, false) => a.source.cmp(b.source).then_with(|| a.iter.cmp(&b.iter)),
704735
}

0 commit comments

Comments
 (0)