robynico
robynico

Reputation: 236

Spring Boot Micrometer : timing abstract controller with dynamic metric name

We have an abstract generic controller for commons methods :

public abstract class GenericController<T> {

    @Timed(value = "generic_controller_get_one", description = "Time taken to getOne")
    public T getOne(ID id) {
    }

    @Timed(value = "generic_controller_search", description = "Time taken to search")
    public Page<T> search (int page, int size) {
    }
}

An then implemented controllers :

@RestController
public class ModelAController extends GenericController<ModelA> {

}
@RestController
public class ModelBController extends GenericController<ModelB> {

}

Question :

Is there a way to time implemented controller with a dynamic metric name like modela_controller_search ?

Currently all controllers are timed with generic_controller_* metric name.

Upvotes: 1

Views: 800

Answers (1)

Jonatan Ivanov
Jonatan Ivanov

Reputation: 6863

You can't really put dynamic things into annotations since based on the Java specs, annotation parameters needs to be constant. I don't think there is support for this in Boot, but I think there are two-three ways to make this work.

  1. Spring Boot does this for you automatically, check the http.server.request metric, it will contain the uri that will separate your controller endpoints.

  2. Instead of renaming the metric, you can add a tag that will separate your controllers, take a look at WebMvcTagsContributor (also WebMvcTagsProvider).

  3. If you really want to rename the metric, take a look at Micrometer's MeterFilter first. It lets you rename meters and add/remove/modify tags. You can write a MeterFilter that renames all of the metrics that's name startsWith generic_controller_*. You can use the tags that are currently there for the naming but you can also use the WebMvcTagsContributor to add new tags as needed.

I'm not recommending separating the controllers in the metric name but I recommend doing it in the tags. Separating controllers in the name will make it harder to aggregate metrics across your controllers, e.g.: show me all of the errors for all of my controllers or show me all of the errors for all of the GET calls, etc.

Upvotes: 1

Related Questions