Search code examples
spring-bootspring-boot-actuatorcodahale-metrics

how to make /metrics endpoint show the metrics Sorted?


I have a Spring Boot 1.3.1 project. Using actuator, so I have /metrics endpoint too.

I am using codahale to add a metric per some of my methods, and the full java classname is the name of the metric. That is all ok.

But the metrics there show unsorted, it's very inadequate, having all sorted would be much better (max, min, percentiles etc for a given metric will be in a single block, not all over the place as now).

Anybody know how to fix this?


Solution

  • I finally found a solution to this problem. I add a new Endpoint that call the MetricsEndpoint and sort the result.

    @Component
    public class CustomMetricsEndpoint extends AbstractEndpoint<Map<String, Object>> {
    
        private MetricsEndpoint metricsEndpoint;
    
        @Autowired
        public CustomMetricsEndpoint(MetricsEndpoint metricsEndpoint) {
            super("custommetrics");
            this.metricsEndpoint = metricsEndpoint;
        }
    
        public Map<String, Object> invoke() {
            SortedMap<String, Object> metricsMap =
                    new TreeMap<String, Object>(this.metricsEndpoint.invoke());
            return metricsMap;
        }
    
    }
    

    Now, when I call /custommetrics, the result is sorted by keys.