Home > Back-end >  Change default health and prometheus actuator endpoints in spring webflux
Change default health and prometheus actuator endpoints in spring webflux

Time:10-07

I am building a new webflux spring service and I have enabled actuator (health, info and prometheus). The infrastructure where this service will be deployed is kind of old-ish and the monitoring tools are already configured to look for "/health", "/metrics" and it seems like it's difficult for the admins (!?) to look for different urls.

Now, I could try to make a dummy controller and respond with some 300 status and redirect to the actual endpoint - but the monitoring tool is configured not to follow redirects.

The best option would be (IMO) to configure spring in application.yml to use a separate url (like, instead of /actuator/health to simply use /health) but I failed to find any relevant resource.

The next best option would be to implement a new RestController and then forward the requests - something like this:

@RestController
public class ObservabilityForwardConfig {

    @GetMapping("/metrics")
    public Mono<Rendering> forwardToMetrics() {
        log.info("metrics endpoint was hit");
        return Mono.just(Rendering.redirectTo("/actuator/prometheus").build());
    }
}

This one fails with some weird exception:

2022-10-05 14:07:27,211 [reactor-http-epoll-4] ERROR e.e.e.c.s.s.c.RestExceptionHandler - Got an unhandled exception: 
org.springframework.core.codec.CodecException: Type definition error: [simple type, class org.springframework.web.reactive.result.view.DefaultRendering]; nested exception is com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.web.reactive.result.view.DefaultRendering and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
    at org.springframework.http.codec.json.AbstractJackson2Encoder.encodeValue(AbstractJackson2Encoder.java:230)
    Suppressed: reactor.core.publisher.FluxOnAssembly$OnAssemblyException: 
Error has been observed at the following site(s):
    *__checkpoint ⇢ Handler eu.europa.ec.cc.storage.service.config.ObservabilityForwardConfig#forwardToMetrics() [DispatcherHandler]
Original Stack Trace:
        at org.springframework.http.codec.json.AbstractJackson2Encoder.encodeValue(AbstractJackson2Encoder.java:230)
...
Caused by: com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class org.springframework.web.reactive.result.view.DefaultRendering and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
    at com.fasterxml.jackson.databind.exc.InvalidDefinitionException.from(InvalidDefinitionException.java:77)
    at com.fasterxml.jackson.databind.SerializerProvider.reportBadDefinition(SerializerProvider.java:1300)

and this is the reason why I am looking for some config alternative.

CodePudding user response:

I haven't found how to configure spring how to overwrite the paths for those actuators but I found why my forwarder class was not working. I should have annotated it with @Controller instead of @RestController:

@Controller
public class ObservabilityForwardConfig {

    @GetMapping("/metrics")
    public Mono<Rendering> forwardToMetrics() {
        return Mono.just(Rendering.redirectTo("/actuator/prometheus").build());
    }
}

CodePudding user response:

The Spring Actuator library has full support for changing the names and paths of it's endpoints. Its pointed out in the actuator chapter in the spring boot documentation.

The following example remaps /actuator/health to /healthcheck:

management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=healthcheck

so the syntax is:

management.endpoints.web.path-mapping.<what_endpoint>=<new_endpoint_path>
  • Related