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

Commit

Permalink
simplify Event type
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Aug 24, 2023
1 parent 566a073 commit 59f9dad
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 165 deletions.
100 changes: 26 additions & 74 deletions core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand All @@ -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 {
Expand All @@ -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<U: Props>(self, other: U) -> Event<'a, Chain<P, U>> {
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 {
Expand All @@ -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>(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<T: Props> fmt::Debug for AsDebug<T> {
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()
}
}
32 changes: 2 additions & 30 deletions core/src/extent.rs
Original file line number Diff line number Diff line change
@@ -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<Range<Timestamp>>);
Expand Down Expand Up @@ -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;
}
Expand Down
34 changes: 0 additions & 34 deletions core/src/well_known.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,17 @@ 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";
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<Timestamp> {
self.get(TIMESTAMP_KEY)?.to_timestamp()
}

fn timestamp_start(&self) -> Option<Timestamp> {
self.get(TIMESTAMP_START_KEY)?.to_timestamp()
}

fn lvl(&self) -> Option<Level> {
self.get(LVL_KEY)?.to_level()
}
Expand All @@ -59,14 +33,6 @@ pub trait WellKnown: Props {
self.get(SPAN_PARENT_KEY)?.to_span_id()
}

fn msg(&self) -> Option<Value> {
self.get(MSG_KEY)
}

fn tpl(&self) -> Option<Value> {
self.get(TPL_KEY)
}

fn err(&self) -> Option<Value> {
self.get(ERR_KEY)
}
Expand Down
5 changes: 0 additions & 5 deletions macros/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ pub fn expand_tokens(opts: ExpandTokens) -> Result<TokenStream, syn::Error> {
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();
Expand Down
5 changes: 0 additions & 5 deletions macros/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
}
Expand Down
11 changes: 0 additions & 11 deletions macros/src/props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}
5 changes: 0 additions & 5 deletions macros/src/with.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,6 @@ impl Parse for Args {
pub fn expand_tokens(opts: ExpandTokens) -> Result<TokenStream, syn::Error> {
let props = syn::parse2::<Props>(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::<Stmt>(opts.item)?;
match &mut item {
// A synchronous function
Expand Down
2 changes: 1 addition & 1 deletion targets/otlp/src/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 59f9dad

Please sign in to comment.