Spring Boot custom health indicator not showing up
Asked Answered
A

6

8

I believe followed almost all tutorials on the Internet and read many SO answers and still I'm stuck.

1. A simple health check

@Component
public class HealthCheck implements HealthIndicator {

    @Override
    public Health health() {
        return Health.up().build();
    }

}

2. Application.yaml is configured to show all details:

management.endpoints.web.exposure.include: "*"
management.endpoint.health.show-details: ALWAYS

3. Spring-boot-actuator included as dependency:

    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'org.springframework.boot:spring-boot-starter-web'

4. Spring boot of recent release:

id 'org.springframework.boot' version '2.2.0.RELEASE'

5. Main application class is annotated with @SpringBootApplication (which implicitly brings @ComponentScan).

@SpringBootApplication
public class PaymentServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(PaymentServiceApplication.class, args);
    }

}

My custom health check has to test for Apache Kafka, but I skipped the details for brevity. Still, invoking /actuator/health endpoint I get the same default result:

{
    "status": "UP",
    "components": {
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 250685575168,
                "free": 99168997376,
                "threshold": 10485760
            }
        },
        "ping": {
            "status": "UP"
        }
    }
}

Is there anything I may have missed?

Affluent answered 22/11, 2019 at 19:25 Comment(5)
i tried it and the health endpoint is used. Could you make a system.out. there and try it again... if there is something in the console.Sloat
Verify the component is actually configured as a spring bean. Just annotating as @Component does not guarantee it becomes a bean if there's no component scan for it.Chromoplast
@Compass, I've updated the question. The main class is annotated with @SpringBootApplication, which enables component scan.Affluent
Your HealthIndicator class doesn't add any extra information.Blowbyblow
add a name to your component like @Component("healtCheck").Transformism
A
3

I found the solution, but am not sure about the cause. The class was indeed not registered as bean, and surprisingly, adding explicitly base packages attribute helped:

@SpringBootApplication(scanBasePackages="com.example.*")

Interesting that there was no need to do the above in a different project.

Affluent answered 27/11, 2019 at 18:53 Comment(1)
The default ComponentScan will scan based on the Spring Boot Application's package path, and its subdirectories, but not anything above it hierarchically. Depending on your package arrangement this is a very likely cause, i.e. if you have a sub-application.Chromoplast
A
2

This is solved by adding this code:

management:
  endpoint:
    health:
      show-details: always

However, it creates another problem. When calling /actuator/health endpoint, you see all the details which is problematic for me. What I want to achieve is to see only statuses for both endpoints.

Anthroposophy answered 15/2, 2023 at 14:50 Comment(1)
you can expose only specific endpoints by adding property management.endpoints.web.exposure.include=endpoint1,endpoint2 where endpoint1 and endpoint2 contains a list of endpoints to expose with a commas between them. see hereAmaryllis
E
0

Interestingly the base package for the HealthIndicator is not recognized by the application container instead of declaring the class as @component Stereotype.

Fix for the issue- declare base package of HealthIndicator in Main Spring boot application class: @SpringBootApplication (scanBasePackages = "basepackage for your class HealthCheck") public class PaymentServiceApplication {

public static void main(String[] args) {
    SpringApplication.run(PaymentServiceApplication.class, args);
}

}

EDIT : NOTE- The above declaration of base package="" is not main spring boot class is not necessary.

You must stop the SERVER and then clean build the maven application and RESTART the server and re-run the application

You can now run find your custom health endpoint.

Eastman answered 15/9, 2020 at 18:41 Comment(0)
T
0

I encounter issue too, what I miss is

management:
  endpoint:
    health:
      show-details: always
Tweed answered 28/2, 2022 at 3:29 Comment(0)
S
0

Instead of using

@SpringBootApplication(scanBasePackages="com.example.*")

You can instead define your packages to scan

@ComponentScan(basePackages = {"com.example", "com.other"}

I will note I tested using scanBasepackages as described in the top answer and that doesn't seem to work if you also have the @ComponentScan defined.

Salesclerk answered 15/8, 2023 at 20:25 Comment(0)
C
0

If you have packages structure like:

com.example.health.MyHealthIndicator
com.example.myapp.MyApp (SpringApplication.run() is here)

then MyHealthIndicator will not be treated as component. In this case in MyApp you should add scanBasePackages

@SpringBootApplication(scanBasePackages="com.example.*")
public class MyApp { ... }

BUT if you put your component inside package where application is, then scanBasePackages is not needed:

com.example.myapp.health.MyHealthIndicator
com.example.myapp.MyApp

Spring will automatically find all components inside com.example.myapp.* packages. And it will not search outside of this package, unless you ask him to do so via scanBasePackages parameter.

Collimate answered 27/9, 2023 at 10:19 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.