Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to include Method Parameters in @Timed annotation used on arbitrary method name

In my application, I have a use case where I have to monitor a method by the argument value it is supplied. I have to expose the metrics to Prometheus endpoint. However, the function is a common function and is used by many different classes. I am trying to get the value passed in the method parameter to @Timed, so as to distinguish between different behaviors this function would exhibit based on the parameter value passed.

I tried using @Timed annotation but could not get the @Timed annotation expose the function parameter as a metric to Prometheus.

@Timed("getFooContent")
public void getFooContent(Arg1 arg1, Arg2 arg2) {
    //some code.... 
}
like image 656
user1184527 Avatar asked Sep 01 '25 03:09

user1184527


1 Answers

There isn't a way to include the parameters in the timer's tags using just the annotation. Micrometer provides the annotation for simple use cases, and recommends using the programmatic approach when you need something more complex.

You should use the record method on the timer and wrap your code in that.

registry.timer("myclass.getFooContent", Tags.of("arg1", arg1)).record(() -> {
  //some code...
})
like image 173
checketts Avatar answered Sep 03 '25 20:09

checketts