From 587a4423199c697f3f49f3795ea874ae999652e6 Mon Sep 17 00:00:00 2001 From: KodrAus Date: Thu, 24 Aug 2023 07:21:32 +1000 Subject: [PATCH] get things to build again --- core/src/extent.rs | 68 +++++++++++++++++++++++++++---- core/src/props.rs | 14 +++++-- core/src/time.rs | 2 +- core/src/well_known.rs | 6 +-- src/lib.rs | 4 +- src/macro_hooks.rs | 17 +++++--- src/platform/thread_local_ctxt.rs | 11 +++-- targets/otlp/src/logs.rs | 2 +- targets/term/src/lib.rs | 8 +--- 9 files changed, 99 insertions(+), 33 deletions(-) diff --git a/core/src/extent.rs b/core/src/extent.rs index 4761400..ab49bed 100644 --- a/core/src/extent.rs +++ b/core/src/extent.rs @@ -6,30 +6,44 @@ use crate::{ value::{ToValue, Value}, well_known::{TIMESTAMP_KEY, TIMESTAMP_START_KEY}, }; -use core::ops::Range; +use core::{fmt, ops::Range}; #[derive(Debug, Clone)] pub struct Extent(Option>); impl Extent { - pub fn point(ts: Timestamp) -> Self { - Extent(Some(ts..ts)) + pub fn new(ts: Range) -> Self { + Extent(Some(ts)) } - pub fn span(ts: Range) -> Self { - Extent(Some(ts)) + pub fn point(ts: Timestamp) -> Self { + Extent(Some(ts..ts)) } pub fn empty() -> Self { Extent(None) } + pub fn range(&self) -> Option<&Range> { + self.0.as_ref() + } + pub fn to_point(&self) -> Option<&Timestamp> { self.0.as_ref().map(|ts| &ts.end) } - pub fn to_span(&self) -> Option<&Range> { - self.0.as_ref().filter(|ts| ts.start != ts.end) + pub fn is_point(&self) -> bool { + self.0 + .as_ref() + .map(|ts| ts.start == ts.end) + .unwrap_or(false) + } + + pub fn is_span(&self) -> bool { + self.0 + .as_ref() + .map(|ts| ts.start != ts.end) + .unwrap_or(false) } pub fn is_empty(&self) -> bool { @@ -37,6 +51,38 @@ impl Extent { } } +impl From for Extent { + fn from(value: Timestamp) -> Self { + Extent::point(value) + } +} + +impl From> for Extent { + fn from(value: Range) -> Self { + Extent::new(value) + } +} + +impl From>> for Extent { + fn from(value: Option>) -> Self { + Extent(value) + } +} + +impl fmt::Display for Extent { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + match self.0 { + Some(ref ts) if ts.start != ts.end => { + fmt::Display::fmt(&ts.start, f)?; + f.write_str("..")?; + fmt::Display::fmt(&ts.end, f) + } + Some(ref ts) => fmt::Display::fmt(&ts.end, f), + None => Ok(()), + } + } +} + impl Props for Extent { fn for_each<'kv, F: FnMut(Key<'kv>, Value<'kv>) -> ControlFlow>( &'kv self, @@ -78,6 +124,12 @@ impl ToExtent for Option { } } +impl ToExtent for Extent { + fn to_extent(&self) -> Extent { + self.clone() + } +} + impl ToExtent for Timestamp { fn to_extent(&self) -> Extent { Extent::point(*self) @@ -86,7 +138,7 @@ impl ToExtent for Timestamp { impl ToExtent for Range { fn to_extent(&self) -> Extent { - Extent::span(self.clone()) + Extent::new(self.clone()) } } diff --git a/core/src/props.rs b/core/src/props.rs index 82f2c82..a98f911 100644 --- a/core/src/props.rs +++ b/core/src/props.rs @@ -90,8 +90,11 @@ impl<'a, P: Props + ?Sized + 'a> Props for alloc::boxed::Box

