Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion batcher/src/internal_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ macro_rules! metrics {
pub(crate) struct $container { $(pub(crate) $name: $ty),* }

impl $container {
pub fn sample(&self) -> impl Iterator<Item = emit::metric::Metric<'static, emit::empty::Empty>> + 'static {
pub fn sample(&self) -> impl Iterator<Item = emit::metric::Metric<'static, usize, emit::empty::Empty>> + 'static {
let $container { $($name),* } = self;

[$(
Expand Down
2 changes: 2 additions & 0 deletions book/src/producing-events/metrics/distributions.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,8 @@ emit::count_sample!(
);
```

The above example demonstrates the data model in its most direct form. See [Building exponential histograms](#building-exponential-histograms) for details on how to compute midpoints.

### Exponential histogram data model

`emit`'s exponential histograms are a pair of well-known properties:
Expand Down
35 changes: 18 additions & 17 deletions book/src/producing-events/tracing/links.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ In `emit`, span links are expressed through the `span_links` [well-known propert
```rust
# extern crate emit;
#[emit::span(
guard: span,
// Add span links to the guard rather than as props in the macro so they aren't
// also added to any child spans or events
evt_props: emit::props! {
#[emit::as_serde]
span_links: [
"0a85ccaf666e11aaca6bd5d469e2850d-2b9caa35eaefed3a",
],
},
"wait a bit",
sleep_ms,
)]
fn wait_a_bit(sleep_ms: u64) {
// Add span links to the guard rather than as props in the macro so they aren't
// also added to any child spans or events
let span_links = [
"0a85ccaf666e11aaca6bd5d469e2850d-2b9caa35eaefed3a",
];
let _span = span.push_prop("span_links", emit::Value::capture_serde(&span_links));

std::thread::sleep(std::time::Duration::from_millis(sleep_ms));

emit::emit!("waiting a bit longer");
Expand All @@ -53,19 +53,20 @@ Since the expected type of `span_links` is a sequence, you'll need to use either
```rust
# extern crate emit;
#[emit::span(
guard: span,
// Add span links to the guard rather than as props in the macro so they aren't
// also added to any child spans or events
evt_props: emit::props! {
span_links: emit::span::SpanLinkSet::from_iter([
(
emit::span::TraceId::from_u128(0x0a85ccaf666e11aaca6bd5d469e2850d).unwrap(),
emit::span::SpanId::from_u64(0x2b9caa35eaefed3a).unwrap()
),
]),
},
"wait a bit",
sleep_ms,
)]
fn wait_a_bit(sleep_ms: u64) {
// Add span links to the guard rather than as props in the macro so they aren't
// also added to any child spans or events
let span_links = emit::span::SpanLinkSet::from_iter([
(emit::span::TraceId::from_u128(0x0a85ccaf666e11aaca6bd5d469e2850d).unwrap(), emit::span::SpanId::from_u64(0x2b9caa35eaefed3a).unwrap()),
]);

let _span = span.push_prop("span_links", span_links);

std::thread::sleep(std::time::Duration::from_millis(sleep_ms));

emit::emit!("waiting a bit longer");
Expand Down
4 changes: 3 additions & 1 deletion book/src/producing-events/tracing/properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,9 @@ Properties on spans have two visibility levels:
# extern crate emit;
#[emit::span(
guard: span,
evt_props: [("private_1", i)],
evt_props: emit::props! {
private_1: i,
},
"checking {public_1: i}",
public_2: i,
)]
Expand Down
2 changes: 1 addition & 1 deletion emitter/file/src/internal_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ macro_rules! metrics {
}

impl $internal_container {
pub fn sample(&self) -> impl Iterator<Item = emit::metric::Metric<'static, emit::empty::Empty>> + 'static {
pub fn sample(&self) -> impl Iterator<Item = emit::metric::Metric<'static, usize, emit::empty::Empty>> + 'static {
let $internal_container { $($metric),* } = self;

[$(
Expand Down
2 changes: 1 addition & 1 deletion emitter/otlp/src/internal_metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ macro_rules! metrics {
}

impl $internal_container {
pub fn sample(&self) -> impl Iterator<Item = emit::metric::Metric<'static, emit::empty::Empty>> + 'static {
pub fn sample(&self) -> impl Iterator<Item = emit::metric::Metric<'static, usize, emit::empty::Empty>> + 'static {
let $internal_container { $($metric),* } = self;

[$(
Expand Down
5 changes: 1 addition & 4 deletions examples/common_patterns/span_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,9 @@ fn worker<T>(queue: Weak<MessageQueue<T>>, mut process: impl FnMut(T)) {
// 2. Create a span for the worker that will include the span link
// This is an inline alternative to creating a function with `#[emit::span]`
// on it, which we could also use
let (span, frame) = emit::span_guard!("worker");
let (mut span, frame) = emit::span_guard!(evt_props: emit::props! { span_links }, "worker");

frame.call(move || {
// 3. Add the link as a property on the span
let mut span = span.push_prop("span_links", span_links);

span.start();

// Invoke the worker closure in the context of the span
Expand Down
11 changes: 5 additions & 6 deletions macros/src/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ use crate::{
capture,
props::{Props, push_evt_props},
template,
util::ToRefTokens,
};

pub struct ExpandPropsTokens {
Expand All @@ -19,7 +18,7 @@ The `props!` macro.
pub fn expand_props_tokens(opts: ExpandPropsTokens) -> Result<TokenStream, syn::Error> {
let props = syn::parse2::<Props>(opts.input)?;

Ok(props.props_tokens())
props.gen_bound_props_tokens()
}

pub struct ExpandTplTokens {
Expand Down Expand Up @@ -145,13 +144,13 @@ pub fn expand_evt_tokens(opts: ExpandEvtTokens) -> Result<TokenStream, syn::Erro

push_evt_props(&mut props, opts.level)?;

let extent_tokens = args.extent.to_tokens().to_ref_tokens();
let base_props_tokens = args.props.to_tokens().to_ref_tokens();
let extent_tokens = args.extent.to_tokens();
let base_props_tokens = args.props.to_tokens();
let template_tokens = template.template_tokens();
let props_tokens = props.props_tokens();
let props_tokens = props.gen_bound_props_tokens()?;
let mdl_tokens = args.mdl.to_tokens();

Ok(
quote!(emit::__private::__private_evt(#mdl_tokens, #template_tokens, #extent_tokens, #base_props_tokens, #props_tokens)),
quote!(emit::__private::__must_use_evt(emit::__private::__private_evt(#mdl_tokens, #template_tokens, #extent_tokens, #base_props_tokens, #props_tokens))),
)
}
24 changes: 16 additions & 8 deletions macros/src/capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,6 @@ pub fn eval_key_value_with_hook(
)
}

pub fn raw_key_value(fv: &FieldValue) -> syn::Result<TokenStream> {
let key = fv.key_expr()?;
let value = &fv.expr;

Ok(quote_spanned!(fv.span()=> (#key, #value)))
}

pub fn eval_key_with_hook(
attrs: &[Attribute],
fv: &FieldValue,
Expand All @@ -72,7 +65,7 @@ pub fn eval_key_with_hook(
hook::eval_hooks(&attrs, syn::parse_quote_spanned!(fv.span()=>#key_tokens))
}

pub(crate) fn value_with_hook(
pub fn value_with_hook(
expr: &Expr,
fn_name: &TokenStream,
interpolated: bool,
Expand All @@ -96,6 +89,21 @@ pub(crate) fn value_with_hook(
})
}

pub fn eval_value_with_hook(
attrs: &[Attribute],
expr: &Expr,
fn_name: &TokenStream,
interpolated: bool,
captured: bool,
) -> Result<TokenStream, syn::Error> {
let value_tokens = value_with_hook(expr, fn_name, interpolated, captured);

hook::eval_hooks(
&attrs,
syn::parse_quote_spanned!(expr.span()=>#value_tokens),
)
}

pub fn default_fn_name(fv: &FieldValue) -> TokenStream {
match fv.key_name().as_deref() {
// Well-known properties
Expand Down
38 changes: 14 additions & 24 deletions macros/src/dbg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ pub fn expand_tokens(opts: ExpandTokens) -> Result<TokenStream, syn::Error> {
push_loc_props(&mut props)?;
push_evt_props(&mut props, Some(quote!(emit::Level::Debug)))?;

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().to_ref_tokens();

let rt_tokens = args::RtArg::default().to_tokens()?.to_ref_tokens();
let when_tokens = None::<TokenStream>.to_option_tokens(quote!(&emit::Empty));

Expand All @@ -73,25 +69,19 @@ pub fn expand_tokens(opts: ExpandTokens) -> Result<TokenStream, syn::Error> {

let template_tokens = template.template_tokens().to_ref_tokens();

let emit_tokens = quote!(
emit::__private::__private_emit(
#rt_tokens,
#mdl_tokens,
#when_tokens,
#extent_tokens,
#template_tokens,
#base_props_tokens,
#props_tokens,
);
);

Ok(quote!({
match (#(#props_match_input_tokens),*) {
(#(#props_match_binding_tokens),*) => {
#emit_tokens
}
}
}))
props.match_bound_props_tokens(|props_tokens| {
Ok(quote!(
emit::__private::__private_emit(
#rt_tokens,
#mdl_tokens,
#when_tokens,
#extent_tokens,
#template_tokens,
#base_props_tokens,
#props_tokens,
)
))
})
}

fn push_loc_props(props: &mut Props) -> Result<(), syn::Error> {
Expand All @@ -116,7 +106,7 @@ fn compute_template(props: &Props) -> Result<Template, syn::Error> {

literal.push_str(name);
literal.push_str(" = {");
literal.push_str(&key_value.hole_tokens().to_string());
literal.push_str(&key_value.hole_tokens()?.to_string());
literal.push_str("}");
}

Expand Down
86 changes: 38 additions & 48 deletions macros/src/emit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,60 +100,50 @@ pub fn expand_tokens(opts: ExpandTokens) -> Result<TokenStream, syn::Error> {
check_evt_props(&props)?;
push_evt_props(&mut props, opts.level)?;

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().to_ref_tokens();

let rt_tokens = args.rt.to_tokens()?.to_ref_tokens();
let when_tokens = args
.when
.to_tokens()
.map(|when| when.to_ref_tokens())
.to_option_tokens(quote!(&emit::Empty));

let emit_tokens = if let Some(event_tokens) = args.evt {
// If the `event` parameter is present, then we can emit it without a template
let template_tokens = template
.map(|template| template.template_tokens().to_ref_tokens())
.to_option_tokens(quote!(&emit::Template));
let event_tokens = event_tokens.to_ref_tokens();

quote!(
emit::__private::__private_emit_event(
#rt_tokens,
#when_tokens,
#event_tokens,
#template_tokens,
#props_tokens,
);
)
} else {
let base_props_tokens = args.props.to_tokens().to_ref_tokens();
let extent_tokens = args.extent.to_tokens().to_ref_tokens();
let mdl_tokens = args.mdl.to_tokens().to_ref_tokens();

let template =
template.ok_or_else(|| syn::Error::new(span, "missing template string literal"))?;
let template_tokens = template.template_tokens().to_ref_tokens();

quote!(
emit::__private::__private_emit(
#rt_tokens,
#mdl_tokens,
#when_tokens,
#extent_tokens,
#template_tokens,
#base_props_tokens,
#props_tokens,
);
)
};

Ok(quote!({
match (#(#props_match_input_tokens),*) {
(#(#props_match_binding_tokens),*) => {
#emit_tokens
}
props.match_bound_props_tokens(|props_tokens| {
if let Some(event_tokens) = args.evt {
// If the `event` parameter is present, then we can emit it without a template
let template_tokens = template
.map(|template| template.template_tokens().to_ref_tokens())
.to_option_tokens(quote!(&emit::Template));
let event_tokens = event_tokens.to_ref_tokens();

Ok(quote!(
emit::__private::__private_emit_event(
#rt_tokens,
#when_tokens,
#event_tokens,
#template_tokens,
#props_tokens,
)
))
} else {
let base_props_tokens = args.props.to_tokens().to_ref_tokens();
let extent_tokens = args.extent.to_tokens().to_ref_tokens();
let mdl_tokens = args.mdl.to_tokens().to_ref_tokens();

let template =
template.ok_or_else(|| syn::Error::new(span, "missing template string literal"))?;
let template_tokens = template.template_tokens().to_ref_tokens();

Ok(quote!(
emit::__private::__private_emit(
#rt_tokens,
#mdl_tokens,
#when_tokens,
#extent_tokens,
#template_tokens,
#base_props_tokens,
#props_tokens,
)
))
}
}))
})
}
22 changes: 7 additions & 15 deletions macros/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub fn expand_tokens(opts: ExpandTokens) -> Result<TokenStream, syn::Error> {
{
use syn::{FieldValue, parse::Parse};

use crate::{args, template, util::ToRefTokens};
use crate::{args, template};

struct Args {}

Expand All @@ -40,21 +40,13 @@ pub fn expand_tokens(opts: ExpandTokens) -> Result<TokenStream, syn::Error> {
let template =
template.ok_or_else(|| syn::Error::new(span, "missing template string literal"))?;

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().to_ref_tokens();

let template_tokens = template.template_tokens();

Ok(quote!({
match (#(#props_match_input_tokens),*) {
(#(#props_match_binding_tokens),*) => {
emit::__private::__private_format(
#template_tokens,
#props_tokens,
)
}
}
}))
props.match_bound_props_tokens(|props_tokens| {
Ok(quote!(emit::__private::__private_format(
#template_tokens,
#props_tokens,
)))
})
}
}
Loading
Loading