How to implement Micrometer metrics as a dynamic map
I have a use case where we want to capture metrics for a certain category of data. The different entries in the category is dynamic, and cannot be hardcoded. How would we define such metrics in Micrometer? I have worked with DistributionSummary, Gauge etc. but it takes measurement for only a single metric.
The example is like this: Suppose if there is a course website (like Udemy, Coursera) where we monitor details for which person attends a course. The table is like this:
| Name | Country | Course | Duration | Active | | ABC | USA | AWS 101 | 1.5 | true | | XYZ | IND | JPN 101 | 0.5 | false |
I want to plot a metric of number of people active per country to show it on CloudWatch or any other service. Something like:
Map<String,AtomicInteger> numberOfActiveUsersPerCountry; .... .... public void incrementActiveUsersPerCountry(String country) { if(numberOfActiveUsersPerCountry.containsKey(country) { //increment existing count } else { //add the new country }
This should be accessible via Micrometer and be accessible in Cloudwatch/ELK etc.