{ } impl Props for (K, V) { - fn for_each<'kv, F: FnMut(Key<'kv>, Value<'kv>) -> ControlFlow>(&'kv self, mut for_each: F) { - for_each(self.0.to_key(), self.1.to_value()); + fn for_each<'kv, F: FnMut(Key<'kv>, Value<'kv>) -> ControlFlow>( + &'kv self, + mut for_each: F, + ) -> ControlFlow { + for_each(self.0.to_key(), self.1.to_value()) } } @@ -135,7 +138,12 @@ where } impl Props for Empty { - fn for_each<'kv, F: FnMut(Key<'kv>, Value<'kv>) -> ControlFlow>(&'kv self, _: F) {} + fn for_each<'kv, F: FnMut(Key<'kv>, Value<'kv>) -> ControlFlow>( + &'kv self, + _: F, + ) -> ControlFlow { + Continue(()) + } } pub struct Chain { diff --git a/core/src/time.rs b/core/src/time.rs index 452f58d..93e2ead 100644 --- a/core/src/time.rs +++ b/core/src/time.rs @@ -165,7 +165,7 @@ impl Timer { let end = self.clock.now(); match (self.start, end) { - (Some(start), Some(end)) => Extent::span(start..end), + (Some(start), Some(end)) => Extent::new(start..end), _ => Extent::empty(), } } diff --git a/core/src/well_known.rs b/core/src/well_known.rs index 5a9d888..8e8c48d 100644 --- a/core/src/well_known.rs +++ b/core/src/well_known.rs @@ -14,9 +14,9 @@ pub const TPL_KEY: &'static str = "#tpl"; pub const ERR_KEY: &'static str = "err"; pub const LVL_KEY: &'static str = "lvl"; pub const MODULE_KEY: &'static str = "mod"; -pub const TRACE_ID_KEY: &'static str = "tr"; -pub const SPAN_ID_KEY: &'static str = "sp"; -pub const SPAN_PARENT_KEY: &'static str = "spp"; +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(); diff --git a/src/lib.rs b/src/lib.rs index b5e3167..34c9dd7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,6 +10,7 @@ use crate::local_frame::{LocalFrame, LocalFrameFuture}; #[doc(inline)] pub use emit_macros::*; +use emit_core::extent::{Extent, ToExtent}; #[doc(inline)] pub use emit_core::{ ctxt, empty, event, filter, id, key, level, props, target, template, time, value, well_known, @@ -43,7 +44,7 @@ fn base_emit( to: impl Target, when: impl Filter, ctxt: impl Ctxt, - ts: Option>, + ts: impl ToExtent, tpl: Template, props: impl Props, ) { @@ -82,6 +83,7 @@ pub fn emit(evt: &Event) { ambient, ambient, evt.extent() + .range() .cloned() .or_else(|| ambient.now().map(|ts| ts..ts)), tpl, diff --git a/src/macro_hooks.rs b/src/macro_hooks.rs index d8b842c..16c466d 100644 --- a/src/macro_hooks.rs +++ b/src/macro_hooks.rs @@ -15,6 +15,7 @@ use emit_core::{ value::{ToValue, Value}, }; +use emit_core::extent::ToExtent; #[cfg(feature = "std")] use std::error::Error; @@ -410,7 +411,7 @@ impl<'a> __PrivateKeyHook for Key<'a> { pub fn __private_emit( to: impl Target, when: impl Filter, - extent: impl Extent, + extent: impl ToExtent, tpl: Template, props: impl Props, ) { @@ -420,7 +421,11 @@ pub fn __private_emit( to.and(ambient), when.and(ambient), ambient, - extent.extent().or_else(|| ambient.now().map(|ts| ts..ts)), + extent + .to_extent() + .range() + .cloned() + .or_else(|| ambient.now().map(|ts| ts..ts)), tpl, props, ); @@ -464,16 +469,16 @@ impl<'a> Props for __PrivateMacroProps<'a> { fn for_each<'kv, F: FnMut(Key<'kv>, Value<'kv>) -> ControlFlow<()>>( &'kv self, mut for_each: F, - ) { + ) -> ControlFlow<()> { for kv in &self.0 { let k = &kv.0; if let Some(ref v) = kv.1 { - if let ControlFlow::Break(()) = for_each(k.by_ref(), v.by_ref()) { - return; - } + for_each(k.by_ref(), v.by_ref())?; } } + + ControlFlow::Continue(()) } fn get<'v, K: ToKey>(&'v self, key: K) -> Option> { diff --git a/src/platform/thread_local_ctxt.rs b/src/platform/thread_local_ctxt.rs index 51dbb67..d3bb82e 100644 --- a/src/platform/thread_local_ctxt.rs +++ b/src/platform/thread_local_ctxt.rs @@ -27,12 +27,15 @@ pub struct ThreadLocalSpan { } impl Props for ThreadLocalSpan { - fn for_each<'a, F: FnMut(Key<'a>, Value<'a>) -> ControlFlow<()>>(&'a self, mut for_each: F) { + fn for_each<'a, F: FnMut(Key<'a>, Value<'a>) -> ControlFlow<()>>( + &'a self, + mut for_each: F, + ) -> ControlFlow<()> { for (k, v) in &self.props { - if let Break(()) = for_each(k.by_ref(), v.by_ref()) { - break; - } + for_each(k.by_ref(), v.by_ref())?; } + + Continue(()) } } diff --git a/targets/otlp/src/logs.rs b/targets/otlp/src/logs.rs index c94fa7e..1f4e592 100644 --- a/targets/otlp/src/logs.rs +++ b/targets/otlp/src/logs.rs @@ -64,7 +64,7 @@ impl emit_core::target::Target for OtlpLogsTarget { fn event(&self, evt: &emit_core::event::Event

) { let time_unix_nano = evt .extent() - .map(|ts| ts.end) + .to_point() .map(|ts| ts.to_unix().as_nanos() as u64) .unwrap_or_default(); diff --git a/targets/term/src/lib.rs b/targets/term/src/lib.rs index ef3d88e..c40d294 100644 --- a/targets/term/src/lib.rs +++ b/targets/term/src/lib.rs @@ -53,14 +53,10 @@ impl emit::target::Target for Stdout { fn print(out: &BufferWriter, buf: &mut Buffer, evt: &emit::Event) { let mut header_empty = true; - if let Some(ts) = evt.extent() { + if !evt.extent().is_empty() { let _ = write!(buf, "["); - if ts.start != ts.end { - let _ = write!(buf, "{:.0}..", ts.start); - } - - let _ = write!(buf, "{:.0}", ts.end); + let _ = write!(buf, "{:.0}", evt.extent()); header_empty = false; }