Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d907ba5

Browse files
author
Jon Schneider
committed
Service level objective health indicators
1 parent c5b75a7 commit d907ba5

File tree

8 files changed

+481
-0
lines changed

8 files changed

+481
-0
lines changed

‎spring-boot-project/spring-boot-actuator-autoconfigure/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ dependencies {
5353
optional("io.micrometer:micrometer-registry-influx")
5454
optional("io.micrometer:micrometer-registry-jmx")
5555
optional("io.micrometer:micrometer-registry-kairos")
56+
optional("io.micrometer:micrometer-registry-health")
5657
optional("io.micrometer:micrometer-registry-new-relic")
5758
optional("io.micrometer:micrometer-registry-prometheus")
5859
optional("io.micrometer:micrometer-registry-stackdriver")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
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.actuate.autoconfigure.metrics.export.health;
18+
19+
import java.util.Arrays;
20+
import java.util.Map;
21+
import java.util.stream.Collectors;
22+
23+
import io.micrometer.core.instrument.Clock;
24+
import io.micrometer.core.instrument.Meter;
25+
import io.micrometer.core.instrument.Tag;
26+
import io.micrometer.core.instrument.binder.BaseUnits;
27+
import io.micrometer.core.instrument.config.NamingConvention;
28+
import io.micrometer.health.HealthConfig;
29+
import io.micrometer.health.HealthMeterRegistry;
30+
import io.micrometer.health.ServiceLevelObjective;
31+
import io.micrometer.health.objectives.JvmServiceLevelObjectives;
32+
import io.micrometer.health.objectives.OperatingSystemServiceLevelObjectives;
33+
34+
import org.springframework.beans.factory.ObjectProvider;
35+
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration;
36+
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration;
37+
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration;
38+
import org.springframework.boot.actuate.health.AbstractHealthIndicator;
39+
import org.springframework.boot.actuate.health.CompositeHealthContributor;
40+
import org.springframework.boot.actuate.health.Health;
41+
import org.springframework.boot.actuate.health.HealthContributor;
42+
import org.springframework.boot.actuate.health.Status;
43+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
44+
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
45+
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
46+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
47+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
48+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
49+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
50+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
51+
import org.springframework.context.annotation.Bean;
52+
import org.springframework.context.annotation.Configuration;
53+
import org.springframework.context.support.GenericApplicationContext;
54+
55+
/**
56+
* {@link EnableAutoConfiguration Auto-configuration} for building health indicators based
57+
* on service level objectives.
58+
*
59+
* @author Jon Schneider
60+
* @since 2.4.0
61+
*/
62+
@Configuration(proxyBeanMethods = false)
63+
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class })
64+
@AutoConfigureAfter(MetricsAutoConfiguration.class)
65+
@ConditionalOnBean(Clock.class)
66+
@ConditionalOnClass(HealthMeterRegistry.class)
67+
@ConditionalOnProperty(prefix = "management.metrics.export.health", name = "enabled", havingValue = "true",
68+
matchIfMissing = true)
69+
@EnableConfigurationProperties(HealthProperties.class)
70+
public class HealthMetricsExportAutoConfiguration {
71+
72+
private final NamingConvention camelCasedHealthIndicatorNames = NamingConvention.camelCase;
73+
74+
private final HealthProperties properties;
75+
76+
public HealthMetricsExportAutoConfiguration(HealthProperties properties) {
77+
this.properties = properties;
78+
}
79+
80+
@Bean
81+
@ConditionalOnMissingBean
82+
public HealthConfig healthConfig() {
83+
return new HealthPropertiesConfigAdapter(this.properties);
84+
}
85+
86+
@Bean
87+
@ConditionalOnMissingBean
88+
public HealthMeterRegistry healthMeterRegistry(HealthConfig healthConfig, Clock clock,
89+
ObjectProvider<ServiceLevelObjective> serviceLevelObjectives,
90+
GenericApplicationContext applicationContext) {
91+
HealthMeterRegistry registry = HealthMeterRegistry.builder(healthConfig).clock(clock)
92+
.serviceLevelObjectives(serviceLevelObjectives.orderedStream().toArray(ServiceLevelObjective[]::new))
93+
.serviceLevelObjectives(JvmServiceLevelObjectives.MEMORY)
94+
.serviceLevelObjectives(OperatingSystemServiceLevelObjectives.DISK)
95+
.serviceLevelObjectives(this.properties.getApiErrorBudgets().entrySet().stream().map((apiErrorBudget) -> {
96+
String apiEndpoints = '/' + apiErrorBudget.getKey().replace('.', '/');
97+
98+
return ServiceLevelObjective.build("api.error.ratio." + apiErrorBudget.getKey())
99+
.failedMessage("API error ratio exceeded.").baseUnit(BaseUnits.PERCENT)
100+
.tag("uri.matches", apiEndpoints + "/**").tag("error.outcome", "SERVER_ERROR")
101+
.errorRatio(
102+
(s) -> s.name("http.server.requests").tag("uri", (uri) -> uri.startsWith(apiEndpoints)),
103+
(all) -> all.tag("outcome", "SERVER_ERROR"))
104+
.isLessThan(apiErrorBudget.getValue());
105+
}).toArray(ServiceLevelObjective[]::new)).build();
106+
107+
for (ServiceLevelObjective slo : registry.getServiceLevelObjectives()) {
108+
applicationContext.registerBean(this.camelCasedHealthIndicatorNames.name(slo.getName(), Meter.Type.GAUGE),
109+
HealthContributor.class, () -> toHealthContributor(registry, slo));
110+
}
111+
112+
return registry;
113+
}
114+
115+
private HealthContributor toHealthContributor(HealthMeterRegistry registry, ServiceLevelObjective slo) {
116+
if (slo instanceof ServiceLevelObjective.SingleIndicator) {
117+
final NamingConvention tagConvention = this.camelCasedHealthIndicatorNames;
118+
return new AbstractHealthIndicator(slo.getFailedMessage()) {
119+
@Override
120+
protected void doHealthCheck(Health.Builder builder) {
121+
ServiceLevelObjective.SingleIndicator singleIndicator = (ServiceLevelObjective.SingleIndicator) slo;
122+
builder.status(slo.healthy(registry) ? Status.UP : Status.OUT_OF_SERVICE)
123+
.withDetail("value", singleIndicator.getValueAsString(registry))
124+
.withDetail("mustBe", singleIndicator.getTestDescription());
125+
126+
for (Tag tag : slo.getTags()) {
127+
builder.withDetail(tagConvention.tagKey(tag.getKey()), tag.getValue());
128+
}
129+
130+
if (slo.getBaseUnit() != null) {
131+
builder.withDetail("unit", slo.getBaseUnit());
132+
}
133+
}
134+
};
135+
}
136+
else {
137+
ServiceLevelObjective.MultipleIndicator multipleIndicator = (ServiceLevelObjective.MultipleIndicator) slo;
138+
Map<String, HealthContributor> objectiveIndicators = Arrays.stream(multipleIndicator.getObjectives())
139+
.collect(Collectors.toMap(
140+
(indicator) -> this.camelCasedHealthIndicatorNames.name(indicator.getName(), Meter.Type.GAUGE),
141+
(indicator) -> toHealthContributor(registry, indicator)));
142+
return CompositeHealthContributor.fromMap(objectiveIndicators);
143+
}
144+
}
145+
146+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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.actuate.autoconfigure.metrics.export.health;
18+
19+
import java.time.Duration;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
22+
23+
import org.springframework.boot.context.properties.ConfigurationProperties;
24+
25+
/**
26+
* {@link ConfigurationProperties @ConfigurationProperties} for configuring health
27+
* indicators based on service level objectives.
28+
*
29+
* @author Jon Schneider
30+
* @since 2.4.0
31+
*/
32+
@ConfigurationProperties(prefix = "management.metrics.export.health")
33+
public class HealthProperties {
34+
35+
/**
36+
* Step size (i.e. polling frequency for moving window indicators) to use.
37+
*/
38+
private Duration step = Duration.ofSeconds(10);
39+
40+
/**
41+
* Error budgets by API endpoint prefix. The value is a percentage in the range [0,1].
42+
*/
43+
private final Map<String, Double> apiErrorBudgets = new LinkedHashMap<>();
44+
45+
public Duration getStep() {
46+
return this.step;
47+
}
48+
49+
public void setStep(Duration step) {
50+
this.step = step;
51+
}
52+
53+
public Map<String, Double> getApiErrorBudgets() {
54+
return this.apiErrorBudgets;
55+
}
56+
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
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.actuate.autoconfigure.metrics.export.health;
18+
19+
import java.time.Duration;
20+
21+
import io.micrometer.health.HealthConfig;
22+
23+
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PropertiesConfigAdapter;
24+
25+
/**
26+
* Adapter to convert {@link HealthProperties} to a {@link HealthConfig}.
27+
*
28+
* @author Jon Schneider
29+
*/
30+
class HealthPropertiesConfigAdapter extends PropertiesConfigAdapter<HealthProperties> implements HealthConfig {
31+
32+
HealthPropertiesConfigAdapter(HealthProperties properties) {
33+
super(properties);
34+
}
35+
36+
@Override
37+
public String prefix() {
38+
return "management.metrics.export.health";
39+
}
40+
41+
@Override
42+
public String get(String k) {
43+
return null;
44+
}
45+
46+
@Override
47+
public Duration step() {
48+
return get(HealthProperties::getStep, HealthConfig.super::step);
49+
}
50+
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
/**
18+
* Support for building health indicators with service level objectives.
19+
*/
20+
package org.springframework.boot.actuate.autoconfigure.metrics.export.health;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /