Grails Custom Health Indicators for Spring Boot Actuator

Every Grails application that runs in production eventually gets asked the same question: is this service healthy enough to keep serving traffic? Spring Boot Actuator answers it with a single aggregated status at /actuator/health, and a Grails app inherits that behaviour as soon as the actuator starter sits on the classpath. The default payload covers the database connection, free disk space and a basic ping, which is useful, but it says very little about the integrations that make an application worth paying for.

Custom health indicators close that gap. They let you express business-critical dependencies as first-class checks, so a stalled payment gateway or a clogged message queue surfaces in the same response your load balancer and container orchestrator already poll. The walkthrough below builds one from scratch in Groovy, registers it the Grails way, locks down the endpoint, and covers the operational details that catch teams out when they deploy across Australian regions.

What the actuator health endpoint reports

The actuator aggregates every registered health contributor and reports the most severe status it finds. A component can be UP, DOWN, OUT_OF_SERVICE or UNKNOWN, and the severity order is configurable. The endpoint answers with HTTP 200 while everything is healthy and switches to 503 once a component is DOWN, which is the signal an Elastic Load Balancer in front of a Grails instance in Sydney expects before it takes that instance out of rotation.

Several indicators arrive with the framework. The db check exercises the DataSource, diskSpace reports free space and ping answers with UP, while mail, redis and mongo appear when the matching libraries sit on the classpath. Grails wires its DataSource through GORM, so db shows up with no extra configuration. Details are hidden by default in current Boot releases, which means a fresh deployment returns little more than a status field until you opt in.

Once details are enabled the payload expands into a components map, where each entry carries its own status and a details object that you control. That is a useful place to publish queue depth, the age of the last successful batch or a provider identifier, information that helps during an incident and is risky to publish openly. Decide early who gets to read it, because the same endpoint is often polled by people, by Kubernetes and by a vendor monitoring agent.

Why custom checks earn their keep

Framework checks catch JVM and disk level problems. They know nothing about the third-party BNPL provider your checkout calls, the document bucket in the AWS Sydney region or the internal queue that sits idle until midnight. Those dependencies fail in ways a ping never sees, and a Grails instance can keep answering HTTP requests while completing no useful work at all.

A custom indicator lets you choose the severity yourself. DOWN means the instance cannot do its job and should leave the rotation. OUT_OF_SERVICE marks a deliberate pause, and UNKNOWN suits a check that could not run, such as an optional integration with no credentials in the current environment. Getting that distinction right stops a half-broken dependency from triggering a restart loop across your whole fleet.

Strong candidates include external API reachability, queue depth and consumer lag, the freshness of scheduled job output, feature flag evaluation and storage bucket access. Keep each one cheap, time-limited and free of side effects, because the health endpoint gets polled constantly, and a slow check quietly becomes a load generator pointed at somebody else's service.

Writing your first indicator in Groovy

Spring Boot offers two shapes for custom checks. HealthIndicator returns a single Health object, while CompositeHealthContributor groups several contributors under one key. Extending AbstractHealthIndicator saves you from declaring the interface method by hand and gives you an easy path from a thrown exception to a DOWN status with the exception recorded in the details.

package payments

import org.springframework.boot.actuate.health.AbstractHealthIndicator
import org.springframework.boot.actuate.health.Health
import org.springframework.stereotype.Component

@Component
class PaymentGatewayHealthIndicator extends AbstractHealthIndicator {

    private final PaymentGatewayClient client

    PaymentGatewayHealthIndicator(PaymentGatewayClient client) {
        this.client = client
    }

    @Override
    protected Health health() {
        def ping = client.ping()
        Health.up()
                .withDetail('provider', 'example-payments')
                .withDetail('latencyMs', ping.latencyMs)
                .withDetail('mode', ping.sandbox ? 'sandbox' : 'live')
                .build()
    }
}

