Skip to content

Commit f04ae4d

Browse files
Allow specifying struct_name when annotating a method (#171)
Specifying the struct name is useful for cases where the autometrics annotation isn’t used on a complete impl block --------- Co-authored-by: Gerry Agbobada <[email protected]> Fixes: #139
1 parent bbba7c1 commit f04ae4d

File tree

3 files changed

+34
-2
lines changed

3 files changed

+34
-2
lines changed

autometrics-macros/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ pub fn autometrics(
2929
let item = parse_macro_input!(item as Item);
3030

3131
let result = match item {
32-
Item::Function(item) => instrument_function(&args, item, None),
32+
Item::Function(item) => instrument_function(&args, item, args.struct_name.as_deref()),
3333
Item::Impl(item) => instrument_impl_block(&args, item, &async_trait),
3434
};
3535

autometrics-macros/src/parse.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use syn::parse::{Parse, ParseStream};
2-
use syn::{Expr, ItemFn, ItemImpl, Result, Token};
2+
use syn::{Expr, ItemFn, ItemImpl, LitStr, Result, Token};
33

44
mod kw {
55
syn::custom_keyword!(track_concurrency);
@@ -8,6 +8,7 @@ mod kw {
88
syn::custom_keyword!(latency);
99
syn::custom_keyword!(ok_if);
1010
syn::custom_keyword!(error_if);
11+
syn::custom_keyword!(struct_name);
1112
}
1213

1314
/// Autometrics can be applied to individual functions or to
@@ -32,6 +33,9 @@ pub(crate) struct AutometricsArgs {
3233
pub ok_if: Option<Expr>,
3334
pub error_if: Option<Expr>,
3435
pub objective: Option<Expr>,
36+
37+
// Fix for https://github.com/autometrics-dev/autometrics-rs/issues/139.
38+
pub struct_name: Option<String>,
3539
}
3640

3741
impl Parse for AutometricsArgs {
@@ -67,6 +71,11 @@ impl Parse for AutometricsArgs {
6771
return Err(input.error("expected only a single `objective` argument"));
6872
}
6973
args.objective = Some(input.parse()?);
74+
} else if lookahead.peek(kw::struct_name) {
75+
let _ = input.parse::<kw::struct_name>()?;
76+
let _ = input.parse::<Token![=]>()?;
77+
let struct_name = input.parse::<LitStr>()?.value();
78+
args.struct_name = Some(struct_name);
7079
} else if lookahead.peek(Token![,]) {
7180
let _ = input.parse::<Token![,]>()?;
7281
} else {

autometrics/tests/integration_test.rs

+23
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,29 @@ fn impl_block() {
7171
&& line.ends_with("} 1")));
7272
}
7373

74+
#[test]
75+
fn struct_name_autometrics_macro_attribute() {
76+
prometheus_exporter::try_init().ok();
77+
78+
struct Bar;
79+
80+
impl Bar {
81+
#[autometrics(struct_name = "Bar")]
82+
fn test_fn() -> &'static str {
83+
"Hello world!"
84+
}
85+
}
86+
87+
Bar::test_fn();
88+
89+
let metrics = prometheus_exporter::encode_to_string().unwrap();
90+
assert!(metrics.lines().any(|line| {
91+
line.starts_with("function_calls_total{")
92+
&& line.contains(r#"function="Bar::test_fn""#)
93+
&& line.ends_with("} 1")
94+
}));
95+
}
96+
7497
#[test]
7598
fn result() {
7699
prometheus_exporter::try_init().ok();

0 commit comments

Comments
 (0)