Skip to content

Commit

Permalink
Allow specifying struct_name when annotating a method (#171)
Browse files Browse the repository at this point in the history
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 <gerry@fiberplane.com>
Fixes: #139
  • Loading branch information
Archisman-Mridha and gagbo committed Feb 12, 2024
1 parent bbba7c1 commit f04ae4d
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
2 changes: 1 addition & 1 deletion autometrics-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub fn autometrics(
let item = parse_macro_input!(item as Item);

let result = match item {
Item::Function(item) => instrument_function(&args, item, None),
Item::Function(item) => instrument_function(&args, item, args.struct_name.as_deref()),
Item::Impl(item) => instrument_impl_block(&args, item, &async_trait),
};

Expand Down
11 changes: 10 additions & 1 deletion autometrics-macros/src/parse.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use syn::parse::{Parse, ParseStream};
use syn::{Expr, ItemFn, ItemImpl, Result, Token};
use syn::{Expr, ItemFn, ItemImpl, LitStr, Result, Token};

mod kw {
syn::custom_keyword!(track_concurrency);
Expand All @@ -8,6 +8,7 @@ mod kw {
syn::custom_keyword!(latency);
syn::custom_keyword!(ok_if);
syn::custom_keyword!(error_if);
syn::custom_keyword!(struct_name);
}

/// Autometrics can be applied to individual functions or to
Expand All @@ -32,6 +33,9 @@ pub(crate) struct AutometricsArgs {
pub ok_if: Option<Expr>,
pub error_if: Option<Expr>,
pub objective: Option<Expr>,

// Fix for https://github.com/autometrics-dev/autometrics-rs/issues/139.
pub struct_name: Option<String>,
}

impl Parse for AutometricsArgs {
Expand Down Expand Up @@ -67,6 +71,11 @@ impl Parse for AutometricsArgs {
return Err(input.error("expected only a single `objective` argument"));
}
args.objective = Some(input.parse()?);
} else if lookahead.peek(kw::struct_name) {
let _ = input.parse::<kw::struct_name>()?;
let _ = input.parse::<Token![=]>()?;
let struct_name = input.parse::<LitStr>()?.value();
args.struct_name = Some(struct_name);
} else if lookahead.peek(Token![,]) {
let _ = input.parse::<Token![,]>()?;
} else {
Expand Down
23 changes: 23 additions & 0 deletions autometrics/tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,29 @@ fn impl_block() {
&& line.ends_with("} 1")));
}

#[test]
fn struct_name_autometrics_macro_attribute() {
prometheus_exporter::try_init().ok();

struct Bar;

impl Bar {
#[autometrics(struct_name = "Bar")]
fn test_fn() -> &'static str {
"Hello world!"
}
}

Bar::test_fn();

let metrics = prometheus_exporter::encode_to_string().unwrap();
assert!(metrics.lines().any(|line| {
line.starts_with("function_calls_total{")
&& line.contains(r#"function="Bar::test_fn""#)
&& line.ends_with("} 1")
}));
}

#[test]
fn result() {
prometheus_exporter::try_init().ok();
Expand Down

0 comments on commit f04ae4d

Please sign in to comment.