Skip to content

span_telemetry has hard-coded caller #2

Open
@alfiedotwtf

Description

@alfiedotwtf

Currently, the span_telemetry macro has "unknown" hard-coded for the caller's name as tracing::span!() requires name to be const. This requirement is because internally tracing::span!() is evaluating name in static context however coercing the call site outside of #[proc_macro_attribute] (like how #[tracing::instrument] is implemented) has been impossible (I've tried everything and then some).

This issue is to track the possibility of either tracing::span() relaxing their requirement for static context, or somehow get the following compiling with the same meaning:

macro_rules! error_telemetry {
    ($($arg:tt)*) => {{
        fn caller() -> String {
            let call_site = std::any::type_name_of_val(&(|| ()));
            let mut call_chain = call_site.rsplit("::");

            if let (Some(here), Some(_), Some(caller)) =
                (call_chain.next(), call_chain.next(), call_chain.next())
            {
                caller.to_string()
            } else {
                "unknown".to_string()
            }
        }

        tracing::span!(tracing::Level::ERROR, caller().as_str(), telemetry = true).in_scope(|| {
            tracing::error!($($arg)*)
        });
    }};
}

Since we already have the caller here, I even tried passing the caller's name to a #[proc_macro] which creates a literal string via a token stream and injects it into it's own call to tracing::span!(), but the compiler could still tell it wasn't const 🙃

Metadata

Metadata

Assignees

Labels

enhancementNew feature or request

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions