diff --git a/core/src/event.rs b/core/src/event.rs index d82e299..50b0b7d 100644 --- a/core/src/event.rs +++ b/core/src/event.rs @@ -2,11 +2,8 @@ use core::{fmt, ops::ControlFlow}; use crate::{ extent::{Extent, ToExtent}, - key::{Key, ToKey}, - props::{ByRef, Chain, ErasedProps, Props}, + props::{ByRef, ErasedProps, Props}, template::{Render, Template}, - value::{ToValue, Value}, - well_known::{MSG_KEY, TPL_KEY}, }; #[derive(Clone)] @@ -16,20 +13,6 @@ pub struct Event<'a, P> { props: P, } -impl<'a, P: Props> fmt::Debug for Event<'a, P> { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let mut f = f.debug_struct("Event"); - - self.for_each(|k, v| { - f.field(k.as_str(), &v); - - ControlFlow::Continue(()) - }); - - f.finish() - } -} - impl<'a, P> Event<'a, P> { pub fn new(extent: impl ToExtent, tpl: Template<'a>, props: P) -> Self { Event { @@ -42,35 +25,20 @@ impl<'a, P> Event<'a, P> { pub fn extent(&self) -> &Extent { &self.extent } -} - -impl<'a, P: Props> Event<'a, P> { - pub fn msg(&self) -> Render<&P> { - self.tpl.render(&self.props) - } pub fn tpl(&self) -> Template { self.tpl.by_ref() } - pub fn chain(self, other: U) -> Event<'a, Chain> { - Event { - extent: self.extent, - tpl: self.tpl, - props: self.props.chain(other), - } - } - - pub fn for_each<'kv, F: FnMut(Key<'kv>, Value<'kv>) -> ControlFlow<()>>( - &'kv self, - for_each: F, - ) { - let _ = Props::for_each(self, for_each); - } - pub fn props(&self) -> &P { &self.props } +} + +impl<'a, P: Props> Event<'a, P> { + pub fn msg(&self) -> Render<&P> { + self.tpl.render(&self.props) + } pub fn by_ref<'b>(&'b self) -> Event<'b, ByRef<'b, P>> { Event { @@ -89,47 +57,31 @@ impl<'a, P: Props> Event<'a, P> { } } -impl<'a, P> ToExtent for Event<'a, P> { - fn to_extent(&self) -> Extent { - self.extent.clone() - } -} +impl<'a, P: Props> fmt::Debug for Event<'a, P> { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + struct AsDebug(T); -impl<'a, P: Props> Props for Event<'a, P> { - fn for_each<'kv, F: FnMut(Key<'kv>, Value<'kv>) -> ControlFlow<()>>( - &'kv self, - mut for_each: F, - ) -> ControlFlow<()> { - self.extent.for_each(&mut for_each)?; + impl fmt::Debug for AsDebug { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + let mut f = f.debug_struct(""); - for_each(TPL_KEY.to_key(), self.tpl.to_value())?; - for_each(MSG_KEY.to_key(), Msg::new_ref(self).to_value())?; + self.0.for_each(|k, v| { + f.field(k.as_str(), &v); - self.props.for_each(for_each) - } -} + ControlFlow::Continue(()) + }); -#[repr(transparent)] -struct Msg<'a, P>(Event<'a, P>); + f.finish() + } + } -impl<'a, P> Msg<'a, P> { - fn new_ref<'b>(evt: &'b Event<'a, P>) -> &'b Msg<'a, P> { - unsafe { &*(evt as *const Event<'a, P> as *const Msg<'a, P>) } - } -} + let mut f = f.debug_struct("Event"); -impl<'a, P: Props> ToValue for Msg<'a, P> { - fn to_value(&self) -> Value { - if let Some(msg) = self.0.tpl.as_str() { - Value::from(msg) - } else { - Value::from_display(self) - } - } -} + f.field("extent", &self.extent); + f.field("msg", &self.msg()); + f.field("tpl", &self.tpl); + f.field("props", &AsDebug(&self.props)); -impl<'a, P: Props> fmt::Display for Msg<'a, P> { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - fmt::Display::fmt(&self.0.msg(), f) + f.finish() } } diff --git a/core/src/extent.rs b/core/src/extent.rs index a2bea68..d83ff6f 100644 --- a/core/src/extent.rs +++ b/core/src/extent.rs @@ -1,16 +1,5 @@ -use crate::{ - empty::Empty, - key::{Key, ToKey}, - props::Props, - timestamp::Timestamp, - value::{ToValue, Value}, - well_known::{TIMESTAMP_KEY, TIMESTAMP_START_KEY}, -}; -use core::{ - fmt, - ops::{ControlFlow, Range}, - time::Duration, -}; +use crate::{empty::Empty, timestamp::Timestamp}; +use core::{fmt, ops::Range, time::Duration}; #[derive(Debug, Clone)] pub struct Extent(Option>); @@ -111,23 +100,6 @@ impl fmt::Display for Extent { } } -impl Props for Extent { - fn for_each<'kv, F: FnMut(Key<'kv>, Value<'kv>) -> ControlFlow<()>>( - &'kv self, - mut for_each: F, - ) -> ControlFlow<()> { - if let Some(ref ts) = self.0 { - if ts.start != ts.end { - for_each(TIMESTAMP_START_KEY.to_key(), ts.start.to_value())?; - } - - for_each(TIMESTAMP_KEY.to_key(), ts.end.to_value()) - } else { - ControlFlow::Continue(()) - } - } -} - pub trait ToExtent { fn to_extent(&self) -> Extent; } diff --git a/core/src/well_known.rs b/core/src/well_known.rs index 5d8c22b..47a18ad 100644 --- a/core/src/well_known.rs +++ b/core/src/well_known.rs @@ -2,15 +2,9 @@ use crate::{ id::{SpanId, TraceId}, level::Level, props::Props, - timestamp::Timestamp, value::Value, }; -pub const TIMESTAMP_KEY: &'static str = "#ts"; -pub const TIMESTAMP_START_KEY: &'static str = "#tss"; -pub const MSG_KEY: &'static str = "#msg"; -pub const TPL_KEY: &'static str = "#tpl"; - pub const ERR_KEY: &'static str = "err"; pub const LVL_KEY: &'static str = "lvl"; pub const LOCATION_KEY: &'static str = "loc"; @@ -18,27 +12,7 @@ pub const TRACE_ID_KEY: &'static str = "trace_id"; pub const SPAN_ID_KEY: &'static str = "span_id"; pub const SPAN_PARENT_KEY: &'static str = "span_parent"; -pub const fn is_reserved(key: &str) -> bool { - let key = key.as_bytes(); - - if key.len() > 1 { - key[0] == b'#' && key[1] != b'#' - } else if key.len() == 1 { - key[0] == b'#' - } else { - false - } -} - pub trait WellKnown: Props { - fn timestamp(&self) -> Option { - self.get(TIMESTAMP_KEY)?.to_timestamp() - } - - fn timestamp_start(&self) -> Option { - self.get(TIMESTAMP_START_KEY)?.to_timestamp() - } - fn lvl(&self) -> Option { self.get(LVL_KEY)?.to_level() } @@ -59,14 +33,6 @@ pub trait WellKnown: Props { self.get(SPAN_PARENT_KEY)?.to_span_id() } - fn msg(&self) -> Option { - self.get(MSG_KEY) - } - - fn tpl(&self) -> Option { - self.get(TPL_KEY) - } - fn err(&self) -> Option { self.get(ERR_KEY) } diff --git a/macros/src/emit.rs b/macros/src/emit.rs index 6c4ecd6..cd03350 100644 --- a/macros/src/emit.rs +++ b/macros/src/emit.rs @@ -66,11 +66,6 @@ pub fn expand_tokens(opts: ExpandTokens) -> Result { quote!(#loc_ident: emit::__private::caller()), )?)?; - // Ensure props don't use reserved identifiers - for prop in props.iter() { - props::ensure_not_reserved(&prop.label, prop.span())?; - } - let props_match_input_tokens = props.match_input_tokens(); let props_match_binding_tokens = props.match_binding_tokens(); let props_tokens = props.match_bound_tokens(); diff --git a/macros/src/key.rs b/macros/src/key.rs index 3249d4d..6016124 100644 --- a/macros/src/key.rs +++ b/macros/src/key.rs @@ -61,11 +61,6 @@ impl Parse for Args { .ok_or_else(|| syn::Error::new(input.span(), "the `name` argument is missing"))? }; - // Ensure key literals are not reserved - if let Name::Str(ref name) = name { - props::ensure_not_reserved(&name, span)?; - } - Ok(Args { name }) } } diff --git a/macros/src/props.rs b/macros/src/props.rs index 4b748d6..33e94b1 100644 --- a/macros/src/props.rs +++ b/macros/src/props.rs @@ -162,14 +162,3 @@ impl Props { Ok(()) } } - -pub fn ensure_not_reserved(k: &str, span: Span) -> Result<(), syn::Error> { - if emit_core::well_known::is_reserved(k) { - Err(syn::Error::new( - span, - format!("`{}` is a reserved identifier", k), - )) - } else { - Ok(()) - } -} diff --git a/macros/src/with.rs b/macros/src/with.rs index 88b5902..4462696 100644 --- a/macros/src/with.rs +++ b/macros/src/with.rs @@ -24,11 +24,6 @@ impl Parse for Args { pub fn expand_tokens(opts: ExpandTokens) -> Result { let props = syn::parse2::(opts.input)?; - // Ensure props don't use reserved identifiers - for prop in props.iter() { - props::ensure_not_reserved(&prop.label, prop.span())?; - } - let mut item = syn::parse2::(opts.item)?; match &mut item { // A synchronous function diff --git a/targets/otlp/src/logs.rs b/targets/otlp/src/logs.rs index a80eb3c..88174ac 100644 --- a/targets/otlp/src/logs.rs +++ b/targets/otlp/src/logs.rs @@ -70,7 +70,7 @@ impl emit_core::emitter::Emitter for OtlpLogsTarget { let observed_time_unix_nano = time_unix_nano; - let level = evt.lvl().unwrap_or(emit_core::level::Level::Info); + let level = evt.props().lvl().unwrap_or(emit_core::level::Level::Info); let severity_number = match level { emit_core::level::Level::Debug => SeverityNumber::Debug as i32,