Mahima Tiwary
Mahima Tiwary

Reputation: 383

Track the number of requests per client per api in Spring Boot using Micrometer

I am using AWS Cloudwatch to monitor my application. I want to track the number of requests I receive per client per API. I have micrometer configured for other metrics. How do I track this metric using micrometer?

For reference, I have a finite set of clients using my service. I can make a manual Counter for each of them. But is there a way to automate this since I don't want to manually create a counter every time a new client is added. Or is there any other way of doing this?

Upvotes: 1

Views: 1034

Answers (1)

Jonatan Ivanov
Jonatan Ivanov

Reputation: 6863

You don't need to create extra Counters since Spring Boot already creates a Timer for your requests (you should see http.server.requests in CloudWatch). A Timer always contains a count, the rule of thumb is Never Count something that you can Time.

Spring Boot already tags your Timers with the API path so the problem is how to tag every timer with the client information. For this Spring Boot offers you, WebMvcTagsContributor and WebFluxTagsContributor for MVC and for WebFlux app. You implement one of these interfaces (depending if you use MVC of WebFlux) create a @Bean from it and Spring Boot will auto-configure it for you so that your tags will be enhanced with whatever data you want.

Implementation tip: try to avoid adding raw user input to your tags because that can lead to potentially having hight cardinality data. Try to map your user input to distinct values and fall back to invalid/unknown/etc. if you get a non matching one.

Upvotes: 1

Related Questions