Health and Info
What is inside.
A single UP is nice, but what exactly is healthy? The database? The disk? And how do we add our own checks?
- What is healthy?
- Database, disk
- Our own checks
Health components
Show components lists every check, that makes up the health. Run it. The database is up. There's enough disk space. And the app is live, and ready for traffic. If any check is down, the overall status is down too.
management.endpoint.health.show-components=always{
"components":{
"db":{"status":"UP"},
"diskSpace":{"status":"UP"},
"livenessState":{"status":"UP"},
"ping":{"status":"UP"},
"readinessState":{"status":"UP"},
"ssl":{"status":"UP"}
},
"groups":["liveness","readiness"],
"status":"UP"
}Also uses BookController.java
@RestController
public class BookController {
@GetMapping("/books")
public String books() { return "Book list"; }
}Also uses _curl.sh
curl localhost:8080/actuator/healthHealth indicators
Each check is a Health Indicator bean. Spring Boot adds them for the database, the disk, and more. You can write your own, for anything your app depends on. Its name comes from the class name, without Health Indicator.
| key | value |
|---|---|
| Each check | a HealthIndicator bean |
| Built in | db, diskSpace, ping |
| Your own | implement health() |
| Name | BookStock from BookStockHealthIndicator |
Your own health check
This indicator counts the books, in the repository. With no books, the shop is down. Otherwise, it's up, with the count as a detail. Run it. Our check appears, under its own path. It's up, with eight books.
@Component
public class BookStockHealthIndicator implements HealthIndicator {
private final BookRepository books;
public BookStockHealthIndicator(BookRepository books) { this.books = books; }
@Override
public Health health() {
long count = books.count();
if (count == 0) {
return Health.down().withDetail("books", 0).build();
}
return Health.up().withDetail("books", count).build();
}
}{"details":{"books":8},"status":"UP"}Also uses Book.java
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
private String author;
private double price;
protected Book() {}
public Book(String title, String author, double price) {
this.title = title; this.author = author; this.price = price;
}
public Long getId() { return id; }
public String getTitle() { return title; }
public String getAuthor() { return author; }
public double getPrice() { return price; }
public void setPrice(double price) { this.price = price; }
@Override
public String toString() { return "Book " + id + ": " + title + " (" + price + ")"; }
}Also uses BookRepository.java
public interface BookRepository extends JpaRepository<Book, Long> {
}Also uses data.sql
INSERT INTO book (title, author, price) VALUES
('Dune', 'Frank Herbert', 9.99),
('The Hobbit', 'J.R.R. Tolkien', 8.99),
('Circe', 'Madeline Miller', 10.99),
('Children of Dune', 'Frank Herbert', 8.49),
('Song of Achilles', 'Madeline Miller', 9.49),
('The Fellowship of the Ring', 'J.R.R. Tolkien', 11.99),
('Emma', 'Jane Austen', 6.99),
('Persuasion', 'Jane Austen', 7.49);Also uses application.properties
spring.jpa.defer-datasource-initialization=true
management.endpoint.health.show-details=alwaysAlso uses _curl.sh
curl localhost:8080/actuator/health/bookStockDescribing the app
Info shows anything we like. Properties starting with info dot appear in the info endpoint. Run it. The name, version, and owner come back, as JSON. Monitoring tools show this, next to the health.
management.endpoints.web.exposure.include=health,info
management.info.env.enabled=true
info.app.name=Bookshop API
info.app.version=1.0.0
info.app.owner=Bookshop team{"app":{"name":"Bookshop API","version":"1.0.0","owner":"Bookshop team"}}Also uses BookController.java
@RestController
public class BookController {
@GetMapping("/books")
public String books() { return "Book list"; }
}Also uses _curl.sh
curl localhost:8080/actuator/infoSummary
Health is made of indicators, and you can add your own. Info describes your app, from info properties. Next, packaging the app, as a jar.
Key takeaways- show-components
- HealthIndicator
- info.app.*