Skip to content

Commit e0f94f5

Browse files
committed
Add "Health as Gauge" how-to documentation
Add how-to documentation describing how health information can be exported to a Micrometer Gauge. Closes gh-18329
1 parent 3e3587e commit e0f94f5

File tree

3 files changed

+160
-0
lines changed

3 files changed

+160
-0
lines changed

spring-boot-project/spring-boot-docs/src/main/asciidoc/howto.adoc

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2177,6 +2177,22 @@ The patterns to use can be customized using the `management.endpoint.env.keys-to
21772177

21782178

21792179

2180+
[[howto-map-health-indicators-to-metrics]]
2181+
=== Map Health Indicators to Micrometer Metrics
2182+
Spring Boot health indicators return a `Status` type to indicate the overall system health.
2183+
If you want to monitor or alert on levels of health for a particular application, you can export these statuses as metrics via Micrometer.
2184+
By default, the status codes "`up`", "`down`", "`out of service`" and "`unknown`" are used by Spring Boot.
2185+
To export these, you'll need to convert these states to some set of numbers so that they can be used with a Micrometer `Gauge`.
2186+
2187+
The follow example shows one way to write such an exporter:
2188+
2189+
[source,java,indent=0,subs="verbatim,quotes,attributes"]
2190+
----
2191+
include::{code-examples}/actuate/metrics/MetricsHealthMicrometerExportExample.java[tag=configuration]
2192+
----
2193+
2194+
2195+
21802196
[[howto-security]]
21812197
== Security
21822198
This section addresses questions about security when working with Spring Boot, including questions that arise from using Spring Security with Spring Boot.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.actuate.metrics;
18+
19+
import java.util.Map;
20+
21+
import io.micrometer.core.instrument.Gauge;
22+
import io.micrometer.core.instrument.MeterRegistry;
23+
24+
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
25+
import org.springframework.boot.actuate.health.HealthAggregator;
26+
import org.springframework.boot.actuate.health.HealthIndicator;
27+
import org.springframework.boot.actuate.health.Status;
28+
import org.springframework.context.annotation.Configuration;
29+
30+
/**
31+
* Example to show how to export {@link HealthIndicator} beans to a {@link MeterRegistry}.
32+
*
33+
* @author Phillip Webb
34+
*/
35+
public class MetricsHealthMicrometerExportExample {
36+
37+
// tag::configuration[]
38+
@Configuration
39+
public class HealthMetricsConfiguration {
40+
41+
public HealthMetricsConfiguration(MeterRegistry registry, HealthAggregator aggregator,
42+
Map<String, HealthIndicator> indicators) {
43+
// This example presumes common tags (such as the app) are applied elsewhere
44+
HealthIndicator health = new CompositeHealthIndicator(aggregator, indicators);
45+
Gauge.builder("health", health, this::getStatusCode).strongReference(true).register(registry);
46+
}
47+
48+
private int getStatusCode(HealthIndicator health) {
49+
Status status = health.health().getStatus();
50+
if (Status.UP.equals(status)) {
51+
return 3;
52+
}
53+
if (Status.OUT_OF_SERVICE.equals(status)) {
54+
return 2;
55+
}
56+
if (Status.DOWN.equals(status)) {
57+
return 1;
58+
}
59+
return 0;
60+
}
61+
62+
}
63+
// end::configuration[]
64+
65+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2012-2020 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.docs.actuate.metrics;
18+
19+
import io.micrometer.core.instrument.Gauge;
20+
import io.micrometer.core.instrument.MeterRegistry;
21+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
22+
import org.junit.Test;
23+
import org.junit.runner.RunWith;
24+
25+
import org.springframework.beans.factory.annotation.Autowired;
26+
import org.springframework.boot.actuate.autoconfigure.health.HealthIndicatorAutoConfiguration;
27+
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
28+
import org.springframework.boot.actuate.health.Health;
29+
import org.springframework.boot.actuate.health.HealthIndicator;
30+
import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
31+
import org.springframework.boot.test.context.SpringBootTest;
32+
import org.springframework.context.annotation.Bean;
33+
import org.springframework.context.annotation.Configuration;
34+
import org.springframework.context.annotation.Import;
35+
import org.springframework.test.context.junit4.SpringRunner;
36+
37+
import static org.assertj.core.api.Assertions.assertThat;
38+
39+
/**
40+
* Tests for {@link MetricsHealthMicrometerExportExample}.
41+
*
42+
* @author Phillip Webb
43+
*/
44+
@RunWith(SpringRunner.class)
45+
@SpringBootTest
46+
public class MetricsHealthMicrometerExportExampleTests {
47+
48+
@Autowired
49+
private MeterRegistry registry;
50+
51+
@Test
52+
public void registryExportsHealth() throws Exception {
53+
Gauge gauge = this.registry.get("health").gauge();
54+
assertThat(gauge.value()).isEqualTo(2);
55+
}
56+
57+
@Configuration
58+
@Import(MetricsHealthMicrometerExportExample.HealthMetricsConfiguration.class)
59+
@ImportAutoConfiguration(classes = { HealthIndicatorAutoConfiguration.class, MetricsAutoConfiguration.class })
60+
public static class Config {
61+
62+
@Bean
63+
public MetricsHealthMicrometerExportExample example() {
64+
return new MetricsHealthMicrometerExportExample();
65+
}
66+
67+
@Bean
68+
public SimpleMeterRegistry simpleMeterRegistry() {
69+
return new SimpleMeterRegistry();
70+
}
71+
72+
@Bean
73+
public HealthIndicator outOfService() {
74+
return () -> new Health.Builder().outOfService().build();
75+
}
76+
77+
}
78+
79+
}

0 commit comments

Comments
 (0)