Skip to content

Latest commit

 

History

History
77 lines (67 loc) · 2.05 KB

actuator.adoc

File metadata and controls

77 lines (67 loc) · 2.05 KB

Actuator

  1. add maven dependency

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
  2. restart your app to check the following endpoints

  3. write your own metrics and health indicator

    @RestController
    class FlipFlopController implements HealthIndicator {
        private final AtomicLong requestCount = new AtomicLong();
        private final CounterService counterService;
    
        FlipFlopController(CounterService counterService) {
            this.counterService = counterService;
        }
    
        @Override
        public Health health() {
            long count = requestCount.get();
            return count % 2 == 1
                    ? Health.up().build()
                    : Health.down().withDetail("count", count).build();
        }
    
        @RequestMapping("/metered")
        String metered() {
            counterService.increment("metered.request.count");
            return "ok" + requestCount.incrementAndGet();
        }
    }
  4. now restart your app, and go to http://localhost:8080/health (depends on the login, admin can view more details than user). Read the reference when you have time (optional)

    admin view

    {
      "status": "DOWN",
      "flipFlopController": {
        "status": "DOWN",
        "count": 0
      },
      "diskSpace": {
        "status": "UP",
        "total": 499071844352,
        "free": 243076169728,
        "threshold": 10485760
      },
      "db": {
        "status": "UP",
        "database": "H2",
        "hello": 1
      }
    }
  5. hit the url http://localhost:8080/metered once and check the health endpoint again

  6. hit the url http://localhost:8080/metrics to search for the key metered.request.count

  7. (optional) Experiment with more endpoints