如何将Micrometer计时器与Webflux端点一起使用

如何将Micrometer计时器与Webflux端点一起使用

本文介绍了如何将Micrometer计时器与Webflux端点一起使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有简单的方法可以将千分尺计时器与Webflux控制器一起使用?

IS there any simple way to use Micrometer timers with Webflux controllers?

@Timed似乎仅适用于非反应性方法.对于反应式,它记录的时间值非常低.

It seems that @Timed works only with non-reactive methods. For reactive it records very low time values.

我发现了一个类似的问题:,但是对于这样一个常见问题,答案太复杂了

I found a similar question: How to use Micrometer Timer to record duration of async method (returns Mono or Flux) but the answers were too complex for such a common issue

有什么想法吗?

推荐答案

如果您想测量Web-flux方法/调用的时间,则可以直接从Flux/Mono轻松使用指标(此外,还可以将项目配置为导出指标,例如用于石墨)

If you want to measure time for Web-flux methods/calls you can then use easily metrics directly from Flux/Mono (plus configure your project to export metrics, e.g. for graphite)

一个例子如下

Flux<String> dataStream = Flux.just("AA", "BB", "CC", "DD");
dataStream.name("my-test-name").tag("key1", "value1").metrics().subscribe(p ->
   {
      System.out.println("Hello " + p);
      try {
         Thread.sleep(1000);
      } catch (InterruptedException e) {
         e.printStackTrace();
   }
});

然后在我的情况下,石墨中的度量标准位于应用程序名称.magdalena下. reactor.flow.duration.exception.flow.my-test-name.status.completed.type.Flux.p50 p50-是一半请求的等待时间(或98%pf的请求的p98等待时间等).感谢他们在这个示例中的人为延迟,您可以观察到它们的值接近4000(处理1s x 4个元素).

Then in my case the metrics in the graphite are e.g.under application-name.magdalena.reactor.flow.duration.exception.flow.my-test-name.status.completed.type.Flux.p50p50 - is the latency for the half of the requests (or p98 latency for 98% pf the requestsetc.). Thanks they the artificial delay in this example you can observe that they values is near 4000 (1s x 4 elements processed).

application.yml中的石墨配置:

Configuration in application.yml for graphite:

management:
    metrics:
        export:
            graphite:
                enabled: true
                host: graphite-lhr10.something.com
                port: 2003
                protocol: plaintext

@Timed注释对我也无效.

@Timed annotation did not worked fro me too.

这篇关于如何将Micrometer计时器与Webflux端点一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 12:02