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

Commit

Permalink
various updates and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed Jan 18, 2024
1 parent cedf7fe commit 3fa1fd7
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 33 deletions.
4 changes: 2 additions & 2 deletions core/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ impl<'a, T: Emitter + ?Sized> Emitter for &'a T {
}
}

#[cfg(feature = "std")]
impl<'a, T: Emitter + ?Sized + 'a> Emitter for Box<T> {
#[cfg(feature = "alloc")]
impl<'a, T: Emitter + ?Sized + 'a> Emitter for alloc::boxed::Box<T> {
fn emit<P: Props>(&self, evt: &Event<P>) {
(**self).emit(evt)
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ impl<'a, F: Filter + ?Sized> Filter for &'a F {
}
}

#[cfg(feature = "std")]
impl<'a, F: Filter + ?Sized + 'a> Filter for Box<F> {
#[cfg(feature = "alloc")]
impl<'a, F: Filter + ?Sized + 'a> Filter for alloc::boxed::Box<F> {
fn matches<P: Props>(&self, evt: &Event<P>) -> bool {
(**self).matches(evt)
}
Expand Down
3 changes: 3 additions & 0 deletions core/src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,9 @@ impl<'a, W: Write + ?Sized> Write for &'a mut W {
}
}

#[cfg(feature = "alloc")]
impl Write for alloc::string::String {}

impl<'a> Write for fmt::Formatter<'a> {
fn write_hole_value(&mut self, _: &str, value: Value) -> fmt::Result {
fmt::Display::fmt(&value, self)
Expand Down
45 changes: 45 additions & 0 deletions macros/src/format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
use proc_macro2::TokenStream;
use syn::{parse::Parse, FieldValue};

use crate::{
args::{self},
template,
};

pub struct ExpandTokens {
pub input: TokenStream,
}

struct Args {}

impl Parse for Args {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
args::set_from_field_values(
input.parse_terminated(FieldValue::parse, Token![,])?.iter(),
[],
)?;

Ok(Args {})
}
}

pub fn expand_tokens(opts: ExpandTokens) -> Result<TokenStream, syn::Error> {
let (_, template, props) = template::parse2::<Args>(opts.input, true)?;

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

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,
)
}
}
}))
}
24 changes: 12 additions & 12 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod capture;
mod emit;
mod filter;
mod fmt;
mod format;
mod hook;
mod in_ctxt;
mod key;
Expand All @@ -30,6 +31,17 @@ mod util;

use util::ResultToTokens;

/**
Format a template.
*/
#[proc_macro]
pub fn format(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
format::expand_tokens(format::ExpandTokens {
input: TokenStream::from(item),
})
.unwrap_or_compile_error()
}

/**
Emit a debug record.
*/
Expand Down Expand Up @@ -62,18 +74,6 @@ pub fn error(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
emit(quote!(Error), TokenStream::from(item))
}

/**
Format a template.
*/
#[proc_macro]
pub fn format(item: proc_macro::TokenStream) -> proc_macro::TokenStream {
emit::expand_tokens(emit::ExpandTokens {
level: quote!(default()),
input: TokenStream::from(item),
})
.unwrap_or_compile_error()
}

/**
Construct a set of properties.
*/
Expand Down
8 changes: 8 additions & 0 deletions src/macro_hooks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,14 @@ impl<'a> __PrivateKeyHook for Str<'a> {
}
}

#[track_caller]
pub fn __private_format(tpl: Template, props: impl Props) -> String {
let mut s = String::new();
tpl.render(props).write(&mut s).expect("infallible write");

s
}

