0

Below is my endpoint.

    @Path("/data")
    @POST
    @Produces(MediaType.APPLICATION_JSON)
    @Consumes(MediaType.APPLICATION_JSON)
    @Timed("rest.request.timer")
    public Multi<***> getData(***) {
        \\ app logic
    return x;
    }
    

Micrometer by default times the HTTP requests and http_server_requests_seconds_sum is giving the right value for how long it takes to respond to a request.

I have added custom metric on the method with @Timed annotation, I am expecting the value of this metric to be same as that of http_server_requests_seconds_sum , but they are not same.

I have also tried recording the time taken without annotation like below , still the values are not same. Any clue why they not same ?


Timer timer = Timer.builder("rest.request.timer").register(Metrics.globalRegistry);

public Uni<***> getData(* request) throws IOException 
{ 
   return this.timer.record(()-> {
            return serviceObject.get();
        });
 } 

    ```
Jaiprasad
  • 149
  • 11
  • @Timed with a Multi return type is not really going to work. What are you trying to measure? – geoand Oct 20 '22 at 16:06
  • Basically I want to measure the time taken to complete the request. I want to add a tag say "id" to metric to distinguish between the requests. – Jaiprasad Oct 21 '22 at 05:44
  • 1
    But a multi might not complete, it could be an infinite stream – geoand Oct 22 '22 at 06:07
  • Hi geo , I have modified the return type to Uni and trying to time the requests in the below way , still the metric not giving the correct value. public Uni<***> getData(* request) throws IOException { long start = Instant.now().toEpochMilli(); // app logic long end = Instant.now().toEpochMilli()-start; this.timer.record(end,TimeUnit.MILLISECONDS); return x; } – Jaiprasad Oct 26 '22 at 07:33
  • Metric is registered as Timer.builder("request.timer").tag("id","req1").register(Metrics.globalRegistry); – Jaiprasad Oct 26 '22 at 07:37
  • Please update the description of the issue with the new code as it is impossible to read code in comments – geoand Oct 27 '22 at 07:50
  • updated , pls look at the second block of code – Jaiprasad Oct 27 '22 at 09:03

1 Answers1

0

If I were you, I would do something like:

Timer timer = Timer.builder("rest.request.timer").register(Metrics.globalRegistry);

public Uni<***> getData(* request) throws IOException 
{ 
   long start = Instant.now().toEpochMilli(); 
   // app logic 
   return x.invoke(() -> {
       long end = Instant.now().toEpochMilli()-start; 
       this.timer.record(end,TimeUnit.MILLISECONDS);
   });
 } 
geoand
  • 60,071
  • 24
  • 172
  • 190
  • tried by passing the business logic into the timer to record the execution time, metric didnt give the right value , pls check the updated code block , is this the suggested approach ? – Jaiprasad Oct 27 '22 at 11:43