Actuator enablement (Spring Boot)

1. Reference

Production ready endpoints > https://docs.spring.io/spring-boot/docs/current/reference/html/production-ready-endpoints.html

Spring Boot Actuator > https://www.baeldung.com/spring-boot-actuators

Actuator in Spring Boot 2.0 > https://dzone.com/articles/spring-boot-actuator-in-spring-boot-20

2. Introduction

This article will enable Spring Boot actuators for tests and production.

3. Configuration

Add dependency to pom.xml:

   <dependency>

       <!-- Spring Boot actuator enablement -->

       <groupId>org.springframework.boot</groupId>

       <artifactId>spring-boot-starter-actuator</artifactId>

  </dependency>

Enable actuators for production [/src/main/resources/application.properties] and/or test [/src/test/resources/application.properties]:

management.endpoint.env.show-values=always

management.endpoints.web.base-path=/actuator

management.endpoints.web.exposure.exclude=shutdown

management.endpoints.web.exposure.include=*

4. Access actuators

http://localhost:8080/actuator/

{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"caches":{"href":"http://localhost:8080/actuator/caches","templated":false},"caches-cache":{"href":"http://localhost:8080/actuator/caches/{cache}","templated":true},"health":{"href":"http://localhost:8080/actuator/health","templated":false},"health-component-instance":{"href":"http://localhost:8080/actuator/health/{component}/{instance}","templated":true},"health-component":{"href":"http://localhost:8080/actuator/health/{component}","templated":true},"info":{"href":"http://localhost:8080/actuator/info","templated":false},"liquibase":{"href":"http://localhost:8080/actuator/liquibase","templated":false},"loggers-name":{"href":"http://localhost:8080/actuator/loggers/{name}","templated":true},"loggers":{"href":"http://localhost:8080/actuator/loggers","templated":false}}}


5. Actuator: loggers

5.1. Log level change in runtime

By default, log level is INFO. Example of changing it to TRACE for logger 'edu.cou.myapp':

curl -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel": "TRACE"}' http://myapp.cloudapps.cou.edu:80/actuator/loggers/edu.cou.myapp

Note: If needed, credentials can be provided w/ cURL option:  --user {username}:{password}


5.2.  JPA Hibernate query execution time

a) By default, log level for 'org.hibernate.stat' is INFO. For logging query execution time, log level must be at least DEBUG:

curl --user {username}:{password} -i -X POST -H 'Content-Type: application/json' -d '{"configuredLevel": "DEBUG"}' http://myapp.cloudapps.cou.edu:80/actuator/loggers/org.hibernate.stat


b) Search in log for entries similar to the following:

2019-07-04 10:27:27.739 DEBUG 11797 --- [nio-8080-exec-3] o.h.stat.internal.StatisticsImpl         : HHH000117: HQL: select count(*) from AppRequest x WHERE x.uuid = :uuid, time: 1ms, rows: 1