use tracing::Level;
use super::tracing_timing::{ClientOperation, ClientTimingFlags};
#[derive(Clone, Debug)]
pub struct TracingConfig {
pub(crate) connect_level: Level,
pub(crate) send_level: Level,
pub(crate) receive_level: Level,
pub(crate) call_level: Level,
pub(crate) streaming_level: Level,
pub(crate) close_level: Level,
timing: ClientTimingFlags,
}
impl Default for TracingConfig {
fn default() -> Self {
Self {
connect_level: Level::INFO,
send_level: Level::DEBUG,
receive_level: Level::DEBUG,
call_level: Level::DEBUG,
streaming_level: Level::DEBUG,
close_level: Level::INFO,
timing: ClientTimingFlags::default(),
}
}
}
impl TracingConfig {
#[must_use]
pub const fn with_connect_level(mut self, level: Level) -> Self {
self.connect_level = level;
self
}
#[must_use]
pub const fn with_connect_timing(mut self, enabled: bool) -> Self {
self.timing = self.timing.with_enabled(ClientOperation::Connect, enabled);
self
}
#[must_use]
pub const fn with_send_level(mut self, level: Level) -> Self {
self.send_level = level;
self
}
#[must_use]
pub const fn with_send_timing(mut self, enabled: bool) -> Self {
self.timing = self.timing.with_enabled(ClientOperation::Send, enabled);
self
}
#[must_use]
pub const fn with_receive_level(mut self, level: Level) -> Self {
self.receive_level = level;
self
}
#[must_use]
pub const fn with_receive_timing(mut self, enabled: bool) -> Self {
self.timing = self.timing.with_enabled(ClientOperation::Receive, enabled);
self
}
#[must_use]
pub const fn with_call_level(mut self, level: Level) -> Self {
self.call_level = level;
self
}
#[must_use]
pub const fn with_call_timing(mut self, enabled: bool) -> Self {
self.timing = self.timing.with_enabled(ClientOperation::Call, enabled);
self
}
#[must_use]
pub const fn with_streaming_level(mut self, level: Level) -> Self {
self.streaming_level = level;
self
}
#[must_use]
pub const fn with_streaming_timing(mut self, enabled: bool) -> Self {
self.timing = self
.timing
.with_enabled(ClientOperation::Streaming, enabled);
self
}
#[must_use]
pub const fn with_close_level(mut self, level: Level) -> Self {
self.close_level = level;
self
}
#[must_use]
pub const fn with_close_timing(mut self, enabled: bool) -> Self {
self.timing = self.timing.with_enabled(ClientOperation::Close, enabled);
self
}
#[must_use]
pub const fn with_all_levels(mut self, level: Level) -> Self {
self.connect_level = level;
self.send_level = level;
self.receive_level = level;
self.call_level = level;
self.streaming_level = level;
self.close_level = level;
self
}
#[must_use]
pub const fn with_all_timing(mut self, enabled: bool) -> Self {
self.timing = ClientTimingFlags::all(enabled);
self
}
#[must_use]
pub(crate) const fn timing_enabled(&self, operation: ClientOperation) -> bool {
self.timing.is_enabled(operation)
}
}
#[cfg(test)]
mod tests {
use rstest::rstest;
use tracing::Level;
use super::{ClientOperation, TracingConfig};
fn assert_levels(
cfg: &TracingConfig,
[connect, send, receive, call, streaming, close]: [Level; 6],
) {
assert_eq!(cfg.connect_level, connect, "connect_level");
assert_eq!(cfg.send_level, send, "send_level");
assert_eq!(cfg.receive_level, receive, "receive_level");
assert_eq!(cfg.call_level, call, "call_level");
assert_eq!(cfg.streaming_level, streaming, "streaming_level");
assert_eq!(cfg.close_level, close, "close_level");
}
#[track_caller]
fn assert_timing_flags(cfg: &TracingConfig, expected: [bool; 6]) {
let actual = [
cfg.timing_enabled(ClientOperation::Connect),
cfg.timing_enabled(ClientOperation::Send),
cfg.timing_enabled(ClientOperation::Receive),
cfg.timing_enabled(ClientOperation::Call),
cfg.timing_enabled(ClientOperation::Streaming),
cfg.timing_enabled(ClientOperation::Close),
];
assert_eq!(
actual, expected,
"timing flags are ordered connect, send, receive, call, streaming, close"
);
}
#[test]
fn default_levels_are_info_for_lifecycle_debug_for_data_ops() {
let cfg = TracingConfig::default();
assert_levels(
&cfg,
[
Level::INFO,
Level::DEBUG,
Level::DEBUG,
Level::DEBUG,
Level::DEBUG,
Level::INFO,
],
);
}
#[test]
fn default_timing_flags_are_all_false() {
let cfg = TracingConfig::default();
assert_timing_flags(&cfg, [false; 6]);
}
#[test]
fn with_all_levels_updates_every_level_field() {
let cfg = TracingConfig::default().with_all_levels(Level::TRACE);
assert_levels(&cfg, [Level::TRACE; 6]);
}
#[test]
fn with_all_timing_true_enables_every_flag() {
let cfg = TracingConfig::default().with_all_timing(true);
assert_timing_flags(&cfg, [true; 6]);
}
#[test]
fn with_all_timing_false_disables_every_flag() {
let cfg = TracingConfig::default()
.with_all_timing(true)
.with_all_timing(false);
assert_timing_flags(&cfg, [false; 6]);
}
#[test]
fn disabling_one_timing_flag_preserves_other_enabled_operations() {
let cfg = TracingConfig::default()
.with_all_timing(true)
.with_send_timing(false);
assert_timing_flags(&cfg, [true, false, true, true, true, true]);
}
#[rstest]
#[case::connect(
TracingConfig::default().with_connect_timing(true),
[true, false, false, false, false, false],
)]
#[case::send(
TracingConfig::default().with_send_timing(true),
[false, true, false, false, false, false],
)]
#[case::receive(
TracingConfig::default().with_receive_timing(true),
[false, false, true, false, false, false],
)]
#[case::call(
TracingConfig::default().with_call_timing(true),
[false, false, false, true, false, false],
)]
#[case::streaming(
TracingConfig::default().with_streaming_timing(true),
[false, false, false, false, true, false],
)]
#[case::close(
TracingConfig::default().with_close_timing(true),
[false, false, false, false, false, true],
)]
fn individual_timing_setters_only_affect_their_flag(
#[case] cfg: TracingConfig,
#[case] expected: [bool; 6],
) {
assert_timing_flags(&cfg, expected);
}
#[rstest]
#[case::connect(
TracingConfig::default().with_connect_level(Level::TRACE),
[Level::TRACE, Level::DEBUG, Level::DEBUG, Level::DEBUG, Level::DEBUG, Level::INFO],
)]
#[case::send(
TracingConfig::default().with_send_level(Level::TRACE),
[Level::INFO, Level::TRACE, Level::DEBUG, Level::DEBUG, Level::DEBUG, Level::INFO],
)]
#[case::receive(
TracingConfig::default().with_receive_level(Level::TRACE),
[Level::INFO, Level::DEBUG, Level::TRACE, Level::DEBUG, Level::DEBUG, Level::INFO],
)]
#[case::call(
TracingConfig::default().with_call_level(Level::TRACE),
[Level::INFO, Level::DEBUG, Level::DEBUG, Level::TRACE, Level::DEBUG, Level::INFO],
)]
#[case::streaming(
TracingConfig::default().with_streaming_level(Level::TRACE),
[Level::INFO, Level::DEBUG, Level::DEBUG, Level::DEBUG, Level::TRACE, Level::INFO],
)]
#[case::close(
TracingConfig::default().with_close_level(Level::TRACE),
[Level::INFO, Level::DEBUG, Level::DEBUG, Level::DEBUG, Level::DEBUG, Level::TRACE],
)]
fn individual_level_setters_only_affect_their_field(
#[case] cfg: TracingConfig,
#[case] expected: [Level; 6],
) {
assert_levels(&cfg, expected);
}
}