How to use Micrometer timer together with webflux endpoints

限于喜欢 提交于 2019-12-11 15:49:09

问题


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

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

Any ideas?


回答1:


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)

An example looks like

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();
   }
});

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.p50 p50 - 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).

Configuration in application.yml for graphite:

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

@Timed annotation did not worked fro me too.



来源:https://stackoverflow.com/questions/52932685/how-to-use-micrometer-timer-together-with-webflux-endpoints

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!