Feature gate instrument imports on overwatch-derive.

This commit is contained in:
Alejandro Cabeza Romero 2024-12-19 16:25:16 +01:00
parent dd93596818
commit f8a0cc2681
No known key found for this signature in database
GPG Key ID: DA3D14AE478030FD
2 changed files with 11 additions and 5 deletions

View File

@ -21,6 +21,9 @@ exclude = [
[lib] [lib]
proc-macro = true proc-macro = true
[features]
instrumentation = []
[dependencies] [dependencies]
heck = "0.4" heck = "0.4"
syn = "1.0" syn = "1.0"

View File

@ -3,6 +3,8 @@ mod utils;
use proc_macro_error::{abort_call_site, proc_macro_error}; use proc_macro_error::{abort_call_site, proc_macro_error};
use quote::{format_ident, quote}; use quote::{format_ident, quote};
use syn::{punctuated::Punctuated, token::Comma, Data, DeriveInput, Field, Generics}; use syn::{punctuated::Punctuated, token::Comma, Data, DeriveInput, Field, Generics};
#[cfg(feature = "instrumentation")]
use tracing::instrument;
#[proc_macro_derive(Services)] #[proc_macro_derive(Services)]
#[proc_macro_error] #[proc_macro_error]
@ -185,7 +187,8 @@ fn generate_start_all_impl(fields: &Punctuated<Field, Comma>) -> proc_macro2::To
}); });
quote! { quote! {
#[::tracing::instrument(skip(self), err)]
#[cfg_attr(feature = "instrumentation", instrument(skip(self), err))]
fn start_all(&mut self) -> Result<::overwatch_rs::overwatch::ServicesLifeCycleHandle, ::overwatch_rs::overwatch::Error> { fn start_all(&mut self) -> Result<::overwatch_rs::overwatch::ServicesLifeCycleHandle, ::overwatch_rs::overwatch::Error> {
::std::result::Result::Ok([#( #call_start ),*].try_into()?) ::std::result::Result::Ok([#( #call_start ),*].try_into()?)
} }
@ -205,7 +208,7 @@ fn generate_start_impl(fields: &Punctuated<Field, Comma>) -> proc_macro2::TokenS
}); });
quote! { quote! {
#[::tracing::instrument(skip(self), err)] #[cfg_attr(feature = "instrumentation", instrument(skip(self), err))]
fn start(&mut self, service_id: ::overwatch_rs::services::ServiceId) -> Result<(), ::overwatch_rs::overwatch::Error> { fn start(&mut self, service_id: ::overwatch_rs::services::ServiceId) -> Result<(), ::overwatch_rs::overwatch::Error> {
match service_id { match service_id {
#( #cases ),* #( #cases ),*
@ -226,7 +229,7 @@ fn generate_stop_impl(fields: &Punctuated<Field, Comma>) -> proc_macro2::TokenSt
}); });
quote! { quote! {
#[::tracing::instrument(skip(self), err)] #[cfg_attr(feature = "instrumentation", instrument(skip(self), err))]
fn stop(&mut self, service_id: ::overwatch_rs::services::ServiceId) -> Result<(), ::overwatch_rs::overwatch::Error> { fn stop(&mut self, service_id: ::overwatch_rs::services::ServiceId) -> Result<(), ::overwatch_rs::overwatch::Error> {
match service_id { match service_id {
#( #cases ),* #( #cases ),*
@ -252,7 +255,7 @@ fn generate_request_relay_impl(fields: &Punctuated<Field, Comma>) -> proc_macro2
}); });
quote! { quote! {
#[::tracing::instrument(skip(self), err)] #[cfg_attr(feature = "instrumentation", instrument(skip(self), err))]
fn request_relay(&mut self, service_id: ::overwatch_rs::services::ServiceId) -> ::overwatch_rs::services::relay::RelayResult { fn request_relay(&mut self, service_id: ::overwatch_rs::services::ServiceId) -> ::overwatch_rs::services::relay::RelayResult {
{ {
match service_id { match service_id {
@ -282,7 +285,7 @@ fn generate_update_settings_impl(fields: &Punctuated<Field, Comma>) -> proc_macr
}); });
quote! { quote! {
#[::tracing::instrument(skip(self, settings), err)] #[cfg_attr(feature = "instrumentation", instrument(skip(self, settings), err))]
fn update_settings(&mut self, settings: Self::Settings) -> Result<(), ::overwatch_rs::overwatch::Error> { fn update_settings(&mut self, settings: Self::Settings) -> Result<(), ::overwatch_rs::overwatch::Error> {
let Self::Settings { let Self::Settings {
#( #fields_settings ),* #( #fields_settings ),*