{"id":59773,"date":"2020-08-21T15:27:47","date_gmt":"2020-08-21T15:27:47","guid":{"rendered":"https:\/\/www.baeldung.com\/?p=86046"},"modified":"2020-08-21T15:27:47","modified_gmt":"2020-08-21T15:27:47","slug":"health-indicators-in-spring-boot","status":"publish","type":"post","link":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/2020\/08\/21\/health-indicators-in-spring-boot\/","title":{"rendered":"Health Indicators in Spring Boot"},"content":{"rendered":"<p class=\"syndicated-attribution\"><meta name= \\\"keywords \\\" content= \\\"\u96fb\u5b50\u8a08\u7b97\u6a5f, \u6559\u80b2, IT \u96fb\u8166\u73ed,\u96fb\u8166\u88dc\u7fd2\uff0c \u96fb\u8166\u73ed\uff0c \u5bb6\u6559\uff0c \u79c1\u4eba\u8001\u5e2b\uff0c \u8cc7\u8a0a\u6280\u8853\uff0c \u7a0b\u5e8f\u8a2d\u8a08\uff0c \u96fb\u5b50\u8a08\u7b97\u6a5f\uff0c \u904a\u6232\uff0c \u860b\u679c\uff0c \u96fb\u5f71\uff0c \u8a08\u7b97\u6a5f\uff0c\u7de8\u78bc\uff0c Java\uff0c C\/C++\uff0c JavaScript\uff0c PHP\uff0c HTML\uff0c CSS\uff0c MySQL\uff0c mobile\uff0c Android\uff0c \u52d5\u6f2b\uff0c Python\uff0c teacher\uff0c \u88dc\u7fd2\uff0c \u96fb\u8166\u88dc\u7fd2 \u8cc7\u8a0a, \u7535\u5b50\u8ba1\u7b97\u673a, IT ,Game, apple, movie, Computer,student,Java,\u6559\u80b2, ,\u5b66\u751f, \u5b66\u4e60, learn, \u6559\u5b66,  Android, apple,anime, animation, \u4fe1\u606f\u6280\u672f, \u7a0b\u5e8f\u8bbe\u8ba1, \u79fb\u52a8\u7535\u8bdd, \u8cc7\u8a0a\u79d1\u6280,Game, Jeu, Juego,Call Of Duty ,\u4f7f\u547d\u53ec\u559a , \u6e38\u620f, \u7535\u5b50\u6e38\u620f,, \u591a\u4eba\u7535\u5b50\u6e38\u620f, \u7f51\u7edc\u6e38\u620f\uff0conline\uff0conline game, \u624b\u673a\u6e38\u620f, mobile \\\"><\/p>\n<h2 data-id=\"overview-3\">1. Overview<\/h2>\n<div class=\"bd-anchor\" id=\"overview-3\"><\/div>\n<p>Spring Boot provides a few different ways to inspect the status and health of a running application and its components. Among those approaches, the <em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/2.3.0.RELEASE\/api\/org\/springframework\/boot\/actuate\/health\/HealthContributor.html\">HealthContributor<\/a>\u00a0<\/em>and\u00a0<em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/current\/api\/org\/springframework\/boot\/actuate\/health\/HealthIndicator.html\">HealthIndicator<\/a>\u00a0<\/em>APIs are two of the notable ones.<\/p>\n<p>In this tutorial, we&#8217;re going to get familiar with these APIs, learn how they work, and see how we can contribute custom information to them.<\/p>\n<h2 data-id=\"dependencies\">2. Dependencies<\/h2>\n<div class=\"bd-anchor\" id=\"dependencies\"><\/div>\n<p>Health information contributors are part of the <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/spring-boot-actuators\">Spring Boot actuator<\/a> module, so we need the appropriate <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/search.maven.org\/search?q=g:org.springframework.boot%20AND%20a:spring-boot-starter-actuator\">Maven dependency<\/a>:<\/p>\n<pre><code class=\"language-xml\">&lt;dependency&gt;\r\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\r\n    &lt;artifactId&gt;spring-boot-starter-actuator&lt;\/artifactId&gt;\r\n&lt;\/dependency<\/code><\/pre>\n<h2 data-id=\"built-inhealthindicators\">3. Built-in\u00a0<em>HealthIndicator<\/em>s<\/h2>\n<div class=\"bd-anchor\" id=\"built-inhealthindicators\"><\/div>\n<p>Out of the box, <strong>Spring Boot registers many\u00a0<em>HealthIndicator<\/em>s to report the healthiness of a particular application aspect<\/strong>.<\/p>\n<p>Some of those indicators are almost always registered, such as <em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/current\/api\/org\/springframework\/boot\/actuate\/system\/DiskSpaceHealthIndicator.html\">DiskSpaceHealthIndicator<\/a>\u00a0<\/em>or\u00a0<a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/current\/api\/org\/springframework\/boot\/actuate\/health\/PingHealthIndicator.html\"><em>PingHealthIndicator<\/em><\/a>. The former reports the current state of the disk and the latter serves as a\u00a0ping endpoint for the application.<\/p>\n<p><strong>On the other hand, Spring Boot registers some indicators conditionally<\/strong>. That is if some dependencies are on the classpath or some other conditions are met, Spring Boot might register a few other\u00a0<em>HealthIndicator<\/em>s, too. For instance, if we&#8217;re using relational databases, then Spring Boot registers\u00a0<a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/current\/api\/org\/springframework\/boot\/actuate\/jdbc\/DataSourceHealthIndicator.html\"><em>DataSourceHealthIndicator<\/em><\/a>. Similarly, it&#8217;ll register\u00a0<em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/current\/api\/org\/springframework\/boot\/actuate\/cassandra\/CassandraHealthIndicator.html\">CassandraHealthIndicator<\/a>\u00a0<\/em>if we happen to use\u00a0Cassandra as our data store.<\/p>\n<p><strong>In order to inspect the health status of a Spring Boot application, we can call the\u00a0<em>\/actuator\/health\u00a0<\/em>endpoint<\/strong>. This endpoint will report an aggregated result of all registered\u00a0<em>HealthIndicator<\/em>s.<\/p>\n<p><strong>Also, to see the health report from one specific indicator, we can call the\u00a0<em>\/actuator\/health\/{name}\u00a0<\/em>endpoint<\/strong>. For instance, calling the\u00a0<em>\/actuator\/health\/diskSpace\u00a0<\/em>endpoint will return a status report from the\u00a0<em>DiskSpaceHealthIndicator<\/em>:<\/p>\n<pre><code class=\"language-json\">{\r\n  \"status\": \"UP\",\r\n  \"details\": {\r\n    \"total\": 499963170816,\r\n    \"free\": 134414831616,\r\n    \"threshold\": 10485760,\r\n    \"exists\": true\r\n  }\r\n}<\/code><\/pre>\n<h2 data-id=\"customhealthindicators\">4. Custom\u00a0<em>HealthIndicator<\/em>s<\/h2>\n<div class=\"bd-anchor\" id=\"customhealthindicators\"><\/div>\n<p>In addition to the built-in ones, we can register custom\u00a0<em>HealthIndicator<\/em>s\u00a0to report the health of a component or subsystem. In order to that, <strong>all we have to do is to register an implementation of the\u00a0<em>HealthIndicator <\/em>interface\u00a0as a Spring bean<\/strong>.<\/p>\n<p>For instance, the following implementation reports a failure randomly:<\/p>\n<pre><code class=\"language-java\">@Component\r\npublic class RandomHealthIndicator implements HealthIndicator {\r\n    @Override\r\n    public Health health() {\r\n        double chance = ThreadLocalRandom.current().nextDouble();\r\n        Health.Builder status = Health.up();\r\n        if (chance &gt; 0.9) {\r\n            status = Health.down();\r\n        }\r\n        return status.build();\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>According to the health report from this indicator, the application should be up only 90% of the time. Here we&#8217;re using <em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/current\/api\/org\/springframework\/boot\/actuate\/health\/Health.html\">Health<\/a>\u00a0<\/em>builders to report the health information.<\/p>\n<p><strong>In reactive applications, however, we should register a bean of type\u00a0<a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/current\/api\/org\/springframework\/boot\/actuate\/health\/ReactiveHealthIndicator.html\"><em>ReactiveHealthIndicator<\/em><\/a><\/strong>. The reactive\u00a0<em>health()\u00a0<\/em>method returns a <em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/www.baeldung.com\/reactor-core#2-mono\">Mono&lt;Health&gt;<\/a>\u00a0<\/em>instead of a simple\u00a0<em>Health<\/em>. Other than that, other details are the same for both web application types.<\/p>\n<h3 data-id=\"1-indicator-name\">4.1. Indicator Name<\/h3>\n<div class=\"bd-anchor\" id=\"1-indicator-name\"><\/div>\n<p>To see the report for this particular indicator, we can call the\u00a0<em>\/actuator\/health\/random\u00a0<\/em>endpoint. For instance, here&#8217;s what the API response might look like:<\/p>\n<pre><code class=\"language-json\">{\"status\": \"UP\"}<\/code><\/pre>\n<p>The\u00a0<em>random\u00a0<\/em>in the\u00a0<em>\/actuator\/health\/random\u00a0<\/em>URL is the identifier for this indicator.\u00a0<strong>The identifier for a particular\u00a0<em>HealthIndicator\u00a0<\/em><\/strong><strong>implementation is equal to the bean name without the\u00a0<em>HealthIndicator\u00a0<\/em><\/strong><strong>suffix.\u00a0<\/strong>Since the bean name is\u00a0<em>randomHealthIdenticator<\/em>, the\u00a0<em>random\u00a0<\/em>prefix\u00a0will be the identifier.<\/p>\n<p>With this algorithm, if we change the bean name to, say,\u00a0<em>rand<\/em>:<\/p>\n<pre><code class=\"language-java\">@Component(\"rand\")\r\npublic class RandomHealthIndicator implements HealthIndicator {\r\n    \/\/ omitted\r\n}<\/code><\/pre>\n<p>Then the indicator identifier will be\u00a0<em>rand\u00a0<\/em>instead of\u00a0<em>random<\/em>.<\/p>\n<h3 data-id=\"2-disabling-the-indicator\">4.2. Disabling the Indicator<\/h3>\n<div class=\"bd-anchor\" id=\"2-disabling-the-indicator\"><\/div>\n<p><strong>To disable a particular indicator, we can set the\u00a0<em>&#8220;<\/em><em>management.health.&lt;indicator_identifier&gt;.enabled&#8221;\u00a0<\/em>configuration\u00a0property to\u00a0<em>false<\/em><\/strong>. For instance, if we add the following to our\u00a0<em>application.properties<\/em>:<\/p>\n<pre><code class=\"language-plaintext\">management.health.random.enabled=false<\/code><\/pre>\n<p>Then Spring Boot will disable the\u00a0<em>RandomHealthIndicator<\/em>. To activate this configuration property, we should also add the\u00a0<em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/current\/api\/org\/springframework\/boot\/actuate\/autoconfigure\/health\/ConditionalOnEnabledHealthIndicator.html\">@ConditionalOnEnabledHealthIndicator<\/a>\u00a0<\/em>annotation on the indicator:<\/p>\n<pre><code class=\"language-java\">@Component\r\n@ConditionalOnEnabledHealthIndicator(\"random\")\r\npublic class RandomHealthIndicator implements HealthIndicator { \r\n    \/\/ omitted\r\n}<\/code><\/pre>\n<p>Now if we call the\u00a0<em>\/actuator\/health\/random<\/em>, Spring Boot will return a 404 Not Found HTTP response:<\/p>\n<pre><code class=\"language-java\">@SpringBootTest\r\n@AutoConfigureMockMvc\r\n@TestPropertySource(properties = \"management.health.random.enabled=false\")\r\nclass DisabledRandomHealthIndicatorIntegrationTest {\r\n    @Autowired\r\n    private MockMvc mockMvc;\r\n    @Test\r\n    void givenADisabledIndicator_whenSendingRequest_thenReturns404() throws Exception {\r\n        mockMvc.perform(get(\"\/actuator\/health\/random\"))\r\n          .andExpect(status().isNotFound());\r\n    }\r\n}<\/code><\/pre>\n<p>Please note that disabling built-in or custom indicators is similar to each other. Therefore, we can apply the same configuration to the built-in indicators, too.<\/p>\n<h3 data-id=\"3-additional-details\">4.3. Additional Details<\/h3>\n<div class=\"bd-anchor\" id=\"3-additional-details\"><\/div>\n<p>In addition to reporting the status, we can attach additional key-value details using the\u00a0<a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/current\/api\/org\/springframework\/boot\/actuate\/health\/Health.Builder.html#withDetail-java.lang.String-java.lang.Object-\"><em>withDetail(key, value)<\/em><\/a>:<\/p>\n<pre><code class=\"language-java\">public Health health() {\r\n    double chance = ThreadLocalRandom.current().nextDouble();\r\n    Health.Builder status = Health.up();\r\n    if (chance &gt; 0.9) {\r\n        status = Health.down();\r\n    }\r\n    return status\r\n      .withDetail(\"chance\", chance)\r\n      .withDetail(\"strategy\", \"thread-local\")\r\n      .build();\r\n}<\/code><\/pre>\n<p>Here we&#8217;re adding two pieces of information to the status report. Also, we can achieve the same thing by passing a <em>Map&lt;String, Object&gt;\u00a0<\/em>to the\u00a0<em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/current\/api\/org\/springframework\/boot\/actuate\/health\/Health.Builder.html#withDetail-java.lang.String-java.lang.Object-\">withDetails(map)<\/a>\u00a0<\/em>method:<\/p>\n<pre><code class=\"language-java\">Map&lt;String, Object&gt; details = new HashMap&lt;&gt;();\r\ndetails.put(\"chance\", chance);\r\ndetails.put(\"strategy\", \"thread-local\");\r\n        \r\nreturn status.withDetails(details).build();<\/code><\/pre>\n<p>Now if we call the\u00a0<em>\/actuator\/health\/random<\/em>, we might see something like:<\/p>\n<pre><code class=\"language-json\">{\r\n  \"status\": \"DOWN\",\r\n  \"details\": {\r\n    \"chance\": 0.9883560157173152,\r\n    \"strategy\": \"thread-local\"\r\n  }\r\n}<\/code><\/pre>\n<p>We can verify this behavior with an automated test, too:<\/p>\n<pre><code class=\"language-java\">mockMvc.perform(get(\"\/actuator\/health\/random\"))\r\n  .andExpect(jsonPath(\"$.status\").exists())\r\n  .andExpect(jsonPath(\"$.details.strategy\").value(\"thread-local\"))\r\n  .andExpect(jsonPath(\"$.details.chance\").exists());<\/code><\/pre>\n<p>Sometimes an exception occurs while communicating to a system component such as Database or Disk. We can report such exceptions using the\u00a0<em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/current\/api\/org\/springframework\/boot\/actuate\/health\/Health.Builder.html#withException-java.lang.Throwable-\">withException(ex)<\/a>\u00a0<\/em>method:<\/p>\n<pre><code class=\"language-java\">if (chance &gt; 0.9) {\r\n    status.withException(new RuntimeException(\"Bad luck\"));\r\n}<\/code><\/pre>\n<p>We can also pass the exception to the\u00a0<em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/current\/api\/org\/springframework\/boot\/actuate\/health\/Health.Builder.html#down-java.lang.Throwable-\">down(ex)<\/a>\u00a0<\/em>method we saw earlier:<\/p>\n<pre><code class=\"language-java\">if (chance &gt; 0.9) {\r\n    status = Health.down(new RuntimeException(\"Bad Luck\"));\r\n}<\/code><\/pre>\n<p>Now the health report will contain the stack trace:<\/p>\n<pre><code class=\"language-json\">{\r\n  \"status\": \"DOWN\",\r\n  \"details\": {\r\n    \"error\": \"java.lang.RuntimeException: Bad Luck\",\r\n    \"chance\": 0.9603739107139401,\r\n    \"strategy\": \"thread-local\"\r\n  }\r\n}<\/code><\/pre>\n<h3 data-id=\"4-details-exposure\">4.4. Details Exposure<\/h3>\n<div class=\"bd-anchor\" id=\"4-details-exposure\"><\/div>\n<p><strong>The\u00a0<em>management.endpoint.health.show-details\u00a0<\/em>configuration property controls the level of details each health endpoint can expose.\u00a0<\/strong><\/p>\n<p>For instance, if we set this property to\u00a0<em>always,\u00a0<\/em>then Spring Boot will always return the\u00a0<em>details\u00a0<\/em>field in the health report, just like the above example.<\/p>\n<p>On the other hand, <strong>if we set this property to\u00a0<em>never<\/em>, then Spring Boot will always omit the <em>details<\/em> from the output<\/strong>. There is also the\u00a0<em>when_authorized\u00a0<\/em>value which exposes the additional <em>details<\/em> only for authorized users. A user is authorized if and only if:<\/p>\n<ul>\n<li>She&#8217;s authenticated<\/li>\n<li>And she possesses the roles specified in the\u00a0<em>management.endpoint.health.roles\u00a0<\/em>configuration property<\/li>\n<\/ul>\n<h3 data-id=\"5-health-status\">4.5. Health Status<\/h3>\n<div class=\"bd-anchor\" id=\"5-health-status\"><\/div>\n<p>By default, Spring Boot defines four different values as the health <em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/current\/api\/org\/springframework\/boot\/actuate\/health\/Status.html\">Status<\/a><\/em>:<\/p>\n<ul>\n<li><em>UP &#8212;<\/em>\u00a0The component or subsystem is working as expected<\/li>\n<li><em>DOWN\u00a0<\/em>&#8212; The component is not working<\/li>\n<li><em>OUT_OF_SERVICE\u00a0<\/em>&#8212; The component is out of service temporarily<\/li>\n<li><em>UNKNOWN\u00a0<\/em>&#8212; The component state is unknown<\/li>\n<\/ul>\n<p>These states are declared as\u00a0<em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/spring-projects\/spring-boot\/blob\/310ef6e9995fab302f6af8b284d0a59ca0f212e9\/spring-boot-project\/spring-boot-actuator\/src\/main\/java\/org\/springframework\/boot\/actuate\/health\/Status.java#L43\">public static final<\/a>\u00a0<\/em>instances instead of Java enums. So it&#8217;s possible to define our own custom health states. To do that, we can use the <em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/current\/api\/org\/springframework\/boot\/actuate\/health\/Health.Builder.html#status-java.lang.String-\">status(name)<\/a>\u00a0<\/em>method:<\/p>\n<pre><code class=\"language-java\">Health.Builder warning = Health.status(\"WARNING\");<\/code><\/pre>\n<p><strong>The health status affects the HTTP status code of the health endpoint<\/strong>. By default, Spring Boot maps the\u00a0<em>DOWN<\/em>, and <em>OUT_OF_SERVICE\u00a0<\/em>states to throw a 503 status code. On the other hand, <em>UP\u00a0<\/em>and any other unmapped statuses will be translated to a 200 OK status code.<\/p>\n<p>To customize this mapping, <strong>we can set the\u00a0<em>management.endpoint.health.status.http-mapping.&lt;status&gt;\u00a0<\/em>configuration property to the desired HTTP status code number:<\/strong><\/p>\n<pre><code class=\"language-plaintext\">management.endpoint.health.status.http-mapping.down=500\r\nmanagement.endpoint.health.status.http-mapping.out_of_service=503\r\nmanagement.endpoint.health.status.http-mapping.warning=500<\/code><\/pre>\n<p>Now Spring Boot will map the\u00a0<em>DOWN\u00a0<\/em>status to 500,\u00a0<em>OUT_OF_SERVICE\u00a0<\/em>to 503, and <em>WARNING\u00a0<\/em>to 500\u00a0HTTP status codes:<\/p>\n<pre><code class=\"language-java\">mockMvc.perform(get(\"\/actuator\/health\/warning\"))\r\n  .andExpect(jsonPath(\"$.status\").value(\"WARNING\"))\r\n  .andExpect(status().isInternalServerError());<\/code><\/pre>\n<p>Similarly, <strong>we can register a bean of type\u00a0<em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/2.2.3.RELEASE\/api\/org\/springframework\/boot\/actuate\/health\/HttpCodeStatusMapper.html\">HttpCodeStatusMapper<\/a>\u00a0<\/em>to customize the HTTP status code mapping<\/strong>:<\/p>\n<pre><code class=\"language-java\">@Component\r\npublic class CustomStatusCodeMapper implements HttpCodeStatusMapper {\r\n    @Override\r\n    public int getStatusCode(Status status) {\r\n        if (status == Status.DOWN) {\r\n            return 500;\r\n        }\r\n        \r\n        if (status == Status.OUT_OF_SERVICE) {\r\n            return 503;\r\n        }\r\n        \r\n        if (status == Status.UNKNOWN) {\r\n            return 500;\r\n        }\r\n        return 200;\r\n    }\r\n}\r\n<\/code><\/pre>\n<p>The\u00a0<em>getStatusCode(status)\u00a0<\/em>method takes the health status as the input and returns the HTTP status code as the output. Also, it&#8217;s possible to map custom\u00a0<em>Status\u00a0<\/em>instances:<\/p>\n<pre><code class=\"language-java\">if (status.getCode().equals(\"WARNING\")) {\r\n    return 500;\r\n}<\/code><\/pre>\n<p>By default, Spring Boot registers a simple implementation of this interface with default mappings. <strong>The\u00a0<em><a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/docs.spring.io\/spring-boot\/docs\/2.2.3.RELEASE\/api\/org\/springframework\/boot\/actuate\/health\/SimpleHttpCodeStatusMapper.html\">SimpleHttpCodeStatusMapper<\/a>\u00a0<\/em>is also capable of reading the mappings from the configuration files, as we saw earlier.<\/strong><\/p>\n<h2 data-id=\"health-information-vs-metrics\">5. Health Information vs Metrics<\/h2>\n<div class=\"bd-anchor\" id=\"health-information-vs-metrics\"><\/div>\n<p>Non-trivial applications usually contain a few different components. For instance, consider a Spring Boot applications using Cassandra as its database, Apache Kafka as its pub-sub platform, and Hazelcast as its in-memory data grid.<\/p>\n<p><strong>We should use\u00a0<em>HealthIndicator<\/em>s to see whether the application can communicate with these components or not<\/strong>. If the communication link fails or the component itself is down or slow, then we have an unhealthy component that we should be aware of. In other words, these indicators should be used to report the healthiness of different components or subsystems.<\/p>\n<p>On the contrary, we should avoid using <em>HealthIndicator<\/em>s to measure values, count events, or\u00a0measure durations. That&#8217;s why we have metrics. Put simply, <strong>metrics are a better tool to report CPU usage, load average, heap size, HTTP response distributions, and so on.<\/strong><\/p>\n<h2 data-id=\"conclusion-4\">6. Conclusion<\/h2>\n<div class=\"bd-anchor\" id=\"conclusion-4\"><\/div>\n<p>In this tutorial, we saw how to contribute more health information to actuator health endpoints. Moreover, we had in-depth coverage of different components in the health APIs such as <em>Health<\/em>,\u00a0<em>Status<\/em>, and the status to HTTP status mapping.<\/p>\n<p>To wrap things up, we had a quick discussion on the difference between health information and metrics and also, learned when to use each of them.<\/p>\n<p>As usual, all the examples are available <a href=\"https:\/\/feeds.feedblitz.com\/~\/t\/0\/0\/baeldung\/~https:\/\/github.com\/eugenp\/tutorials\/tree\/master\/spring-boot-modules\/spring-boot-actuator\">over on GitHub<\/a>.<\/p>\n<p><Img align=\"left\" border=\"0\" height=\"1\" width=\"1\" alt=\"\" style=\"border:0;float:left;margin:0;padding:0;width:1px!important;height:1px!important;\" hspace=\"0\" src=\"https:\/\/feeds.feedblitz.com\/~\/i\/634095788\/0\/baeldung\"><\/p>\n<div style=\"clear:both;padding-top:0.2em;\"><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/634095788\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/634095788\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/634095788\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/634095788\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/634095788\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/634095788\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\" style=\"border:0;margin:0;padding:0;\"><\/a>&#160;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/spring-boot-health-indicators#respond\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&#160;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/spring-boot-health-indicators\/feed\/\"><img decoding=\"async\" height=\"20\" style=\"border:0;margin:0;padding:0;\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&#160;<\/div>\n\n<p class=\"syndicated-attribution\"><figure class= \\\"wp-block-image alignnone \\\"><img src= \\\"http:\/\/itteacheritfreelance.hk\/test\/wordpress\/wp-content\/uploads\/2016\/05\/logo2-2.png\\\" alt=\\\"IT\u96fb\u8166\u88dc\u7fd2 java\u88dc\u7fd2 \u70ba\u5927\u5bb6\u914d\u5c0d\u96fb\u8166\u88dc\u7fd2,IT freelance, \u79c1\u4eba\u8001\u5e2b, PHP\u88dc\u7fd2,CSS\u88dc\u7fd2,XML,Java\u88dc\u7fd2,MySQL\u88dc\u7fd2,graphic design\u88dc\u7fd2,\u4e2d\u5c0f\u5b78ICT\u88dc\u7fd2,\u4e00\u5c0d\u4e00\u79c1\u4eba\u88dc\u7fd2\u548cFreelance\u81ea\u7531\u5de5\u4f5c\u914d\u5c0d\u3002\\\"\/><figcaption>\u7acb\u523b\u8a3b\u518a\u53ca\u5831\u540d\u96fb\u8166\u88dc\u7fd2\u8ab2\u7a0b\u5427!<\/figcaption><\/figure>\r\n<\/br>Find A Teacher Form:\r\n<\/br>https:\/\/docs.google.com\/forms\/d\/1vREBnX5n262umf4wU5U2pyTwvk9O-JrAgblA-wH9GFQ\/viewform?edit_requested=true#responses\r\n<\/br><\/br>Email:\r\n<\/br>public1989two@gmail.com<br><br><br><br><br><br><br>\r\n<a href=www.itsec.hk style=color:#FFFFFF;>www.itsec.hk<\/a><br>\r\n<a href=\\\"www.itsec.vip\\\" style=color:#FFFFFF;>www.itsec.vip<\/a><br>\r\n<a href=\\\"www.itseceu.uk\\\" style=color:#FFFFFF;>www.itseceu.uk<\/a><br><\/p>","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Learn all about Health Indicators in Spring Boot.<\/p>\n<div><a title=\"Like on Facebook\" href=\"https:\/\/feeds.feedblitz.com\/_\/28\/634095788\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/fblike20.png\"><\/a>&nbsp;<a title=\"Share on Google+\" href=\"https:\/\/feeds.feedblitz.com\/_\/30\/634095788\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/googleplus20.png\"><\/a>&nbsp;<a title=\"Pin it!\" href=\"https:\/\/feeds.feedblitz.com\/_\/29\/634095788\/baeldung,\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/pinterest20.png\"><\/a>&nbsp;<a title=\"Tweet This\" href=\"https:\/\/feeds.feedblitz.com\/_\/24\/634095788\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/twitter20.png\"><\/a>&nbsp;<a title=\"Subscribe by email\" href=\"https:\/\/feeds.feedblitz.com\/_\/19\/634095788\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/email20.png\"><\/a>&nbsp;<a title=\"Subscribe by RSS\" href=\"https:\/\/feeds.feedblitz.com\/_\/20\/634095788\/baeldung\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/rss20.png\"><\/a>&nbsp;<a rel=\"NOFOLLOW\" title=\"View Comments\" href=\"https:\/\/www.baeldung.com\/spring-boot-health-indicators#respond\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/comments20.png\"><\/a>&nbsp;<a title=\"Follow Comments via RSS\" href=\"https:\/\/www.baeldung.com\/spring-boot-health-indicators\/feed\/\"><img decoding=\"async\" height=\"20\" src=\"https:\/\/assets.feedblitz.com\/i\/commentsrss20.png\"><\/a>&nbsp;<\/div>\n<\/div>","protected":false},"author":760,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"slim_seo":{"title":"Health Indicators in Spring Boot - ITTeacherITFreelance.hk","description":"Learn all about Health Indicators in Spring Boot. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"},"footnotes":""},"categories":[6],"tags":[],"_links":{"self":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/59773"}],"collection":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/users\/760"}],"replies":[{"embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/comments?post=59773"}],"version-history":[{"count":5,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/59773\/revisions"}],"predecessor-version":[{"id":62425,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/posts\/59773\/revisions\/62425"}],"wp:attachment":[{"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/media?parent=59773"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/categories?post=59773"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itteacheritfreelance.hk\/wordpress\/index.php\/wp-json\/wp\/v2\/tags?post=59773"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}