From 8a2c124baa6a76c214079c6081fb8fe893fc1363 Mon Sep 17 00:00:00 2001 From: KodrAus Date: Thu, 8 Feb 2024 21:05:09 +1000 Subject: [PATCH] fix up nostd build --- Cargo.toml | 2 +- core/src/emitter.rs | 2 +- core/src/filter.rs | 2 +- core/src/runtime.rs | 83 ++++++++++++++++++++------------ core/src/well_known.rs | 30 +++++++----- metrics/Cargo.toml | 2 +- metrics/src/lib.rs | 18 +++---- src/macro_hooks.rs | 42 ++++++++++------ src/platform.rs | 5 +- src/setup.rs | 2 +- targets/otlp/src/data/metrics.rs | 10 ++-- tests/smoke-test/main.rs | 8 +-- 12 files changed, 122 insertions(+), 84 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 2fef158..2fec46e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ edition = "2021" [features] default = ["std", "rng"] -std = ["emit_macros/std", "emit_core/std"] +std = ["alloc", "emit_macros/std", "emit_core/std"] alloc = ["emit_core/alloc"] sval = ["emit_macros/sval", "emit_core/sval", "dep:sval"] serde = ["emit_macros/serde", "emit_core/serde", "dep:serde"] diff --git a/core/src/emitter.rs b/core/src/emitter.rs index 10ac113..4014e75 100644 --- a/core/src/emitter.rs +++ b/core/src/emitter.rs @@ -11,7 +11,7 @@ pub trait Emitter { fn blocking_flush(&self, timeout: Duration); - fn and(self, other: U) -> And + fn and_emitter(self, other: U) -> And where Self: Sized, { diff --git a/core/src/filter.rs b/core/src/filter.rs index c42d7b9..3f28af2 100644 --- a/core/src/filter.rs +++ b/core/src/filter.rs @@ -10,7 +10,7 @@ use crate::{ pub trait Filter { fn matches(&self, evt: &Event

) -> bool; - fn and(self, other: U) -> And + fn and_filter(self, other: U) -> And where Self: Sized, { diff --git a/core/src/runtime.rs b/core/src/runtime.rs index d740ddb..ac8db0c 100644 --- a/core/src/runtime.rs +++ b/core/src/runtime.rs @@ -389,12 +389,14 @@ mod std_support { use super::*; - trait AmbientTarget: Any + ErasedEmitter + Send + Sync + 'static { + pub type AmbientEmitter<'a> = &'a (dyn ErasedEmitter + Send + Sync + 'static); + + trait AnyEmitter: Any + ErasedEmitter + Send + Sync + 'static { fn as_any(&self) -> &dyn Any; fn as_super(&self) -> &(dyn ErasedEmitter + Send + Sync + 'static); } - impl AmbientTarget for T { + impl AnyEmitter for T { fn as_any(&self) -> &dyn Any { self } @@ -404,12 +406,14 @@ mod std_support { } } - trait AmbientFilter: Any + ErasedFilter + Send + Sync + 'static { + pub type AmbientFilter<'a> = &'a (dyn ErasedFilter + Send + Sync + 'static); + + trait AnyFilter: Any + ErasedFilter + Send + Sync + 'static { fn as_any(&self) -> &dyn Any; fn as_super(&self) -> &(dyn ErasedFilter + Send + Sync + 'static); } - impl AmbientFilter for T { + impl AnyFilter for T { fn as_any(&self) -> &dyn Any { self } @@ -419,12 +423,14 @@ mod std_support { } } - trait AmbientCtxt: Any + ErasedCtxt + Send + Sync + 'static { + pub type AmbientCtxt<'a> = &'a (dyn ErasedCtxt + Send + Sync + 'static); + + trait AnyCtxt: Any + ErasedCtxt + Send + Sync + 'static { fn as_any(&self) -> &dyn Any; fn as_super(&self) -> &(dyn ErasedCtxt + Send + Sync + 'static); } - impl AmbientCtxt for T { + impl AnyCtxt for T { fn as_any(&self) -> &dyn Any { self } @@ -434,12 +440,14 @@ mod std_support { } } - trait AmbientClock: Any + ErasedClock + Send + Sync + 'static { + pub type AmbientClock<'a> = &'a (dyn ErasedClock + Send + Sync + 'static); + + trait AnyClock: Any + ErasedClock + Send + Sync + 'static { fn as_any(&self) -> &dyn Any; fn as_super(&self) -> &(dyn ErasedClock + Send + Sync + 'static); } - impl AmbientClock for T { + impl AnyClock for T { fn as_any(&self) -> &dyn Any { self } @@ -449,12 +457,14 @@ mod std_support { } } - trait AmbientGenId: Any + ErasedRng + Send + Sync + 'static { + pub type AmbientRng<'a> = &'a (dyn ErasedRng + Send + Sync + 'static); + + trait AnyRng: Any + ErasedRng + Send + Sync + 'static { fn as_any(&self) -> &dyn Any; fn as_super(&self) -> &(dyn ErasedRng + Send + Sync + 'static); } - impl AmbientGenId for T { + impl AnyRng for T { fn as_any(&self) -> &dyn Any { self } @@ -474,11 +484,11 @@ mod std_support { } type AmbientSyncValue = Runtime< - Box, - Box, - Box, - Box, - Box, + Box, + Box, + Box, + Box, + Box, >; type AmbientSyncRuntime = Runtime< @@ -490,11 +500,11 @@ mod std_support { >; pub type AmbientRuntime<'a> = Runtime< - &'a (dyn ErasedEmitter + Send + Sync), - &'a (dyn ErasedFilter + Send + Sync), - &'a (dyn ErasedCtxt + Send + Sync), - &'a (dyn ErasedClock + Send + Sync), - &'a (dyn ErasedRng + Send + Sync), + AmbientEmitter<'a>, + AmbientFilter<'a>, + AmbientCtxt<'a>, + AmbientClock<'a>, + AmbientRng<'a>, >; unsafe impl Send for AmbientSync where AmbientSyncValue: Send {} @@ -525,14 +535,12 @@ mod std_support { .set({ let value = pipeline .map_emitter(|emitter| { - Box::new(emitter) as Box - }) - .map_filter(|filter| { - Box::new(filter) as Box + Box::new(emitter) as Box }) - .map_ctxt(|ctxt| Box::new(ctxt) as Box) - .map_clock(|clock| Box::new(clock) as Box) - .map_rng(|id_gen| Box::new(id_gen) as Box); + .map_filter(|filter| Box::new(filter) as Box) + .map_ctxt(|ctxt| Box::new(ctxt) as Box) + .map_clock(|clock| Box::new(clock) as Box) + .map_rng(|id_gen| Box::new(id_gen) as Box); let runtime = Runtime::build( value.emitter().as_super() as *const _, @@ -625,8 +633,9 @@ mod no_std_support { false } - pub fn get(&self) -> &Runtime { - const EMPTY_AMBIENT_RUNTIME: Runtime = Runtime::new(); + pub fn get(&self) -> &AmbientRuntime { + const EMPTY_AMBIENT_RUNTIME: AmbientRuntime = + Runtime::build(&Empty, &Empty, &Empty, &Empty, &Empty); &EMPTY_AMBIENT_RUNTIME } @@ -641,12 +650,24 @@ mod no_std_support { false } - pub fn get(&self) -> &Runtime { + pub fn get(&self) -> &AmbientRuntime { self.0.get() } } - pub type AmbientRuntime<'a> = Runtime; + pub type AmbientEmitter<'a> = &'a Empty; + pub type AmbientFilter<'a> = &'a Empty; + pub type AmbientCtxt<'a> = &'a Empty; + pub type AmbientClock<'a> = &'a Empty; + pub type AmbientRng<'a> = &'a Empty; + + pub type AmbientRuntime<'a> = Runtime< + AmbientEmitter<'a>, + AmbientFilter<'a>, + AmbientCtxt<'a>, + AmbientClock<'a>, + AmbientRng<'a>, + >; } #[cfg(not(feature = "std"))] diff --git a/core/src/well_known.rs b/core/src/well_known.rs index 8db6995..942c51d 100644 --- a/core/src/well_known.rs +++ b/core/src/well_known.rs @@ -1,24 +1,32 @@ +// Event pub const TS_KEY: &'static str = "ts"; pub const TS_START_KEY: &'static str = "ts_start"; - pub const TPL_KEY: &'static str = "tpl"; pub const MSG_KEY: &'static str = "msg"; -pub const ERR_KEY: &'static str = "err"; +// Level pub const LVL_KEY: &'static str = "lvl"; + +pub const LVL_DEBUG: &'static str = "debug"; +pub const LVL_INFO: &'static str = "info"; +pub const LVL_WARN: &'static str = "warn"; +pub const LVL_ERR: &'static str = "err"; + +pub const ERR_KEY: &'static str = "err"; + pub const MODULE_KEY: &'static str = "module"; + +// Trace 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"; + +// Metrics pub const METRIC_NAME_KEY: &'static str = "metric_name"; -pub const METRIC_KIND_KEY: &'static str = "metric_kind"; +pub const METRIC_AGG_KEY: &'static str = "metric_agg"; pub const METRIC_VALUE_KEY: &'static str = "metric_value"; +pub const METRIC_UNIT_KEY: &'static str = "metric_unit"; -pub const LVL_DEBUG: &'static str = "debug"; -pub const LVL_INFO: &'static str = "info"; -pub const LVL_WARN: &'static str = "warn"; -pub const LVL_ERR: &'static str = "err"; - -pub const METRIC_KIND_SUM: &'static str = "sum"; -pub const METRIC_KIND_MIN: &'static str = "min"; -pub const METRIC_KIND_MAX: &'static str = "max"; +pub const METRIC_AGG_SUM: &'static str = "sum"; +pub const METRIC_AGG_MIN: &'static str = "min"; +pub const METRIC_AGG_MAX: &'static str = "max"; diff --git a/metrics/Cargo.toml b/metrics/Cargo.toml index 04f986f..def281f 100644 --- a/metrics/Cargo.toml +++ b/metrics/Cargo.toml @@ -6,4 +6,4 @@ edition = "2021" [dependencies.emit] path = "../" default-features = false -features = ["sval"] +features = ["alloc", "sval"] diff --git a/metrics/src/lib.rs b/metrics/src/lib.rs index 759f11a..c5365c2 100644 --- a/metrics/src/lib.rs +++ b/metrics/src/lib.rs @@ -5,8 +5,8 @@ use std::{borrow::Cow, cmp, collections::HashMap, mem, ops::Range, sync::Mutex, use emit::{ props::Props, str::Str, - well_known::{METRIC_KIND_KEY, METRIC_KIND_SUM, METRIC_NAME_KEY, METRIC_VALUE_KEY}, - Event, Timestamp, + well_known::{METRIC_AGG_KEY, METRIC_AGG_SUM, METRIC_NAME_KEY, METRIC_VALUE_KEY}, + Event, Timestamp, Value, }; pub fn aggregate_by_count(count: usize, emitter: E) -> MetricsEmitter { @@ -46,7 +46,7 @@ impl emit::Emitter for MetricsEmitter { continue; } - let metric_kind = METRIC_KIND_SUM; + let metric_agg = METRIC_AGG_SUM; let histogram = histogram.compute(); let x = histogram.timestamp_range(); @@ -57,9 +57,9 @@ impl emit::Emitter for MetricsEmitter { self.inner.emit(&emit::Event::new( x, - emit::tpl!("{metric_kind} of {metric_name} is in the range {#[emit::fmt(\".3\")] min}..={#[emit::fmt(\".3\")] max}"), + emit::tpl!("{metric_agg} of {metric_name} is in the range {#[emit::fmt(\".3\")] min}..={#[emit::fmt(\".3\")] max}"), emit::props! { - metric_kind, + metric_agg, metric_name, #[emit::as_sval] metric_value, @@ -135,15 +135,15 @@ impl Aggregator { } pub fn record_metric(&mut self, evt: &Event) -> bool { - if let (Some(extent), Some(metric_name), Some(metric_kind), Some(metric_value)) = ( + if let (Some(extent), Some(metric_name), Some(metric_agg), Some(metric_value)) = ( evt.extent() .filter(|extent| extent.is_point()) .map(|extent| extent.as_point()), evt.props().pull::<_, Str>(METRIC_NAME_KEY), - evt.props().pull::<_, Str>(METRIC_KIND_KEY), - evt.props().get(METRIC_VALUE_KEY), + evt.props().pull::<_, Str>(METRIC_AGG_KEY), + evt.props().pull::<_, Value>(METRIC_VALUE_KEY), ) { - if metric_kind == METRIC_KIND_SUM { + if metric_agg == METRIC_AGG_SUM { return self.record_sum_point(metric_name.to_cow(), *extent, metric_value.as_f64()); } } diff --git a/src/macro_hooks.rs b/src/macro_hooks.rs index f193f86..426e517 100644 --- a/src/macro_hooks.rs +++ b/src/macro_hooks.rs @@ -1,31 +1,37 @@ use core::{any::Any, fmt, ops::ControlFlow}; use emit_core::{ - clock::{Clock, ErasedClock}, - ctxt::{Ctxt, ErasedCtxt}, + clock::Clock, emitter::Emitter, - empty::Empty, - event::Event, - extent::Extent, + extent::{Extent, ToExtent}, filter::Filter, props::Props, str::ToStr, template::Template, value::{ToValue, Value}, +}; + +#[cfg(feature = "alloc")] +use emit_core::{ + ctxt::Ctxt, + empty::Empty, + event::Event, + runtime::{AmbientClock, AmbientCtxt}, well_known::{SPAN_ID_KEY, SPAN_PARENT_KEY, TRACE_ID_KEY}, }; -use emit_core::extent::ToExtent; +#[cfg(feature = "alloc")] +use crate::{frame::Frame, IdRng}; + #[cfg(feature = "std")] use std::error::Error; use crate::{ base_emit, - frame::Frame, id::{SpanId, TraceId}, template::{Formatter, Part}, timer::TimerGuard, - IdRng, Level, Str, Timer, + Level, Str, Timer, }; /** @@ -436,8 +442,9 @@ impl<'a> __PrivateKeyHook for Str<'a> { } #[track_caller] -pub fn __private_format(tpl: Template, props: impl Props) -> String { - let mut s = String::new(); +#[cfg(feature = "alloc")] +pub fn __private_format(tpl: Template, props: impl Props) -> alloc::string::String { + let mut s = alloc::string::String::new(); tpl.render(props).write(&mut s).expect("infallible write"); s @@ -454,8 +461,8 @@ pub fn __private_emit( let rt = crate::runtime::shared(); base_emit( - rt.emitter().and(to), - rt.filter().and(when), + rt.emitter().and_emitter(to), + rt.filter().and_filter(when), rt.ctxt(), extent.to_extent().or_else(|| rt.now().to_extent()), tpl, @@ -464,28 +471,31 @@ pub fn __private_emit( } #[track_caller] -pub fn __private_push_ctxt(props: impl Props) -> Frame<&'static (dyn ErasedCtxt + Send + Sync)> { +#[cfg(feature = "alloc")] +pub fn __private_push_ctxt(props: impl Props) -> Frame> { let rt = crate::runtime::shared(); Frame::new_push(rt.ctxt(), props) } #[track_caller] -pub fn __private_root_ctxt(props: impl Props) -> Frame<&'static (dyn ErasedCtxt + Send + Sync)> { +#[cfg(feature = "alloc")] +pub fn __private_root_ctxt(props: impl Props) -> Frame> { let rt = crate::runtime::shared(); Frame::new_root(rt.ctxt(), props) } #[track_caller] +#[cfg(feature = "alloc")] pub fn __private_push_span_ctxt( when: impl Filter, tpl: Template, ctxt_props: impl Props, evt_props: impl Props, ) -> ( - Frame>, - Option>, + Frame>>, + Option>>, ) { struct TraceContext { trace_id: Option, diff --git a/src/platform.rs b/src/platform.rs index 3758134..4882dc4 100644 --- a/src/platform.rs +++ b/src/platform.rs @@ -3,6 +3,7 @@ use crate::{clock::Clock, rng::Rng, Timestamp}; #[cfg(not(feature = "std"))] use emit_core::empty::Empty; +#[cfg(feature = "std")] use emit_core::runtime::{InternalClock, InternalRng}; #[cfg(feature = "std")] @@ -26,8 +27,6 @@ type DefaultIdGen = Empty; #[cfg(feature = "std")] pub(crate) type DefaultCtxt = thread_local_ctxt::ThreadLocalCtxt; -#[cfg(not(feature = "std"))] -pub(crate) type DefaultCtxt = Empty; pub(crate) struct Platform { #[cfg(not(feature = "std"))] @@ -35,7 +34,7 @@ pub(crate) struct Platform { #[cfg(feature = "std")] pub(crate) clock: Box, #[cfg(not(feature = "std"))] - pub(crate) id_gen: DefaultIdGen, + pub(crate) rng: DefaultIdGen, #[cfg(feature = "std")] pub(crate) rng: Box, } diff --git a/src/setup.rs b/src/setup.rs index 36ad2af..3303eaf 100644 --- a/src/setup.rs +++ b/src/setup.rs @@ -58,7 +58,7 @@ impl Setup Setup, TFilter, TCtxt> { Setup { - emitter: self.emitter.and(emitter), + emitter: self.emitter.and_emitter(emitter), filter: self.filter, ctxt: self.ctxt, platform: self.platform, diff --git a/targets/otlp/src/data/metrics.rs b/targets/otlp/src/data/metrics.rs index a0586a8..e049924 100644 --- a/targets/otlp/src/data/metrics.rs +++ b/targets/otlp/src/data/metrics.rs @@ -2,7 +2,7 @@ use std::{borrow::Cow, ops::ControlFlow}; use emit::{ value::FromValue, - well_known::{METRIC_KIND_KEY, METRIC_KIND_SUM, METRIC_NAME_KEY, METRIC_VALUE_KEY}, + well_known::{METRIC_AGG_KEY, METRIC_AGG_SUM, METRIC_NAME_KEY, METRIC_VALUE_KEY}, Props, }; use emit_batcher::BatchError; @@ -21,10 +21,10 @@ impl EventEncoder { ) -> Option { use prost::Message; - if let (Some(metric_name), Some(metric_value), metric_kind) = ( + if let (Some(metric_name), Some(metric_value), metric_agg) = ( evt.props().get(METRIC_NAME_KEY), evt.props().get(METRIC_VALUE_KEY), - evt.props().get(METRIC_KIND_KEY), + evt.props().get(METRIC_AGG_KEY), ) { use crate::data::generated::{common::v1::*, metrics::v1::*}; @@ -75,8 +75,8 @@ impl EventEncoder { }), }; - let data = match metric_kind.and_then(|kind| kind.to_cow_str()).as_deref() { - Some(METRIC_KIND_SUM) => Some(metric::Data::Sum(Sum { + let data = match metric_agg.and_then(|kind| kind.to_cow_str()).as_deref() { + Some(METRIC_AGG_SUM) => Some(metric::Data::Sum(Sum { aggregation_temporality, is_monotonic: false, data_points: vec![data_point], diff --git a/tests/smoke-test/main.rs b/tests/smoke-test/main.rs index 22bc978..b56963e 100644 --- a/tests/smoke-test/main.rs +++ b/tests/smoke-test/main.rs @@ -141,15 +141,15 @@ fn increment(metric: &AtomicUsize) { fn sample_metrics() { let now = emit::runtime::shared().now(); - for (metric_value, metric_kind, metric_name) in [( + for (metric_value, metric_agg, metric_name) in [( &COUNT, - emit::well_known::METRIC_KIND_SUM, + emit::well_known::METRIC_AGG_SUM, "smoke_test::count", )] { emit::emit!( extent: now, - "{metric_kind} of {metric_name} is {metric_value}", - metric_kind, + "{metric_agg} of {metric_name} is {metric_value}", + metric_agg, metric_name, metric_value: metric_value.load(Ordering::Relaxed), );