Tomek Cejner
Tomek Cejner

Reputation: 1222

Spring Boot custom health indicator not showing up

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?

Upvotes: 9

Views: 7204

Answers (6)

Igor Dobrovolskyi
Igor Dobrovolskyi

Reputation: 11

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.

Upvotes: 0

Joseph.Chambers
Joseph.Chambers

Reputation: 41

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.

Upvotes: 0

Łukasz Bocheński
Łukasz Bocheński

Reputation: 31

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.

Upvotes: 2

lich0079
lich0079

Reputation: 121

I encounter issue too, what I miss is

management:
  endpoint:
    health:
      show-details: always

Upvotes: 0

Gaurav Swain
Gaurav Swain

Reputation: 1

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.

Upvotes: 0

Tomek Cejner
Tomek Cejner

Reputation: 1222

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.

Upvotes: 3

Related Questions