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

Commit

Permalink
add an event_kind prop to distinguish metric spans from trace spans
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Apr 8, 2024
1 parent 52e6c63 commit 5bc0b53
Show file tree
Hide file tree
Showing 15 changed files with 437 additions and 95 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ members = [
"targets/file",
"targets/otlp",
"targets/otlp/gen",
"targets/opentelemetry",
"macros",
"benchmark",
"tests/smoke-test",
Expand Down
4 changes: 4 additions & 0 deletions core/src/well_known.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ pub const KEY_TS: &'static str = "ts";
pub const KEY_TS_START: &'static str = "ts_start";
pub const KEY_TPL: &'static str = "tpl";
pub const KEY_MSG: &'static str = "msg";
pub const KEY_EVENT_KIND: &'static str = "event_kind";

pub const EVENT_KIND_SPAN: &'static str = "span";
pub const EVENT_KIND_METRIC: &'static str = "metric";

// Log
pub const KEY_LVL: &'static str = "lvl";
Expand Down
12 changes: 6 additions & 6 deletions macros/src/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,17 +157,17 @@ fn inject_sync(
let template_tokens = template.template_tokens();

quote!({
let (mut __ctxt, __timer) = emit::__private::__private_push_span_ctxt(#rt_tokens, #module_tokens, #when_tokens, #template_tokens, #ctxt_props_tokens, #evt_props_tokens);
let (mut __ctxt, __timer, __span_props) = emit::__private::__private_push_span_ctxt(#rt_tokens, #module_tokens, #when_tokens, #template_tokens, #ctxt_props_tokens, #evt_props_tokens);
let __ctxt_guard = __ctxt.enter();

let #span_arg = emit::__private::__private_begin_span(__timer, |extent| {
let #span_arg = emit::__private::__private_begin_span(__timer, __span_props, |extent, props| {
emit::__private::__private_emit(
#rt_tokens,
#module_tokens,
emit::__private::__private_filter_span_complete(),
extent,
#template_tokens,
#evt_props_tokens,
emit::Props::chain(props, #evt_props_tokens),
)
});

Expand All @@ -190,17 +190,17 @@ fn inject_async(
let template_tokens = template.template_tokens();

quote!({
let (__ctxt, __timer) = emit::__private::__private_push_span_ctxt(#rt_tokens, #module_tokens, #when_tokens, #template_tokens, #ctxt_props_tokens, #evt_props_tokens);
let (__ctxt, __timer, __span_props) = emit::__private::__private_push_span_ctxt(#rt_tokens, #module_tokens, #when_tokens, #template_tokens, #ctxt_props_tokens, #evt_props_tokens);

__ctxt.in_future(async {
let #span_arg = emit::__private::__private_begin_span(__timer, |extent| {
let #span_arg = emit::__private::__private_begin_span(__timer, __span_props, |extent, props| {
emit::__private::__private_emit(
#rt_tokens,
#module_tokens,
emit::__private::__private_filter_span_complete(),
extent,
#template_tokens,
#evt_props_tokens,
emit::Props::chain(props, #evt_props_tokens),
)
});

Expand Down
90 changes: 33 additions & 57 deletions src/macro_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use emit_core::{
use emit_core::{
empty::Empty,
event::Event,
well_known::{KEY_SPAN_ID, KEY_SPAN_PARENT, KEY_TRACE_ID},
well_known::{KEY_SPAN_ID, KEY_TRACE_ID},
};

#[cfg(feature = "alloc")]
Expand All @@ -30,8 +30,7 @@ use std::error::Error;

use crate::{
base_emit,
timer::TimerGuard,
trace::{SpanId, TraceId},
trace::{Span, SpanId, SpanProps, TraceId},
Level, Timer,
};

Expand Down Expand Up @@ -528,39 +527,16 @@ pub fn __private_emit<'a, E: Emitter, F: Filter, C: Ctxt, T: Clock, R: Rng>(
#[cfg(feature = "alloc")]
pub fn __private_push_span_ctxt<'a, 'b, E: Emitter, F: Filter, C: Ctxt, T: Clock, R: Rng>(
rt: &'a Runtime<E, F, C, T, R>,
source: impl Into<Path<'b>>,
module: impl Into<Path<'b>>,
when: Option<impl Filter>,
tpl: Template<'b>,
ctxt_props: impl Props,
evt_props: impl Props,
) -> (Frame<Option<&'a C>>, Option<Timer<&'a T>>) {
struct TraceContext {
trace_id: Option<TraceId>,
span_parent: Option<SpanId>,
span_id: Option<SpanId>,
}

impl Props for TraceContext {
fn for_each<'kv, F: FnMut(Str<'kv>, Value<'kv>) -> ControlFlow<()>>(
&'kv self,
mut for_each: F,
) -> ControlFlow<()> {
if let Some(ref trace_id) = self.trace_id {
for_each(KEY_TRACE_ID.to_str(), trace_id.to_value())?;
}

if let Some(ref span_parent) = self.span_parent {
for_each(KEY_SPAN_PARENT.to_str(), span_parent.to_value())?;
}

if let Some(ref span_id) = self.span_id {
for_each(KEY_SPAN_ID.to_str(), span_id.to_value())?;
}

ControlFlow::Continue(())
}
}

) -> (
Frame<Option<&'a C>>,
Option<Timer<&'a T>>,
Option<SpanProps<Empty>>,
) {
let (mut trace_id, span_parent) = rt.ctxt().with_current(|current| {
(
current.pull::<TraceId, _>(KEY_TRACE_ID),
Expand All @@ -571,38 +547,38 @@ pub fn __private_push_span_ctxt<'a, 'b, E: Emitter, F: Filter, C: Ctxt, T: Clock
trace_id = trace_id.or_else(|| TraceId::random(rt.rng()));
let span_id = SpanId::random(rt.rng());

let trace_ctxt = TraceContext {
trace_id,
span_parent,
span_id,
};

let timer = Timer::start(rt.clock());

if FirstDefined(when, rt.filter()).matches(&Event::new(
source,
timer.extent().map(|extent| *extent.as_point()),
tpl,
ctxt_props.by_ref().chain(&trace_ctxt).chain(&evt_props),
)) {
(
Frame::push(Some(rt.ctxt()), trace_ctxt.chain(ctxt_props)),
Some(timer),
)
} else {
(Frame::push(None, Empty), None)
if let (Some(trace_id), Some(span_id)) = (trace_id, span_id) {
let timer = Timer::start(rt.clock());

let span_props = SpanProps::new(trace_id, span_parent, span_id, Empty);

if FirstDefined(when, rt.filter()).matches(&Event::new(
module,
timer.extent().map(|extent| *extent.as_point()),
tpl,
ctxt_props.by_ref().chain(&span_props).chain(&evt_props),
)) {
return (
Frame::push(Some(rt.ctxt()), span_props.ctxt().chain(ctxt_props)),
Some(timer),
Some(span_props),
);
}
}

(Frame::push(None, Empty), None, None)
}

#[track_caller]
pub fn __private_begin_span<C: Clock, F: FnOnce(Option<Extent>)>(
pub fn __private_begin_span<C: Clock, P: Props, F: FnOnce(Option<Extent>, SpanProps<P>)>(
timer: Option<Timer<C>>,
props: Option<SpanProps<P>>,
default_complete: F,
) -> TimerGuard<C, F> {
if let Some(timer) = timer {
TimerGuard::new(timer, default_complete)
) -> Span<C, P, F> {
if let (Some(timer), Some(props)) = (timer, props) {
Span::new(timer, props, default_complete)
} else {
TimerGuard::disabled()
Span::disabled()
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/metric.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use emit_core::{
str::{Str, ToStr},
template::{self, Template},
value::{ToValue, Value},
well_known::{KEY_METRIC_AGG, KEY_METRIC_NAME, KEY_METRIC_VALUE},
well_known::{
EVENT_KIND_METRIC, KEY_EVENT_KIND, KEY_METRIC_AGG, KEY_METRIC_NAME, KEY_METRIC_VALUE,
},
};

pub struct Metric<'a, P> {
Expand Down Expand Up @@ -153,6 +155,7 @@ impl<'a, P: Props> Props for Metric<'a, P> {
&'kv self,
mut for_each: F,
) -> ControlFlow<()> {
for_each(KEY_EVENT_KIND.to_str(), EVENT_KIND_METRIC.to_value())?;
for_each(KEY_METRIC_NAME.to_str(), self.name.to_value())?;
for_each(KEY_METRIC_AGG.to_str(), self.agg.to_value())?;
for_each(KEY_METRIC_VALUE.to_str(), self.value.by_ref())?;
Expand Down
Loading

0 comments on commit 5bc0b53

Please sign in to comment.