0

I‘m looking for a way to change the header (add some things to it) of the health check of spring boot. I searched for this topic but I only found questions about custom health functions and so on (e.g. How to add a custom health check in spring boot health?). That‘s not what I am looking for.

I want to use the standard/default health function (so I don‘t want to change the body of the response („status“:“UP“) nor do I want to implement my own health functionality or customize the default one. My goal is to change the header of the response in order to achieve two things:

  • add some things to the header (e.g. max age)
  • reaching cors (e.g. I want to set allow-origin to *, allowed-method to GET and so on), I know there is a way to do it in the management properties but this applied for all endpoints and I only want these tgings to apply for the health endpoint

Is there any way to use the default health check and just modify the header and the properties like allowed origin or do I have to create a new controller?

Thanks for your help.

sideshowbarker
  • 62,215
  • 21
  • 143
  • 153

1 Answers1

0

Just implement your own service with the methods getting the adequate metrics, and then of course your custom controller refering to it. I don't think you can modify those default endpoints..

Here's an example of this custom service:

import java.util.Arrays;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.metrics.MetricsEndpoint;
import org.springframework.stereotype.Component;

@Component
public class SpringActuator {
    @Autowired
    MetricsEndpoint metrics;
    
    public Double getCPU() {
        return metrics.metric("process.cpu.usage", Arrays.asList()).getMeasurements().get(0).getValue();
    }
    
    public Double getRAM() {
        return metrics.metric("jvm.memory.used", Arrays.asList()).getMeasurements().get(0).getValue();
    }
}
k-wasilewski
  • 2,683
  • 1
  • 5
  • 15