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

Commit

Permalink
get things to build again
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Aug 23, 2023
1 parent 3e0e836 commit 587a442
Show file tree
Hide file tree
Showing 9 changed files with 99 additions and 33 deletions.
68 changes: 60 additions & 8 deletions core/src/extent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,37 +6,83 @@ 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<Range<Timestamp>>);

impl Extent {
pub fn point(ts: Timestamp) -> Self {
Extent(Some(ts..ts))
pub fn new(ts: Range<Timestamp>) -> Self {
Extent(Some(ts))
}

pub fn span(ts: Range<Timestamp>) -> 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<Timestamp>> {
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<Timestamp>> {
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 {
self.0.is_none()
}
}

impl From<Timestamp> for Extent {
fn from(value: Timestamp) -> Self {
Extent::point(value)
}
}

impl From<Range<Timestamp>> for Extent {
fn from(value: Range<Timestamp>) -> Self {
Extent::new(value)
}
}

impl From<Option<Range<Timestamp>>> for Extent {
fn from(value: Option<Range<Timestamp>>) -> 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,
Expand Down Expand Up @@ -78,6 +124,12 @@ impl<T: ToExtent> ToExtent for Option<T> {
}
}

impl ToExtent for Extent {
fn to_extent(&self) -> Extent {
self.clone()
}
}

impl ToExtent for Timestamp {
fn to_extent(&self) -> Extent {
Extent::point(*self)
Expand All @@ -86,7 +138,7 @@ impl ToExtent for Timestamp {

impl ToExtent for Range<Timestamp> {
fn to_extent(&self) -> Extent {
Extent::span(self.clone())
Extent::new(self.clone())
}
}

Expand Down
14 changes: 11 additions & 3 deletions core/src/props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,11 @@ impl<'a, P: Props + ?Sized + 'a> Props for alloc::boxed::Box<P> {
}

impl<K: ToKey, V: ToValue> 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())
}
}

Expand Down Expand Up @@ -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<T, U> {
Expand Down
2 changes: 1 addition & 1 deletion core/src/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl<C: Clock> Timer<C> {
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(),
}
}
Expand Down
6 changes: 3 additions & 3 deletions core/src/well_known.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -43,7 +44,7 @@ fn base_emit(
to: impl Target,
when: impl Filter,
ctxt: impl Ctxt,
ts: Option<Range<Timestamp>>,
ts: impl ToExtent,
tpl: Template,
props: impl Props,
) {
Expand Down Expand Up @@ -82,6 +83,7 @@ pub fn emit(evt: &Event<impl Props>) {
ambient,
ambient,
evt.extent()
.range()
.cloned()
.or_else(|| ambient.now().map(|ts| ts..ts)),
tpl,
Expand Down
17 changes: 11 additions & 6 deletions src/macro_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use emit_core::{
value::{ToValue, Value},
};

use emit_core::extent::ToExtent;
#[cfg(feature = "std")]
use std::error::Error;

Expand Down Expand Up @@ -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,
) {
Expand All @@ -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,
);
Expand Down Expand Up @@ -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<Value<'v>> {
Expand Down
11 changes: 7 additions & 4 deletions src/platform/thread_local_ctxt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
}

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 @@ -64,7 +64,7 @@ impl emit_core::target::Target for OtlpLogsTarget {
fn event<P: emit_core::props::Props>(&self, evt: &emit_core::event::Event<P>) {
let time_unix_nano = evt
.extent()
.map(|ts| ts.end)
.to_point()
.map(|ts| ts.to_unix().as_nanos() as u64)
.unwrap_or_default();

Expand Down
8 changes: 2 additions & 6 deletions targets/term/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,10 @@ impl emit::target::Target for Stdout {
fn print(out: &BufferWriter, buf: &mut Buffer, evt: &emit::Event<impl emit::Props>) {
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;
}
Expand Down

0 comments on commit 587a442

Please sign in to comment.