使用Timer
作为timer.record()
,并得到如下结果
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
但我想获得真实的执行时间
查了很多资料,没找到
最佳答案
指标基本上是及时聚合的数据点。我认为您要问的是原始数据(未聚合)。 尽管度量库不会向您提供原始数据(因为它们正在聚合数据),但您有几个选择:
- 您可以使用Percentile histograms (推荐)这样您就可以看到例如 100 到 200 毫秒之间的请求数,您还可以在后端计算百分位数
- 您可以使用Client-side percentiles (不推荐)以便您可以看到百分位数
- 将数据推送到日志中(不推荐)
- 使用Spring Cloud Sleuth这是我们的分布式跟踪解决方案,它可以记录单独的 HTTP 请求的持续时间
- 您可以使用 Micrometer 的新 Observation API 并随心所欲地记录数据(发布后将推荐:2022 年底左右,Spring Boot 3 也将支持此功能)
关于timer - Spring Boot Micrometer如何收集方法执行时间,而不是最大时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72300896/