Search code examples
javaspring-bootprometheusgaugemicrometer

Custom metrics using Micrometer is not available in Prometheus endpoint


I'm using Micrometer in Java Spring Boot 1.5 application to scrape custom metrics for Prometheus.

I'm using "guageCollectionSize" method to count the number of entries in a HashMap. When I print the guage value, I'm getting the correct output as expected. But the problem is that I'm not able to view the custom metric in /prometheus endpoint.

Please find the code below

private Map<String, String> _strings;
private MeterRegistry registry;
_strings = new ConcurrentHashMap<String, String>();
_strings.put("Test","Test");
_strings.put("Test2","Test");
registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT);

registry.gaugeCollectionSize("strings_count", Tags.empty(),_strings.keySet());
System.out.println(registry.get("strings_count").gauge().value());

Can someone please help me to fix this issue?


Solution

  • As I told you on the GitHub issue which you created on the project repository, you're creating your own PrometheusMeterRegistry but you should use the auto-configured one.

    I created a sample that uses the auto-configured one and added a test for it.