Adding custom Micrometer meters

In reactive streams, we may easily add a custom monitoring logic even without built-in support. For example, the following code explains how to add an invocation counter for a WebClient that does not have any default instrumentation:

WebClient.create(serviceUri)                                         // (1)
         .get()
         .exchange()
         .flatMap(cr -> cr.toEntity(User.class))                     // (2)
         .doOnTerminate(() -> registry
               .counter("user.request", "uri", serviceUri)           // (3)
               .increment())

Here, we create a new WebClient for a target serviceUri (1), make a request and deserialize the response to a User entity (2). When the operation is terminated, we manually increase the counter with a custom tag uri where the value represents serviceUri (3).

Similarly, we may combine stream hooks (such as .doOnNext(), .doOnComplete(), .doOnSubscribe(), or .doOnTerminate()) with different types of Micrometer meters (such as .counter(), .timer(), or .gauge()) to measure the desired operational characteristics of reactive streams with a sufficient level of detail provided by tags that represent different dimensions.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.227.10.45