1+ use darling:: util:: Flag ;
12use darling:: FromMeta ;
23use proc_macro:: TokenStream ;
34use proc_macro2:: Span ;
4- use quote:: { quote, ToTokens } ;
5+ use quote:: { format_ident , quote, ToTokens } ;
56use syn:: punctuated:: Punctuated ;
67use syn:: {
78 parse_macro_input, parse_quote, Attribute , ExprStruct , Ident , LitStr , Path , Token , Type ,
@@ -13,13 +14,15 @@ mod validation;
1314
1415#[ derive( FromMeta ) ]
1516struct MacroArgs {
17+ unprefixed : Flag ,
1618 #[ darling( default = "Self::default_crate_path" ) ]
1719 crate_path : Path ,
1820}
1921
2022impl Default for MacroArgs {
2123 fn default ( ) -> Self {
2224 Self {
25+ unprefixed : Flag :: default ( ) ,
2326 crate_path : Self :: default_crate_path ( ) ,
2427 }
2528 }
@@ -86,8 +89,11 @@ pub(crate) fn expand(args: TokenStream, item: TokenStream) -> TokenStream {
8689
8790fn expand_from_parsed ( args : MacroArgs , extern_ : Mod ) -> proc_macro2:: TokenStream {
8891 let MacroArgs {
92+ unprefixed,
8993 crate_path : foundations,
9094 } = & args;
95+ let with_service_prefix =
96+ format_ident ! ( "{}" , !unprefixed. is_present( ) , span = unprefixed. span( ) ) ;
9197
9298 let Mod {
9399 attrs : mod_attrs,
@@ -107,24 +113,26 @@ fn expand_from_parsed(args: MacroArgs, extern_: Mod) -> proc_macro2::TokenStream
107113 . iter ( )
108114 . filter_map ( |fn_| label_set_struct ( foundations, fn_) ) ;
109115
110- let registry_init = |var : & str , kind : & str | {
116+ let registry_init = |var : & str , opt : bool | {
111117 let var = Ident :: new ( var, Span :: call_site ( ) ) ;
112- let method = Ident :: new ( & format ! ( "get_{kind}_subsystem" ) , Span :: call_site ( ) ) ;
118+ let optional = format_ident ! ( "{opt}" ) ;
113119
114120 quote ! {
115- let #var = & mut * #foundations:: telemetry:: metrics:: internal:: Registries :: #method( stringify!( #mod_name) ) ;
121+ let #var = & mut * #foundations:: telemetry:: metrics:: internal:: Registries :: get_subsystem(
122+ stringify!( #mod_name) , #optional, #with_service_prefix
123+ ) ;
116124 }
117125 } ;
118126
119127 let init_registry = fns
120128 . iter ( )
121129 . any ( |fn_| !fn_. attrs . optional )
122- . then ( || registry_init ( "registry" , "main" ) ) ;
130+ . then ( || registry_init ( "registry" , false ) ) ;
123131
124132 let init_opt_registry = fns
125133 . iter ( )
126134 . any ( |fn_| fn_. attrs . optional )
127- . then ( || registry_init ( "opt_registry" , "opt" ) ) ;
135+ . then ( || registry_init ( "opt_registry" , true ) ) ;
128136
129137 let metric_inits = fns. iter ( ) . map ( |fn_| metric_init ( foundations, fn_) ) ;
130138
@@ -453,7 +461,7 @@ mod tests {
453461 #[ allow( non_upper_case_globals) ]
454462 static __oxy_Metrics: tarmac:: reexports_for_macros:: once_cell:: sync:: Lazy <__oxy_Metrics> =
455463 tarmac:: reexports_for_macros:: once_cell:: sync:: Lazy :: new( || {
456- let registry = & mut * tarmac:: telemetry:: metrics:: internal:: Registries :: get_main_subsystem ( stringify!( oxy) ) ;
464+ let registry = & mut * tarmac:: telemetry:: metrics:: internal:: Registries :: get_subsystem ( stringify!( oxy) , false , true ) ;
457465
458466 __oxy_Metrics {
459467 connections_total: {
@@ -510,9 +518,83 @@ mod tests {
510518 #[ allow( non_upper_case_globals) ]
511519 static __oxy_Metrics: :: foundations:: reexports_for_macros:: once_cell:: sync:: Lazy <__oxy_Metrics> =
512520 :: foundations:: reexports_for_macros:: once_cell:: sync:: Lazy :: new( || {
513- let opt_registry = & mut * :: foundations:: telemetry:: metrics:: internal:: Registries :: get_opt_subsystem( stringify!( oxy) ) ;
521+ let opt_registry = & mut * :: foundations:: telemetry:: metrics:: internal:: Registries :: get_subsystem( stringify!( oxy) , true , true ) ;
522+
523+ __oxy_Metrics {
524+ connections_total: {
525+ let metric = :: std:: default :: Default :: default ( ) ;
526+
527+ :: foundations:: reexports_for_macros:: prometheus_client:: registry:: Registry :: register(
528+ opt_registry,
529+ :: std:: stringify!( connections_total) ,
530+ str :: trim( " Total number of connections" ) ,
531+ :: std:: boxed:: Box :: new( :: std:: clone:: Clone :: clone( & metric) )
532+ ) ;
533+
534+ metric
535+ } ,
536+ }
537+ } ) ;
538+
539+ #[ doc = " Total number of connections" ]
540+ #[ must_use]
541+ pub ( crate ) fn connections_total( ) -> Counter {
542+ :: std:: clone:: Clone :: clone( & __oxy_Metrics. connections_total)
543+ }
544+ }
545+ } ;
546+
547+ assert_eq ! ( actual, expected) ;
548+ }
549+
550+ #[ test]
551+ fn expand_unprefixed_mixed ( ) {
552+ let attr = parse_attr ! {
553+ #[ metrics( unprefixed) ]
554+ } ;
555+
556+ let src = parse_quote ! {
557+ pub ( crate ) mod oxy {
558+ /// Total number of requests
559+ pub ( crate ) fn requests_total( ) -> Counter ;
560+
561+ /// Total number of connections
562+ #[ optional]
563+ pub ( crate ) fn connections_total( ) -> Counter ;
564+ }
565+ } ;
566+
567+ let actual = expand_from_parsed ( attr, src) . to_string ( ) ;
568+
569+ let expected = code_str ! {
570+ pub ( crate ) mod oxy {
571+ use super :: * ;
572+
573+ #[ allow( non_camel_case_types) ]
574+ struct __oxy_Metrics {
575+ requests_total: Counter ,
576+ connections_total: Counter ,
577+ }
578+
579+ #[ allow( non_upper_case_globals) ]
580+ static __oxy_Metrics: :: foundations:: reexports_for_macros:: once_cell:: sync:: Lazy <__oxy_Metrics> =
581+ :: foundations:: reexports_for_macros:: once_cell:: sync:: Lazy :: new( || {
582+ let registry = & mut * :: foundations:: telemetry:: metrics:: internal:: Registries :: get_subsystem( stringify!( oxy) , false , false ) ;
583+ let opt_registry = & mut * :: foundations:: telemetry:: metrics:: internal:: Registries :: get_subsystem( stringify!( oxy) , true , false ) ;
514584
515585 __oxy_Metrics {
586+ requests_total: {
587+ let metric = :: std:: default :: Default :: default ( ) ;
588+
589+ :: foundations:: reexports_for_macros:: prometheus_client:: registry:: Registry :: register(
590+ registry,
591+ :: std:: stringify!( requests_total) ,
592+ str :: trim( " Total number of requests" ) ,
593+ :: std:: boxed:: Box :: new( :: std:: clone:: Clone :: clone( & metric) )
594+ ) ;
595+
596+ metric
597+ } ,
516598 connections_total: {
517599 let metric = :: std:: default :: Default :: default ( ) ;
518600
@@ -528,6 +610,12 @@ mod tests {
528610 }
529611 } ) ;
530612
613+ #[ doc = " Total number of requests" ]
614+ #[ must_use]
615+ pub ( crate ) fn requests_total( ) -> Counter {
616+ :: std:: clone:: Clone :: clone( & __oxy_Metrics. requests_total)
617+ }
618+
531619 #[ doc = " Total number of connections" ]
532620 #[ must_use]
533621 pub ( crate ) fn connections_total( ) -> Counter {
@@ -596,7 +684,7 @@ mod tests {
596684 #[ allow( non_upper_case_globals) ]
597685 static __oxy_Metrics: :: foundations:: reexports_for_macros:: once_cell:: sync:: Lazy <__oxy_Metrics> =
598686 :: foundations:: reexports_for_macros:: once_cell:: sync:: Lazy :: new( || {
599- let registry = & mut * :: foundations:: telemetry:: metrics:: internal:: Registries :: get_main_subsystem ( stringify!( oxy) ) ;
687+ let registry = & mut * :: foundations:: telemetry:: metrics:: internal:: Registries :: get_subsystem ( stringify!( oxy) , false , true ) ;
600688
601689 __oxy_Metrics {
602690 connections_errors_total: {
@@ -691,7 +779,7 @@ mod tests {
691779 #[ allow( non_upper_case_globals) ]
692780 static __oxy_Metrics: :: foundations:: reexports_for_macros:: once_cell:: sync:: Lazy <__oxy_Metrics> =
693781 :: foundations:: reexports_for_macros:: once_cell:: sync:: Lazy :: new( || {
694- let registry = & mut * :: foundations:: telemetry:: metrics:: internal:: Registries :: get_main_subsystem ( stringify!( oxy) ) ;
782+ let registry = & mut * :: foundations:: telemetry:: metrics:: internal:: Registries :: get_subsystem ( stringify!( oxy) , false , true ) ;
695783
696784 __oxy_Metrics {
697785 connections_latency: {
0 commit comments