3

I am trying to implement error - 404 metric counter where i only want to count the number of 404 requests occurring within my api. I am not seeing the count to go up even though i am trying to mock multiple 404 request to my api. Below is my metric class -

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ControllerAdvice;


@ControllerAdvice
public class MetricController {

    private MeterRegistry meterRegistry;


    @Autowired
    public MetricController(MeterRegistry meterRegistry){
        this.meterRegistry=meterRegistry;
    }


    private void countHttpStatus(HttpStatus status){
      Counter count=meterRegistry.counter(String.format("http.status.%d",status.value()));
      count.increment();

    }


    @Bean
    ApplicationRunner runner(){
        return args ->{
          countHttpStatus(HttpStatus.NOT_FOUND);


        };
    }


}

when i hit this end-point - http://localhost:8081/actuator/metrics/http.status.404

then i see below response -

{
    "name": "http.status.404",
    "description": null,
    "baseUnit": null,
    "measurements": [
        {
            "statistic": "COUNT",
            "value": 1
        }
    ],
    "availableTags": []
}

I want this count to go up if i am hitting my api with multiple 404's

Jon Abraham
  • 851
  • 3
  • 14
  • 27
  • What do you believe is causing you to increment the counter more than once? It seems to me that your counter will get incremented once when your app starts and then never be incremented again. – CryptoFool May 11 '19 at 18:30
  • That's correct but how do i invoke this multiple times if multiple 404's are found ? I feel like my class is simply incrementing without even detecting any 404's – Jon Abraham May 11 '19 at 19:41
  • You're asking how to use Spring. There are many many tutorials on this. It's really easy. Find a tutorial on building a RESTful web service with Spring . That will be 5-10 lines of code if you already have a Spring Boot app running. Then look into Spring HTTP error redirection, which will be just another few lines of code. Asking for more is asking for someone to teach you this basic stuff or do your research for you. – CryptoFool May 12 '19 at 00:03
  • 1
    Ok. I'll do some research for you... Here's the simplest example I could find for building a REST endpoint: https://adityasridhar.com/posts/how-to-create-simple-rest-apis-with-springboot - And here's a simple write-up on capturing REST errors: https://www.springboottutorial.com/spring-boot-exception-handling-for-rest-services – CryptoFool May 12 '19 at 00:13
  • As it is now, where do you expect 404's to be coming from? Do you have other code already? What you've shown so far has nothing to do with HTTP requests (well, except that you use the `HttpStatus.NOT_FOUND` constant), and hence can't produce 404s. – CryptoFool May 12 '19 at 00:19
  • @Steve i already have API up and running. The tutorial that u provided on capturing REST errors is very useful. I am going to try this and let u know – Jon Abraham May 13 '19 at 02:31
  • 1
    you don't have to create custom metric for it - it's already there provided by spring boot/micrometer - ```http_server_requests_seconds_count{status="404"}``` ( – hi_my_name_is May 13 '19 at 12:24
  • @freakman is that a prometheus query ? – Jon Abraham May 13 '19 at 13:28
  • Yes it is, but metric should be visible under http://localhost:8081/actuator/metrics/. Do have anything there? Spring expose a lot od metrics by default – hi_my_name_is May 13 '19 at 15:26
  • @freakman i don't see http_server_request_seconds however i do see tomcat.global.requests when i hit localhost:8081/actuator/metrics – Jon Abraham May 13 '19 at 17:06
  • Just an update folks - I was able to implement custom error metrics even though actuator provided everything that i needed. – Jon Abraham May 16 '19 at 13:53

1 Answers1

1

your code does the following:

  • create ApplicationRunner runner once (because is annotated as @Bean)
  • calls countHttpStatus method once (because is called from ApplicationRunner runner beans at creation time)
  • increments http.status.404 metric once (because is done from countHttpStatus method invoked once at ApplicationRunner runner @Bean creation time)
  • nothing else, this flow is no longer invoked so the value is 1 as you have
Andrea Ciccotta
  • 598
  • 6
  • 16