From ed61ea9dd260946477c8f0ed2fe08c56f7fb5606 Mon Sep 17 00:00:00 2001 From: Trey Lowerison <19714082+tlowerison@users.noreply.github.com> Date: Fri, 1 Dec 2023 12:36:52 -0800 Subject: [PATCH] feat: add support for placing attributes on server functions (#2093) * fix: add support for placing attributes on server functions Adding instrumentation to server functions is not straightforward (requires calling out to another ssr-only function which is instrumented). This commit adds all attributes on the server function to both the generated front end and back end functions. If those attributes are only desirable on the backend say, a user can always wrap their attribute in `#[cfg_attr(feature = "ssr", ..)]`. * nit: formatting in example cargo --- examples/counter_isomorphic/Cargo.toml | 4 ++++ examples/counter_isomorphic/src/counters.rs | 17 +++++++++++++++++ server_fn_macro/src/lib.rs | 3 +++ 3 files changed, 24 insertions(+) diff --git a/examples/counter_isomorphic/Cargo.toml b/examples/counter_isomorphic/Cargo.toml index 1362ca1e7..6c6db15fa 100644 --- a/examples/counter_isomorphic/Cargo.toml +++ b/examples/counter_isomorphic/Cargo.toml @@ -24,9 +24,12 @@ leptos_actix = { path = "../../integrations/actix", optional = true } leptos_meta = { path = "../../meta" } leptos_router = { path = "../../router" } log = "0.4" +once_cell = "1.18" gloo-net = { git = "https://github.com/rustwasm/gloo" } wasm-bindgen = "0.2.87" serde = { version = "1", features = ["derive"] } +simple_logger = "4.3" +tracing = { version = "0.1", optional = true } [features] default = ["nightly"] @@ -34,6 +37,7 @@ hydrate = ["leptos/hydrate", "leptos_meta/hydrate", "leptos_router/hydrate"] ssr = [ "dep:actix-files", "dep:actix-web", + "dep:tracing", "leptos/ssr", "leptos_actix", "leptos_meta/ssr", diff --git a/examples/counter_isomorphic/src/counters.rs b/examples/counter_isomorphic/src/counters.rs index 0722ee0ea..7a0fde001 100644 --- a/examples/counter_isomorphic/src/counters.rs +++ b/examples/counter_isomorphic/src/counters.rs @@ -2,25 +2,38 @@ use cfg_if::cfg_if; use leptos::*; use leptos_meta::*; use leptos_router::*; +#[cfg(feature = "ssr")] +use tracing::instrument; cfg_if! { if #[cfg(feature = "ssr")] { use std::sync::atomic::{AtomicI32, Ordering}; use broadcaster::BroadcastChannel; + use once_cell::sync::OnceCell; + static COUNT: AtomicI32 = AtomicI32::new(0); lazy_static::lazy_static! { pub static ref COUNT_CHANNEL: BroadcastChannel = BroadcastChannel::new(); } + + static LOG_INIT: OnceCell<()> = OnceCell::new(); + fn init_logging() { + LOG_INIT.get_or_init(|| { + simple_logger::SimpleLogger::new().env().init().unwrap(); + }); + } } } #[server] +#[cfg_attr(feature = "ssr", instrument)] pub async fn get_server_count() -> Result { Ok(COUNT.load(Ordering::Relaxed)) } #[server] +#[cfg_attr(feature = "ssr", instrument)] pub async fn adjust_server_count( delta: i32, msg: String, @@ -33,6 +46,7 @@ pub async fn adjust_server_count( } #[server] +#[cfg_attr(feature = "ssr", instrument)] pub async fn clear_server_count() -> Result { COUNT.store(0, Ordering::Relaxed); _ = COUNT_CHANNEL.send(&0).await; @@ -40,6 +54,9 @@ pub async fn clear_server_count() -> Result { } #[component] pub fn Counters() -> impl IntoView { + #[cfg(feature = "ssr")] + init_logging(); + provide_meta_context(); view! { diff --git a/server_fn_macro/src/lib.rs b/server_fn_macro/src/lib.rs index a066d1736..2529a1ce8 100644 --- a/server_fn_macro/src/lib.rs +++ b/server_fn_macro/src/lib.rs @@ -89,6 +89,7 @@ pub fn server_macro_impl( let fn_name_as_str = body.ident.to_string(); let vis = body.vis; let block = body.block; + let attrs = body.attrs; let fields = body .inputs @@ -276,6 +277,7 @@ pub fn server_macro_impl( let func = if cfg!(feature = "ssr") { quote! { #docs + #(#attrs)* #vis async fn #fn_name(#(#fn_args),*) #output_arrow #return_ty { #block } @@ -283,6 +285,7 @@ pub fn server_macro_impl( } else { quote! { #docs + #(#attrs)* #[allow(unused_variables)] #vis async fn #fn_name(#(#fn_args_2),*) #output_arrow #return_ty { #server_fn_path::call_server_fn(