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

Commit

Permalink
fill in some more tracing integration
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Jan 25, 2024
1 parent 53764cf commit b823ee8
Show file tree
Hide file tree
Showing 9 changed files with 358 additions and 33 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ members = [
"targets/tracing",
"targets/log",
"macros",
"benchmark", "targets/tracing", "targets/log",
"benchmark", "targets/tracing", "targets/log", "tests/tracing-test",
]
exclude = [
"tests/smoke-test"
Expand Down
86 changes: 85 additions & 1 deletion src/id.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use emit_core::{
props::{FromProps, Props},
rng::Rng,
str::Str,
value::FromValue,
well_known::{SPAN_ID_KEY, TRACE_ID_KEY},
well_known::{SPAN_ID_KEY, SPAN_PARENT_KEY, TRACE_ID_KEY},
};

use crate::value::{ToValue, Value};
use core::{
fmt,
num::{NonZeroU128, NonZeroU64},
ops::ControlFlow,
str,
str::FromStr,
};
Expand Down Expand Up @@ -51,6 +53,15 @@ impl<'v> FromValue<'v> for TraceId {
}
}

impl Props for TraceId {
fn for_each<'kv, F: FnMut(Str<'kv>, Value<'kv>) -> ControlFlow<()>>(
&'kv self,
mut for_each: F,
) -> ControlFlow<()> {
for_each(Str::new(TRACE_ID_KEY), self.to_value())
}
}

impl<'v> FromProps<'v> for TraceId {
fn from_props<P: Props + ?Sized>(props: &'v P) -> Option<Self> {
props.get(TRACE_ID_KEY)?.pull()
Expand Down Expand Up @@ -158,12 +169,85 @@ impl<'v> FromValue<'v> for SpanId {
}
}

impl Props for SpanId {
fn for_each<'kv, F: FnMut(Str<'kv>, Value<'kv>) -> ControlFlow<()>>(
&'kv self,
mut for_each: F,
) -> ControlFlow<()> {
for_each(Str::new(SPAN_ID_KEY), self.to_value())
}
}

impl<'v> FromProps<'v> for SpanId {
fn from_props<P: Props + ?Sized>(props: &'v P) -> Option<Self> {
props.get(SPAN_ID_KEY)?.pull()
}
}

#[derive(Clone, Copy)]
pub struct SpanParent(SpanId);

impl SpanParent {
pub const fn new(id: SpanId) -> Self {
SpanParent(id)
}

pub const fn id(&self) -> SpanId {
self.0
}
}

impl fmt::Debug for SpanParent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
}
}

impl fmt::Display for SpanParent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Display::fmt(&self.0, f)
}
}

impl From<SpanId> for SpanParent {
fn from(value: SpanId) -> Self {
SpanParent::new(value)
}
}

impl From<SpanParent> for SpanId {
fn from(value: SpanParent) -> Self {
value.id()
}
}

impl ToValue for SpanParent {
fn to_value(&self) -> Value {
self.0.to_value()
}
}

impl<'v> FromValue<'v> for SpanParent {
fn from_value(value: Value<'v>) -> Option<Self> {
Some(SpanParent(SpanId::from_value(value)?))
}
}

impl Props for SpanParent {
fn for_each<'kv, F: FnMut(Str<'kv>, Value<'kv>) -> ControlFlow<()>>(
&'kv self,
mut for_each: F,
) -> ControlFlow<()> {
for_each(Str::new(SPAN_PARENT_KEY), self.to_value())
}
}

impl<'v> FromProps<'v> for SpanParent {
fn from_props<P: Props + ?Sized>(props: &'v P) -> Option<Self> {
props.get(SPAN_PARENT_KEY)?.pull()
}
}

impl SpanId {
pub fn new(v: NonZeroU64) -> Self {
SpanId(v)
Expand Down
12 changes: 11 additions & 1 deletion src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ use emit_core::{
filter::Filter,
props::{FromProps, Props},
runtime::InternalFilter,
str::Str,
value::FromValue,
well_known::LVL_KEY,
};

use crate::value::{ToValue, Value};
use core::{fmt, str::FromStr};
use core::{fmt, ops::ControlFlow, str::FromStr};

#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum Level {
Expand Down Expand Up @@ -107,6 +108,15 @@ impl<'v> FromValue<'v> for Level {
}
}

impl Props for Level {
fn for_each<'kv, F: FnMut(Str<'kv>, Value<'kv>) -> ControlFlow<()>>(
&'kv self,
mut for_each: F,
) -> ControlFlow<()> {
for_each(Str::new(LVL_KEY), self.to_value())
}
}

impl<'v> FromProps<'v> for Level {
fn from_props<P: Props + ?Sized>(props: &'v P) -> Option<Self> {
props.get(LVL_KEY)?.pull()
Expand Down
27 changes: 27 additions & 0 deletions src/setup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ impl Setup {
}

impl<TEmitter: Emitter, TFilter: Filter, TCtxt: Ctxt> Setup<TEmitter, TFilter, TCtxt> {
#[must_use = "call `.init()` to finish setup"]
pub fn to<UEmitter: Emitter>(self, emitter: UEmitter) -> Setup<UEmitter, TFilter, TCtxt> {
Setup {
emitter,
Expand All @@ -51,6 +52,7 @@ impl<TEmitter: Emitter, TFilter: Filter, TCtxt: Ctxt> Setup<TEmitter, TFilter, T
}
}

#[must_use = "call `.init()` to finish setup"]
pub fn and_to<UEmitter: Emitter>(
self,
emitter: UEmitter,
Expand All @@ -59,17 +61,42 @@ impl<TEmitter: Emitter, TFilter: Filter, TCtxt: Ctxt> Setup<TEmitter, TFilter, T
emitter: self.emitter.and(emitter),
filter: self.filter,
ctxt: self.ctxt,
platform: self.platform,
}
}

#[must_use = "call `.init()` to finish setup"]
pub fn map_to<UEmitter: Emitter>(
self,
map: impl FnOnce(TEmitter) -> UEmitter,
) -> Setup<UEmitter, TFilter, TCtxt> {
Setup {
emitter: map(self.emitter),
filter: self.filter,
ctxt: self.ctxt,
platform: self.platform,
}
}

#[must_use = "call `.init()` to finish setup"]
pub fn with<UCtxt: Ctxt>(self, ctxt: UCtxt) -> Setup<TEmitter, TFilter, UCtxt> {
Setup {
emitter: self.emitter,
filter: self.filter,
ctxt,
platform: self.platform,
}
}

#[must_use = "call `.init()` to finish setup"]
pub fn map_with<UCtxt: Ctxt>(
self,
map: impl FnOnce(TCtxt) -> UCtxt,
) -> Setup<TEmitter, TFilter, UCtxt> {
Setup {
emitter: self.emitter,
filter: self.filter,
ctxt: map(self.ctxt),
platform: self.platform,
}
}
Expand Down
5 changes: 5 additions & 0 deletions targets/tracing/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,8 @@ path = "../../"

[dependencies.tracing]
version = "0.1"
default-features = false

[dependencies.tracing-core]
version = "0.1"
default-features = false
Loading

0 comments on commit b823ee8

Please sign in to comment.