Skip to content
This repository has been archived by the owner on Jun 8, 2024. It is now read-only.

Commit

Permalink
fix up nostd build
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Feb 8, 2024
1 parent 5c8470e commit 8a2c124
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 84 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion core/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ pub trait Emitter {

fn blocking_flush(&self, timeout: Duration);

fn and<U>(self, other: U) -> And<Self, U>
fn and_emitter<U>(self, other: U) -> And<Self, U>
where
Self: Sized,
{
Expand Down
2 changes: 1 addition & 1 deletion core/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::{
pub trait Filter {
fn matches<P: Props>(&self, evt: &Event<P>) -> bool;

fn and<U>(self, other: U) -> And<Self, U>
fn and_filter<U>(self, other: U) -> And<Self, U>
where
Self: Sized,
{
Expand Down
83 changes: 52 additions & 31 deletions core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: ErasedEmitter + Send + Sync + 'static> AmbientTarget for T {
impl<T: ErasedEmitter + Send + Sync + 'static> AnyEmitter for T {
fn as_any(&self) -> &dyn Any {
self
}
Expand All @@ -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<T: ErasedFilter + Send + Sync + 'static> AmbientFilter for T {
impl<T: ErasedFilter + Send + Sync + 'static> AnyFilter for T {
fn as_any(&self) -> &dyn Any {
self
}
Expand All @@ -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<T: ErasedCtxt + Send + Sync + 'static> AmbientCtxt for T {
impl<T: ErasedCtxt + Send + Sync + 'static> AnyCtxt for T {
fn as_any(&self) -> &dyn Any {
self
}
Expand All @@ -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<T: ErasedClock + Send + Sync + 'static> AmbientClock for T {
impl<T: ErasedClock + Send + Sync + 'static> AnyClock for T {
fn as_any(&self) -> &dyn Any {
self
}
Expand All @@ -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<T: ErasedRng + Send + Sync + 'static> AmbientGenId for T {
impl<T: ErasedRng + Send + Sync + 'static> AnyRng for T {
fn as_any(&self) -> &dyn Any {
self
}
Expand All @@ -474,11 +484,11 @@ mod std_support {
}

type AmbientSyncValue = Runtime<
Box<dyn AmbientTarget + Send + Sync>,
Box<dyn AmbientFilter + Send + Sync>,
Box<dyn AmbientCtxt + Send + Sync>,
Box<dyn AmbientClock + Send + Sync>,
Box<dyn AmbientGenId + Send + Sync>,
Box<dyn AnyEmitter + Send + Sync>,
Box<dyn AnyFilter + Send + Sync>,
Box<dyn AnyCtxt + Send + Sync>,
Box<dyn AnyClock + Send + Sync>,
Box<dyn AnyRng + Send + Sync>,
>;

type AmbientSyncRuntime = Runtime<
Expand All @@ -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 {}
Expand Down Expand Up @@ -525,14 +535,12 @@ mod std_support {
.set({
let value = pipeline
.map_emitter(|emitter| {
Box::new(emitter) as Box<dyn AmbientTarget + Send + Sync>
})
.map_filter(|filter| {
Box::new(filter) as Box<dyn AmbientFilter + Send + Sync>
Box::new(emitter) as Box<dyn AnyEmitter + Send + Sync>
})
.map_ctxt(|ctxt| Box::new(ctxt) as Box<dyn AmbientCtxt + Send + Sync>)
.map_clock(|clock| Box::new(clock) as Box<dyn AmbientClock + Send + Sync>)
.map_rng(|id_gen| Box::new(id_gen) as Box<dyn AmbientGenId + Send + Sync>);
.map_filter(|filter| Box::new(filter) as Box<dyn AnyFilter + Send + Sync>)
.map_ctxt(|ctxt| Box::new(ctxt) as Box<dyn AnyCtxt + Send + Sync>)
.map_clock(|clock| Box::new(clock) as Box<dyn AnyClock + Send + Sync>)
.map_rng(|id_gen| Box::new(id_gen) as Box<dyn AnyRng + Send + Sync>);

let runtime = Runtime::build(
value.emitter().as_super() as *const _,
Expand Down Expand Up @@ -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
}
Expand All @@ -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"))]
Expand Down
30 changes: 19 additions & 11 deletions core/src/well_known.rs
Original file line number Diff line number Diff line change
@@ -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";
2 changes: 1 addition & 1 deletion metrics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ edition = "2021"
[dependencies.emit]
path = "../"
default-features = false
features = ["sval"]
features = ["alloc", "sval"]
18 changes: 9 additions & 9 deletions metrics/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<E>(count: usize, emitter: E) -> MetricsEmitter<E> {
Expand Down Expand Up @@ -46,7 +46,7 @@ impl<E: emit::Emitter> emit::Emitter for MetricsEmitter<E> {
continue;
}

let metric_kind = METRIC_KIND_SUM;
let metric_agg = METRIC_AGG_SUM;

let histogram = histogram.compute();
let x = histogram.timestamp_range();
Expand All @@ -57,9 +57,9 @@ impl<E: emit::Emitter> emit::Emitter for MetricsEmitter<E> {

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,
Expand Down Expand Up @@ -135,15 +135,15 @@ impl Aggregator {
}

pub fn record_metric(&mut self, evt: &Event<impl Props>) -> 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());
}
}
Expand Down
Loading

0 comments on commit 8a2c124

Please sign in to comment.