Is it possible to change the /health endpoint in spring-boot? - java

I have a spring-boot-web application and I am using actuator to check status of my application. Whenever I do /health I am able to check the status of my application. My doubt is, is it possible to change the /health endpoint to something like /ABC/health(I need to add context to /health). Is it possible or do I need to have a controller for this and need to handle it.

You can use a built-in property to expose all Actuator endpoints under different path. Just set it in your application.properties file:
management.context-path=/ABC
The health will be than available under /ABC/health.

Related

Passing a header to custom actuator health endpoint

I have created a custom spring-boot actuator health endpoint for my application. Spring boot aggregates all these custom health endpoints into a one big json and returns it when I hit application/health url. Now I want to pass some information to the custom health end point that I have implemented in form of a header when I hit the application/health url. How can I achieve this?
If you at all want to pass a header , you might consider hitting the API (a GET API in this case) from any RestClient like PostMan or Insomnia. Simply hitting the url from the browser is actually making a GET request but you can only add path or request parameters in the url, for adding a request body or headers,you will need to use a REST client, or alternatively do it from the command line using curl.
Also, after reading the comments,i think what you need is,is to set spring.profile property and determine your code flow based on that- here's how you can set the profile-
1) set Java System Properties (VM Arguments)
java -jar -Dspring.profiles.active=test myapp.jar
2) set Program arguments
java -jar application.jar --spring.profiles.active=test

How to make Sprint Boot 2 actuator path based on application properties?

I have a microservice application with Spring Boot 2, which used my own library to propagate new endpoint to itself by using actuator functionality - my own actuator implemented an MvcEndpoint interface, and contained 'path' variable which got real API mapping from application.yml of microservice that used this library.
But after I recoded my library to spring boot 2, I have an issue - id property on #Endpoint annotation contains only constant value (as every annotation properties), and I haven't found way to change API path of my actuator endpoint to something like '/api/v1/my-service/my-actuator-endpoint'.
I tried to write this in my application.yml of microservice:
endpoints:
my-actuator-endpoint:
path: /api/v1/my-service/my-actuator-endpoint
But this endpoint still accessible only from /my-actuator-endpoint , which means only by its id.
How can I make this be accessible by custom path like /api/v1/my-service/my-actuator-endpoint, which will be configured in application.yml?
Problem solved, needed to add this property to application.yml:
management:
endpoints:
web:
path-mapping:
my-actuator-endpoint: api/v1/my-service/my-actuator-endpoint
you can expose actuator endpoints by setting configuration in property file like this
management.context-path=/manage

SpringBoot 2 health end point JSON format has changed

We have recently upgraded out Spring-Boot version to 2.1.2 in one of our applications.
I noticed a change in JSON format when I hit this URL:
http://localhost:xxxx/health.
The changed structure is:
{
Health: {
status: "UP"
}
}
Earlier it was:
{
status: "UP"
}
My question is;
1. Why has it been modified?
2. Is there any config to keep the structure as it was before?
With the release of Spring Boot 2, Actuator has been redesigned, and new exciting endpoints were added.
The /actuator/health endpoint is used to check the health or state of the running application. It’s usually exercised by monitoring software to alert us if the running instance goes down or gets unhealthy for other reasons. E.g. Connectivity issues with our DB, lack of disk space…
http://localhost:8080/actuator/health
{
status: "UP"
}
Monitoring and Management over HTTP
If you are developing a web application, Spring Boot Actuator auto-configures all enabled endpoints to be exposed over HTTP. The default convention is to use the id of the endpoint with a prefix of /actuator as the URL path. For example, health is exposed as /actuator/health. TIP: Actuator is supported natively with Spring MVC, Spring WebFlux, and Jersey.
Actuator Security
For security purposes, all actuators other than /health and /info are disabled by default. The management.endpoints.web.exposure.include property can be used to enable the actuators.
If Spring Security is on the classpath and no other WebSecurityConfigurerAdapter is present, all actuators other than /health and /info are secured by Spring Boot auto-configuration. If you define a custom WebSecurityConfigurerAdapter, Spring Boot auto-configuration will back off and you will be in full control of actuator access rules.
Endpoints
Actuator endpoints let you monitor and interact with your application. Spring Boot includes a number of built-in endpoints and lets you add your own. For example, the health endpoint provides basic application health information.
Each individual endpoint can be enabled or disabled. This controls whether or not the endpoint is created and its bean exists in the application context. To be remotely accessible an endpoint also has to be exposed via JMX or HTTP. Most applications choose HTTP, where the ID of the endpoint along with a prefix of /actuator is mapped to a URL. For example, by default, the health endpoint is mapped to /actuator/health.
Actuator JSON
The JSON payloads returned from many endpoints have been improved with Spring Boot 2.0.
Many endpoints now have JSON that more accurately reflects the underlying data. For example, the /actuator/conditions endpoint (/autoconfig in Spring Boot 1.5) now has a top level contexts key to group results by ApplicationContext.
8. Health (health)
To retrieve the health of the application, make a GET request to /actuator/health, as shown in the following curl-based example:
$ curl 'http://localhost:8080/actuator/health' -i -X GET
Useful Information : Baeldung

Spring Boot - actuator- mappings : only own handler

is there a way in Spring Boot to display only my custom
Request Mappings with actuator? By default it shows everything.
As described in the reference documentation, the default behaviour of actuator endpoints, when the actuator module is on the classpath, is 'opt-out' - that is, most endpoints are enabled by default and must be disabled if required.
For the opposite effect (i.e. actuator endpoints must be specifically enabled), add the following setting to your application.properties:
management.endpoints.enabled-by-default=false
or alternatively, if using YAML:
management:
endpoints:
enabled-by-default: false
There is no way to filter which mappings get added. By default, anything with #RequestMapping is included.
You could always disable the provided mapping endpoint and write your own custom endpoint that includes only the controllers you care about.

Spring Boot Actuator Endpoint Override

I have been prototyping with Spring boot where I added dependency on spring-boot-starter-actuator and spring-boot-starter-data-rest and named my testing REST endpoint to /info. Application ran without any errors however my endpoint couldn't be called and app returned 404 all the time.
After some time I found out that actuator project contains SAME endpoint /info and basically overrides my custom RESTful endpoint since I didn't name it.
My question is: Is there any way how I can prevent such behavior in general (meaning bean clashing by mistake)? Or at least get WARN message when this is happening.
Thanks in advance for your answers
You can disable /info actuator endpoint by using the following property;
management.endpoint.info.enabled=false
Actually all can be disabled, or you can enable only certain ones, if you check the source link I've provided below;
By default, all endpoints except for shutdown are enabled. If you prefer to specifically “opt-in” endpoint enablement you can use the endpoints.enabled property.
source
For logging of this behaviour, while deploying you can see the endpoints and corresponding beans, you can deduce from this log I guess. But better not to use same endpoint with actuator while they are enabled.
Yes, there is a chance to disable particular classes by #EnableAutoconfiguration with a parameter exclude= where you can specify classname or whole package by using {} brackets
Example:
#EnableAutoConfiguration(exclude = {MyClassName.class}
#EnableAutoConfiguration(exclude = {MyClassName.class, MyClassName2.class})

Categories