Search code examples
timerspring-boot-actuatormicrometer

springboot micrometer how to collect method execution time,not max time


use Timer as timer.record(), and get results like below

method_metrics_seconds_max{application="mydemo",class="com.demo.service.Impl.MetricsDemoService2Impl",method="getMethod1",} 0.21
method_metrics_seconds_count{application="mydemo",class="com.demo.service.Impl.MetricsDemoService2Impl",method="getMethod4",} 1.0
method_metrics_seconds_sum{application="mydemo",class="com.demo.service.Impl.MetricsDemoService2Impl",method="getMethod4",} 3.603

but I want to get the real execution time

I looked for a lot of information, but couldn't find


Solution

  • Metrics are basically aggregated datapoints in time. I think what you are asking is the raw data (not aggregated). Even though metric libraries will not give the raw data to you (because they are aggregating the data), you have a couple of options:

    1. You can use Percentile histograms (recommended) so that you can see the number of requests that for example fall between 100 and 200ms, you can also calculate percentiles from this on the backend side
    2. You can use Client-side percentiles (not recommended) so that you can see the percentile values
    3. Push the data into your logs (not recommended)
    4. Use Spring Cloud Sleuth which is our Distributed Tracing solution and it can record the duration of the separate HTTP requests
    5. You can use Micrometer's new Observation API and record the data however you please (will be recommended once it is released: around the end of 2022, also Spring Boot 3 will support this)