问题
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