Skip to content
Merged
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
119 changes: 20 additions & 99 deletions rust/otap-dataflow/crates/telemetry/src/internal_events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,11 @@ pub mod _private {
// See issue: https://github.com/tokio-rs/tracing/issues/2774
#[macro_export]
macro_rules! otel_info {
($name:expr $(, $($fields:tt)*)?) => {
$crate::_private::info!(name: $name, target: env!("CARGO_PKG_NAME"), { $($($fields)*)? }, "");
($name:expr, $($fields:tt)+) => {
$crate::_private::info!(name: $name, target: env!("CARGO_PKG_NAME"), $($fields)+);
};
($name:expr) => {
$crate::_private::info!(name: $name, target: env!("CARGO_PKG_NAME"), "");
};
}

Expand All @@ -54,8 +57,11 @@ macro_rules! otel_info {
/// ```
#[macro_export]
macro_rules! otel_warn {
($name:expr $(, $($fields:tt)*)?) => {
$crate::_private::warn!(name: $name, target: env!("CARGO_PKG_NAME"), { $($($fields)*)? }, "");
($name:expr, $($fields:tt)+) => {
$crate::_private::warn!(name: $name, target: env!("CARGO_PKG_NAME"), $($fields)+);
};
($name:expr) => {
$crate::_private::warn!(name: $name, target: env!("CARGO_PKG_NAME"), "");
};
}

Expand All @@ -69,79 +75,15 @@ macro_rules! otel_warn {
/// # Example:
/// ```ignore
/// use otap_df_telemetry::otel_debug;
/// otel_debug!("processing.batch", batch_size = 100, "Processing batch of items");
/// otel_debug!("processing.done", count = 5); // no message, just fields
/// otel_debug!("processing.start"); // event name only
/// otel_debug!("processing.batch", batch_size = 100);
/// ```
#[macro_export]
macro_rules! otel_debug {
// Name only
($name:expr) => {
$crate::_private::debug!(name: $name, target: env!("CARGO_PKG_NAME"), { }, "");
};

// Name + bare literal message (no fields)
($name:expr, $message:literal) => {
$crate::_private::debug!(name: $name, target: env!("CARGO_PKG_NAME"), { }, $message);
};

// With fields (and optional trailing message) - delegate to helper
($name:expr, $($rest:tt)+) => {
$crate::__otel_debug_impl!(@munch [$name] [] $($rest)+)
};
}

/// Internal helper macro for otel_debug! to parse fields with optional trailing message.
#[doc(hidden)]
#[macro_export]
macro_rules! __otel_debug_impl {
// Terminal: trailing literal message only
(@munch [$name:expr] [$($fields:tt)*] $message:literal) => {
$crate::_private::debug!(name: $name, target: env!("CARGO_PKG_NAME"), { $($fields)* }, $message);
};

// Terminal: no more tokens (no trailing message)
(@munch [$name:expr] [$($fields:tt)*]) => {
$crate::_private::debug!(name: $name, target: env!("CARGO_PKG_NAME"), { $($fields)* }, "");
};

// Munch: key = ?value, ... (debug format with comma, more tokens follow)
(@munch [$name:expr] [$($acc:tt)*] $key:ident = ?$val:expr, $($rest:tt)+) => {
$crate::__otel_debug_impl!(@munch [$name] [$($acc)* $key = ?$val,] $($rest)+)
};
// Munch: key = ?value, (debug format with trailing comma, nothing follows)
(@munch [$name:expr] [$($acc:tt)*] $key:ident = ?$val:expr,) => {
$crate::__otel_debug_impl!(@munch [$name] [$($acc)* $key = ?$val,])
};
// Munch: key = ?value (debug format, last field, no comma)
(@munch [$name:expr] [$($acc:tt)*] $key:ident = ?$val:expr) => {
$crate::__otel_debug_impl!(@munch [$name] [$($acc)* $key = ?$val,])
};

// Munch: key = %value, ... (display format with comma, more tokens follow)
(@munch [$name:expr] [$($acc:tt)*] $key:ident = %$val:expr, $($rest:tt)+) => {
$crate::__otel_debug_impl!(@munch [$name] [$($acc)* $key = %$val,] $($rest)+)
};
// Munch: key = %value, (display format with trailing comma, nothing follows)
(@munch [$name:expr] [$($acc:tt)*] $key:ident = %$val:expr,) => {
$crate::__otel_debug_impl!(@munch [$name] [$($acc)* $key = %$val,])
};
// Munch: key = %value (display format, last field, no comma)
(@munch [$name:expr] [$($acc:tt)*] $key:ident = %$val:expr) => {
$crate::__otel_debug_impl!(@munch [$name] [$($acc)* $key = %$val,])
};

// Munch: key = value, ... (regular with comma, more tokens follow)
(@munch [$name:expr] [$($acc:tt)*] $key:ident = $val:expr, $($rest:tt)+) => {
$crate::__otel_debug_impl!(@munch [$name] [$($acc)* $key = $val,] $($rest)+)
};
// Munch: key = value, (regular with trailing comma, nothing follows)
(@munch [$name:expr] [$($acc:tt)*] $key:ident = $val:expr,) => {
$crate::__otel_debug_impl!(@munch [$name] [$($acc)* $key = $val,])
($name:expr, $($fields:tt)+) => {
$crate::_private::debug!(name: $name, target: env!("CARGO_PKG_NAME"), $($fields)+);
};
// Munch: key = value (regular, last field, no comma)
(@munch [$name:expr] [$($acc:tt)*] $key:ident = $val:expr) => {
$crate::__otel_debug_impl!(@munch [$name] [$($acc)* $key = $val,])
($name:expr) => {
$crate::_private::debug!(name: $name, target: env!("CARGO_PKG_NAME"), "");
};
}

Expand All @@ -159,8 +101,11 @@ macro_rules! __otel_debug_impl {
/// ```
#[macro_export]
macro_rules! otel_error {
($name:expr $(, $($fields:tt)*)?) => {
$crate::_private::error!(name: $name, target: env!("CARGO_PKG_NAME"), { $($($fields)*)? }, "");
($name:expr, $($fields:tt)+) => {
$crate::_private::error!(name: $name, target: env!("CARGO_PKG_NAME"), $($fields)+);
};
($name:expr) => {
$crate::_private::error!(name: $name, target: env!("CARGO_PKG_NAME"), "");
};
}

Expand Down Expand Up @@ -222,28 +167,4 @@ mod tests {
raw_error!("raw error message", error = ?err);
raw_error!("simple error message");
}

#[test]
fn test_otel_debug() {
otel_debug!("debug.event");
}

#[test]
fn test_otel_debug_with_attributes() {
otel_debug!("debug.event.with_attributes", value = 42);
}

#[test]
fn test_otel_debug_with_message() {
otel_debug!("debug.event.with_message", "This is a debug message");
}

#[test]
fn test_otel_debug_with_message_and_attributes() {
otel_debug!(
"debug.event.with_message_and_attributes",
value = 42,
"This is a debug message"
);
}
}
Loading