Search

Dark theme | Light theme

January 29, 2025

Helidon SE Helpings: Configure Disk Space Health Check

In Helidon SE you can enable a health check for the disk space usage. If the disk space usage is above a certain threshold then the health check will fail. To enable the disk space health check you need to add the dependency io.helidon.health:helidon-health-checks to your pom.xml file. The dependency contains three health checks: disk space usage, memory usage and dead lock detection. To configure the disk space health check you need to set the configuration property server.features.observe.observers.health.helidon.health.diskSpace.thresholdPercent to the threshold percentage. Or programmatically set the value in your application code. The default value is 99.999, which means that in real life the health check will not fail. You need to set a lower percentage in order to see the health check fail. For example when you set the value to 95.0 then the health check will fail when the disk space usage is above 95% or less than 5% of the disk space is available. You can also configure the path to check for disk space usage. The default path is the current working directory, but it can be set to another path. You need to set the configuration property server.features.observe.observers.health.helidon.health.diskSpace.path to change the path.

In the following example application you add a disk space usage health check. First you add the dependency to the pom.xml file:

<dependency>
  <groupId>io.helidon.health</groupId>
  <artifactId>helidon-health-checks</artifactId>
</dependency>

Next you configure the disk space health check. You need to enable the details view of the health endpoint. Next you set the threshold percentage and the path to check for disk space usage. If your application uses application.properties then you add the following lines to the configuration file:

...
# Enable detailed information for the health endpoint.
server.features.observe.observers.health.details=true

# Set threshold percentage for disk space usage.
# When the disk space usage is above this percentage, the health check will fail.
# Default is 99.999
server.features.observe.observers.health.helidon.health.diskSpace.thresholdPercent=95.0

# Configure path to check for disk space usage.
# Default path is "."
server.features.observe.observers.health.helidon.health.diskSpace.path=/mnt/data
...

In your application code you need to use the configuration to create the WebServer instance:

import io.helidon.webserver.WebServer;
import io.helidon.config.Config;
...
    Config config = Config.create();
    Config.global(config);

    WebServer server =
        WebServer.builder()
            ...
            // Read configuration properties.
            .config(config.get("server"))
            .build()
            .start();
...

The configuration can also be done in application code. In the following example you configure the disk space health check in the application code:

import io.helidon.health.checks.DiskSpaceHealthCheck;
import io.helidon.webserver.WebServer;
import io.helidon.webserver.observe.ObserveFeature;
import io.helidon.webserver.observe.health.HealthObserver;
...
    // Configure disk space usage health check.
    DiskSpaceHealthCheck diskSpaceHealthCheck =
        DiskSpaceHealthCheck.builder()
            // Set threshold percentage for disk space usage.
            // When the disk space usage is above this percentage, the health check will fail.
            // Default is 99.999
            .thresholdPercent(95.0)
            // Configure path to check for disk space usage.
            // Default path is "."
            .path("/mnt/data")
            .build();

    // Create observe feature with health check.
    HealthObserver healthObserver =
        HealthObserver.builder()
            // Show details in health endpoint.
            .details(true)
            // Disable auto-discovery of health checks.
            .useSystemServices(false)
            // Add disk space usage health check.
            .addCheck(diskSpaceHealthCheck)
            .build();

    WebServer server =
        WebServer.builder()
            .port(8080)
            // Add observe feature with health check.
            .addFeature(ObserveFeature.create(healthObserver))
            .build()
            .start();
...

When you access the health endpoint you see the disk space usage health check:

$ curl --silent -X GET --location "http://localhost:8080/observe/health" | jq .
{
  "status": "DOWN",
  "checks": [
    {
      "name": "diskSpace",
      "status": "DOWN",
      "data": {
        "free": "18.94 GB",
        "freeBytes": 20333867008,
        "percentFree": "4.07%",
        "total": "465.63 GB",
        "totalBytes": 499963174912
      }
    }
  ]
}

Written with Helidon SE 4.1.6.