#[track_caller]
pub fn __private_emit(
to: impl Emitter,
Expand Down
9 changes: 4 additions & 5 deletions targets/otlp/src/data/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ mod export_logs_service;
mod log_record;

use emit_batcher::BatchError;
use prost::Message;

pub use self::{export_logs_service::*, log_record::*};

Expand Down Expand Up @@ -63,6 +62,8 @@ pub(crate) fn encode_request(
}

pub(crate) fn decode_response(body: Result<&[u8], &[u8]>) {
use prost::Message;

match body {
Ok(body) => {
let response =
Expand All @@ -75,10 +76,8 @@ pub(crate) fn decode_response(body: Result<&[u8], &[u8]>) {
}
Err(body) => {
let response =
crate::data::generated::collector::logs::v1::ExportLogsServiceResponse::decode(
body,
)
.unwrap();
crate::data::generated::collector::logs::v1::ExportLogsPartialSuccess::decode(body)
.unwrap();

emit::warn!(rt: emit::runtime::INTERNAL.get(), "received {#[emit::as_debug] response}");
}
Expand Down
23 changes: 22 additions & 1 deletion targets/otlp/src/data/traces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,5 +66,26 @@ pub(crate) fn encode_request(
}

pub(crate) fn decode_response(body: Result<&[u8], &[u8]>) {
println!("body: {:?}", body);
use prost::Message;

match body {
Ok(body) => {
let response =
crate::data::generated::collector::trace::v1::ExportTraceServiceResponse::decode(
body,
)
.unwrap();

emit::debug!(rt: emit::runtime::INTERNAL.get(), "received {#[emit::as_debug] response}");
}
Err(body) => {
let response =
crate::data::generated::collector::trace::v1::ExportTracePartialSuccess::decode(
body,
)
.unwrap();

emit::warn!(rt: emit::runtime::INTERNAL.get(), "received {#[emit::as_debug] response}");
}
}
}
18 changes: 10 additions & 8 deletions targets/otlp/src/data/traces/span.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,14 @@ impl<P: emit::props::Props> sval::Value for PropsSpanAttributes<P> {
name: "exception",
time_unix_nano: self.time_unix_nano,
dropped_attributes_count: 0,
attributes: &[KeyValue {
key: "exception.message",
value: AnyValue::<_, (), (), ()>::String(sval::Display::new_borrowed(
&err,
)),
}],
attributes: &InlineEventAttributes {
attributes: &[KeyValue {
key: "exception.message",
value: AnyValue::<_, (), (), ()>::String(
sval::Display::new_borrowed(&err),
),
}],
},
}])
},
)?;
Expand Down Expand Up @@ -236,9 +238,9 @@ const EVENT_ATTRIBUTES_LABEL: sval::Label =
const EVENT_ATTRIBUTES_INDEX: sval::Index = sval::Index::new(3);

#[derive(Value)]
pub struct InlineEventAttributes<'a> {
pub struct InlineEventAttributes<'a, A: ?Sized = [KeyValue<&'a str, &'a AnyValue<'a>>]> {
#[sval(label = EVENT_ATTRIBUTES_LABEL, index = EVENT_ATTRIBUTES_INDEX)]
pub attributes: &'a [KeyValue<&'a str, &'a AnyValue<'a>>],
pub attributes: &'a A,
}

pub struct PropsEventAttributes<P>(pub P);
Expand Down
2 changes: 1 addition & 1 deletion targets/term/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use core::{fmt, str, time::Duration};
use std::{cell::RefCell, cmp, io::Write, sync::Mutex};

use emit::{
well_known::{METRIC_KIND_SUM, METRIC_VALUE_KEY, TRACE_ID_KEY, LOCATION_KEY},
well_known::{LOCATION_KEY, METRIC_KIND_SUM, METRIC_VALUE_KEY, TRACE_ID_KEY},
Event,
};
use emit_metrics::{Bucketing, MetricsCollector};
Expand Down
6 changes: 4 additions & 2 deletions tests/smoke-test/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,18 @@ extern crate serde_derive;

#[tokio::main]
async fn main() {
println!("{}", emit::format!("Hello, {x}", x: "world"));

let emitter = emit::setup()
.to(emit_otlp::proto()
.logs(
emit_otlp::logs_http("http://localhost:4318/v1/logs")
.body(|evt, f| write!(f, "{}", evt.tpl().braced())),
)
/*.traces(
.traces(
emit_otlp::traces_http("http://localhost:4318/v1/traces")
.name(|evt, f| write!(f, "{}", evt.tpl().braced())),
)*/
)
.resource(emit::props! {
#[emit::key("service.name")]
service_name: "smoke-test-rs",
Expand Down

0 comments on commit 3fa1fd7

Please sign in to comment.