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

Commit

Permalink
start cleaning things up
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Mar 14, 2024
1 parent 27c8c40 commit eef3f70
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 80 deletions.
4 changes: 0 additions & 4 deletions batcher/src/internal_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@ pub(crate) struct InternalMetrics {
pub(crate) struct Counter(AtomicUsize);

impl Counter {
pub const fn new() -> Self {
Counter(AtomicUsize::new(0))
}

pub fn increment(&self) {
self.increment_by(1);
}
Expand Down
16 changes: 14 additions & 2 deletions core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{
path::Path,
props::{ByRef, ErasedProps, Props},
template::{Render, Template},
timestamp::Timestamp,
};

#[derive(Clone)]
Expand All @@ -24,13 +25,13 @@ impl<'a, P> Event<'a, P> {
pub fn new(
module: impl Into<Path<'a>>,
extent: impl ToExtent,
tpl: Template<'a>,
tpl: impl Into<Template<'a>>,
props: P,
) -> Self {
Event {
module: module.into(),
extent: extent.to_extent(),
tpl,
tpl: tpl.into(),
props,
}
}
Expand All @@ -43,6 +44,17 @@ impl<'a, P> Event<'a, P> {
self.extent.as_ref()
}

pub fn ts(&self) -> Option<&Timestamp> {
self.extent.as_ref().map(|extent| extent.as_point())
}

pub fn ts_start(&self) -> Option<&Timestamp> {
self.extent
.as_ref()
.and_then(|extent| extent.as_span())
.map(|span| &span.start)
}

pub fn tpl(&self) -> &Template<'a> {
&self.tpl
}
Expand Down
8 changes: 8 additions & 0 deletions core/src/extent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,14 @@ impl Extent {
&self.range.end
}

pub fn as_span(&self) -> Option<&Range<Timestamp>> {
if self.is_span() {
Some(&self.range)
} else {
None
}
}

pub fn len(&self) -> Option<Duration> {
if self.is_span() {
self.range.end.duration_since(self.range.start)
Expand Down
8 changes: 2 additions & 6 deletions core/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,7 @@ impl<'a> serde::Serialize for Path<'a> {

#[cfg(feature = "alloc")]
mod alloc_support {
use alloc::{
borrow::{Cow, ToOwned},
boxed::Box,
string::String,
};
use alloc::{borrow::Cow, boxed::Box};

use super::*;

Expand All @@ -125,7 +121,7 @@ mod alloc_support {
Path(Str::new_cow_ref(path))
}

pub fn to_cow(&self) -> Cow<'static, str> {
pub fn to_cow(&self) -> Cow<'a, str> {
self.0.to_cow()
}

Expand Down
6 changes: 5 additions & 1 deletion core/src/props.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ pub trait Props {
ByRef(self)
}

fn pull<'kv, K: ToStr, V: FromValue<'kv>>(&'kv self, key: K) -> Option<V> {
fn pull<'kv, V: FromValue<'kv>, K: ToStr>(&'kv self, key: K) -> Option<V> {
self.get(key).and_then(|v| v.cast())
}
}
Expand All @@ -66,6 +66,10 @@ impl<'a, P: Props + ?Sized> Props for &'a P {
fn get<'v, K: ToStr>(&'v self, key: K) -> Option<Value<'v>> {
(**self).get(key)
}

fn pull<'kv, V: FromValue<'kv>, K: ToStr>(&'kv self, key: K) -> Option<V> {
(**self).pull(key)
}
}

impl<P: Props> Props for Option<P> {
Expand Down
6 changes: 6 additions & 0 deletions core/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ impl<'a> fmt::Display for Template<'a> {
}
}

impl<'a> From<&'a [Part<'a>]> for Template<'a> {
fn from(value: &'a [Part<'a>]) -> Self {
Template::new_ref(value)
}
}

impl Template<'static> {
pub fn new(parts: &'static [Part<'static>]) -> Self {
Template(TemplateKind::Parts(parts))
Expand Down
54 changes: 6 additions & 48 deletions core/src/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,21 @@ impl<'v> Value<'v> {
pub fn parse<T: FromStr>(&self) -> Option<T> {
struct Extract<T>(Option<T>);

impl<'v, T: FromStr> Visitor<'v> for Extract<T> {
fn visit_any(&mut self, _: Value) {}

fn visit_str(&mut self, value: &str) {
self.0 = value.parse().ok();
}
}

let mut visitor = Extract(None);
self.visit(&mut visitor);
visitor.0
}

pub fn visit(&self, visitor: impl Visitor<'v>) {
struct Visit<V>(V);

impl<'v, V: Visitor<'v>> value_bag::visit::Visit<'v> for Visit<V> {
fn visit_any(&mut self, value: value_bag::ValueBag) -> Result<(), value_bag::Error> {
self.0.visit_any(Value(value));

impl<'v, T: FromStr> value_bag::visit::Visit<'v> for Extract<T> {
fn visit_any(&mut self, _: value_bag::ValueBag) -> Result<(), value_bag::Error> {
Ok(())
}

fn visit_str(&mut self, value: &str) -> Result<(), value_bag::Error> {
self.0.visit_str(value);
self.0 = value.parse().ok();

Ok(())
}
}

let _ = self.0.visit(Visit(visitor));
let mut visitor = Extract(None);
let _ = self.0.visit(&mut visitor);
visitor.0
}

pub fn to_borrowed_str(&self) -> Option<&'v str> {
Expand All @@ -93,32 +77,6 @@ impl<'v> Value<'v> {
}
}

pub trait Visitor<'v> {
fn visit_any(&mut self, value: Value);

fn visit_str(&mut self, value: &str) {
self.visit_any(Value::from(value))
}

fn visit_f64(&mut self, value: f64) {
self.visit_any(Value::from(value))
}
}

impl<'a, 'v, V: Visitor<'v> + ?Sized> Visitor<'v> for &'a mut V {
fn visit_any(&mut self, value: Value) {
(**self).visit_any(value)
}

fn visit_str(&mut self, value: &str) {
(**self).visit_str(value)
}

fn visit_f64(&mut self, value: f64) {
(**self).visit_f64(value)
}
}

impl<'v> fmt::Debug for Value<'v> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
fmt::Debug::fmt(&self.0, f)
Expand Down
3 changes: 2 additions & 1 deletion core/src/well_known.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,9 @@ 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 LVL_ERROR: &'static str = "error";

// Error
pub const ERR_KEY: &'static str = "err";

// Trace
Expand Down
18 changes: 11 additions & 7 deletions src/level.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use emit_core::{
event::Event, filter::Filter, props::Props, runtime::InternalFilter, value::FromValue,
well_known::LVL_KEY,
event::Event,
filter::Filter,
props::Props,
runtime::InternalFilter,
value::FromValue,
well_known::{LVL_DEBUG, LVL_ERROR, LVL_INFO, LVL_KEY, LVL_WARN},
};

use crate::value::{ToValue, Value};
Expand All @@ -23,10 +27,10 @@ impl fmt::Debug for Level {
impl fmt::Display for Level {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Level::Info => "info",
Level::Error => "error",
Level::Warn => "warn",
Level::Debug => "debug",
Level::Info => LVL_INFO,
Level::Error => LVL_ERROR,
Level::Warn => LVL_WARN,
Level::Debug => LVL_DEBUG,
})
}
}
Expand Down Expand Up @@ -129,7 +133,7 @@ impl MinLevel {
impl Filter for MinLevel {
fn matches<P: Props>(&self, evt: &Event<P>) -> bool {
evt.props()
.pull::<_, Level>(LVL_KEY)
.pull::<Level, _>(LVL_KEY)
.unwrap_or(self.default)
>= self.min
}
Expand Down
4 changes: 2 additions & 2 deletions src/macro_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -561,8 +561,8 @@ pub fn __private_push_span_ctxt<'a, 'b, E: Emitter, F: Filter, C: Ctxt, T: Clock
let mut span_parent = None;

rt.with_current(|current| {
trace_id = current.pull::<_, TraceId>(TRACE_ID_KEY);
span_parent = current.pull::<_, SpanId>(SPAN_ID_KEY);
trace_id = current.pull::<TraceId, _>(TRACE_ID_KEY);
span_parent = current.pull::<SpanId, _>(SPAN_ID_KEY);
});

trace_id = trace_id.or_else(|| rt.gen_trace_id());
Expand Down
4 changes: 2 additions & 2 deletions targets/log/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl emit::Emitter for GlobalLogger {
.level(
match evt
.props()
.pull::<_, emit::Level>(emit::well_known::LVL_KEY)
.pull::<emit::Level, _>(emit::well_known::LVL_KEY)
{
Some(emit::Level::Debug) => log::Level::Debug,
Some(emit::Level::Info) => log::Level::Info,
Expand All @@ -29,7 +29,7 @@ impl emit::Emitter for GlobalLogger {
);
}

fn blocking_flush(&self, timeout: Duration) {
fn blocking_flush(&self, _: Duration) {
// NOTE: Doesn't respect the timeout
log::logger().flush();
}
Expand Down
4 changes: 2 additions & 2 deletions targets/otlp/src/client/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,8 @@ async fn send_request(
let mut span_id = None;

rt.ctxt().with_current(|props| {
trace_id = props.pull::<_, emit::TraceId>(TRACE_ID_KEY);
span_id = props.pull::<_, emit::SpanId>(SPAN_ID_KEY);
trace_id = props.pull::<emit::TraceId, _>(TRACE_ID_KEY);
span_id = props.pull::<emit::SpanId, _>(SPAN_ID_KEY);
});

req = if let (Some(trace_id), Some(span_id)) = (trace_id, span_id) {
Expand Down
6 changes: 3 additions & 3 deletions targets/term/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ fn print_event(
buf: &mut Buffer,
evt: &emit::event::Event<impl emit::props::Props>,
) {
if let Some(span_id) = evt.props().pull::<_, emit::SpanId>(SPAN_ID_KEY) {
if let Some(trace_id) = evt.props().pull::<_, emit::TraceId>(TRACE_ID_KEY) {
if let Some(span_id) = evt.props().pull::<emit::SpanId, _>(SPAN_ID_KEY) {
if let Some(trace_id) = evt.props().pull::<emit::TraceId, _>(TRACE_ID_KEY) {
let trace_id_color = trace_id_color(&trace_id);

write_fg(buf, "▓", Color::Ansi256(trace_id_color));
Expand Down Expand Up @@ -233,7 +233,7 @@ fn print_event(
write_plain(buf, " ");
}

if let Some(level) = evt.props().pull::<_, emit::Level>(LVL_KEY) {
if let Some(level) = evt.props().pull::<emit::Level, _>(LVL_KEY) {
if let Some(level_color) = level_color(&level) {
write_fg(buf, level, Color::Ansi256(level_color));
write_plain(buf, " ");
Expand Down
4 changes: 2 additions & 2 deletions targets/tracing/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<C: emit::Ctxt, S: tracing::Subscriber> emit::Ctxt for TracingCtxt<C, S> {
static CALLSITE: tracing::callsite::DefaultCallsite =
tracing::callsite::DefaultCallsite::new(&METADATA);

let tracing_id = if let Some(span_id) = props.pull::<_, emit::SpanId>(SPAN_ID_KEY) {
let tracing_id = if let Some(span_id) = props.pull::<emit::SpanId, _>(SPAN_ID_KEY) {
let fields = tracing::field::FieldSet::new(
&[
emit::well_known::TRACE_ID_KEY,
Expand All @@ -55,7 +55,7 @@ impl<C: emit::Ctxt, S: tracing::Subscriber> emit::Ctxt for TracingCtxt<C, S> {
);

let trace_id = props
.pull::<_, emit::TraceId>(TRACE_ID_KEY)
.pull::<emit::TraceId, _>(TRACE_ID_KEY)
.map(tracing::field::display);

let id = self.1.new_span(&tracing::span::Attributes::new(
Expand Down

0 comments on commit eef3f70

Please sign in to comment.