Place the class under grails-app/services or src/main/groovy. Artefacts in the services directory are Spring beans already, so @Component is optional there, and constructor injection behaves the same way it does in a controller. Groovy property access keeps the class compact, but the health object still has to be assembled explicitly, and every withDetail value should be a simple type rather than a domain instance.

Keep credentials out of the details. A connection string or API key in a health response eventually reaches a dashboard, a log aggregator or a screenshot in a postmortem. Report identifiers, latencies and modes instead, and reserve withException for genuine failures, where the message is the fastest route to a diagnosis.

Registering the bean inside a Grails application

Bean naming decides what operators see. The name registered in the Spring context becomes the key in the components map, so pick something deliberate such as paymentGateway and the endpoint lists components.paymentGateway instead of a generated artefact name that nobody remembers by Friday.

package config

import org.springframework.context.annotation.Bean

class HealthConfiguration {

    @Bean
    PaymentGatewayHealthIndicator paymentGateway(PaymentGatewayClient client) {
        new PaymentGatewayHealthIndicator(client)
    }
}

Registering through a @Bean method in a Grails configuration class gives you control over the name and over the order in which beans are created. If a family of checks belongs together, implement CompositeHealthContributor to nest them under a single heading such as payments, which keeps a busy endpoint readable.

Testing follows normal Grails practice. Write a Spock specification in src/test/groovy, assert on the returned status and details for a healthy client and a failing one, then run grails test-app so the indicator is exercised without a running server.

Securing the endpoint and shaping the payload

Configuration lives in grails-app/conf/application.yml, and the actuator accepts the usual Boot properties. A typical block looks like this:

management:
  endpoint:
    health:
      show-details: when-authorized
      probes:
        enabled: true
  endpoints:
    web:
      exposure:
        include: health,info

Details stay hidden unless the caller is authorised, and a load balancer or a kubelet never needs the full contents, only the status. Grails projects running the Spring Security Core plugin can lock the management endpoints down with role rules, and management.server.port moves the actuator onto a separate port so a security group in an Australian cloud account can keep it off the public internet.

Probes deserve separate treatment from human monitoring. The liveness group should answer as long as the JVM is alive, while the readiness group can include the checks that decide whether traffic should arrive. Adding a slow external dependency to liveness turns a partner outage into a restart storm. You can also switch off noisy defaults with management.health.db.enabled or map statuses to codes with management.endpoint.health.status.http-mapping.

Checks that map to Australian deployments

Deploying in Australia adds a few wrinkles worth reflecting in your checks. Latency to an AWS Sydney region endpoint in ap-southeast-2 is low from a Sydney or Melbourne data centre, but a batch job that ships data to a provider with a European home office runs during European business hours, which lands in the middle of the night in Brisbane or Perth. A timed check with a narrow window can fail every evening while everything works exactly as designed. Perth-based resources operators running remote sites over intermittent links need readiness to report a degraded state rather than a hard down, otherwise traffic gets shifted to sites that cannot serve it either.

Public holidays deserve the same attention. Daily reconciliation that runs in Sydney produces no fresh output on Australia Day, ANZAC Day or Boxing Day, and a naive last-run-age check marks the service DOWN for days at a time. Australian teams also wind down across the Christmas and New Year period, when even healthy dependencies look stale. Calendar-aware thresholds, or a check that reports the expected holiday schedule, prevent a flood of false alerts every December.

Checks worth adding to a Grails application

Settings worth reviewing before go-live

Ship your first custom health indicator

A custom health indicator is a small class with a large payoff: a handful of lines of Groovy that turns a hidden dependency into a visible status your platform can act on. Start with one check, wire it into an existing Grails application, and read the response through /actuator/health in staging before you let an orchestrator make decisions from it.

Confirm the shape of the response in staging and run grails test-app until it tells the story you want on a dashboard. The Grails and Groovy tutorials on Grails Example cover the framework setup, actuator configuration and testing patterns behind this walkthrough, so you can move from a working indicator to a monitored service in an afternoon.