-
Notifications
You must be signed in to change notification settings - Fork 40.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Service level objective health indicators
- Loading branch information
Jon Schneider
committed
May 5, 2020
1 parent
c5b75a7
commit d907ba5
Showing
8 changed files
with
481 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
...oot/actuate/autoconfigure/metrics/export/health/HealthMetricsExportAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
/* | ||
* Copyright 2012-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.actuate.autoconfigure.metrics.export.health; | ||
|
||
import java.util.Arrays; | ||
import java.util.Map; | ||
import java.util.stream.Collectors; | ||
|
||
import io.micrometer.core.instrument.Clock; | ||
import io.micrometer.core.instrument.Meter; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.binder.BaseUnits; | ||
import io.micrometer.core.instrument.config.NamingConvention; | ||
import io.micrometer.health.HealthConfig; | ||
import io.micrometer.health.HealthMeterRegistry; | ||
import io.micrometer.health.ServiceLevelObjective; | ||
import io.micrometer.health.objectives.JvmServiceLevelObjectives; | ||
import io.micrometer.health.objectives.OperatingSystemServiceLevelObjectives; | ||
|
||
import org.springframework.beans.factory.ObjectProvider; | ||
import org.springframework.boot.actuate.autoconfigure.metrics.CompositeMeterRegistryAutoConfiguration; | ||
import org.springframework.boot.actuate.autoconfigure.metrics.MetricsAutoConfiguration; | ||
import org.springframework.boot.actuate.autoconfigure.metrics.export.simple.SimpleMetricsExportAutoConfiguration; | ||
import org.springframework.boot.actuate.health.AbstractHealthIndicator; | ||
import org.springframework.boot.actuate.health.CompositeHealthContributor; | ||
import org.springframework.boot.actuate.health.Health; | ||
import org.springframework.boot.actuate.health.HealthContributor; | ||
import org.springframework.boot.actuate.health.Status; | ||
import org.springframework.boot.autoconfigure.AutoConfigureAfter; | ||
import org.springframework.boot.autoconfigure.AutoConfigureBefore; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.support.GenericApplicationContext; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for building health indicators based | ||
* on service level objectives. | ||
* | ||
* @author Jon Schneider | ||
* @since 2.4.0 | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
@AutoConfigureBefore({ CompositeMeterRegistryAutoConfiguration.class, SimpleMetricsExportAutoConfiguration.class }) | ||
@AutoConfigureAfter(MetricsAutoConfiguration.class) | ||
@ConditionalOnBean(Clock.class) | ||
@ConditionalOnClass(HealthMeterRegistry.class) | ||
@ConditionalOnProperty(prefix = "management.metrics.export.health", name = "enabled", havingValue = "true", | ||
matchIfMissing = true) | ||
@EnableConfigurationProperties(HealthProperties.class) | ||
public class HealthMetricsExportAutoConfiguration { | ||
|
||
private final NamingConvention camelCasedHealthIndicatorNames = NamingConvention.camelCase; | ||
|
||
private final HealthProperties properties; | ||
|
||
public HealthMetricsExportAutoConfiguration(HealthProperties properties) { | ||
this.properties = properties; | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public HealthConfig healthConfig() { | ||
return new HealthPropertiesConfigAdapter(this.properties); | ||
} | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public HealthMeterRegistry healthMeterRegistry(HealthConfig healthConfig, Clock clock, | ||
ObjectProvider<ServiceLevelObjective> serviceLevelObjectives, | ||
GenericApplicationContext applicationContext) { | ||
HealthMeterRegistry registry = HealthMeterRegistry.builder(healthConfig).clock(clock) | ||
.serviceLevelObjectives(serviceLevelObjectives.orderedStream().toArray(ServiceLevelObjective[]::new)) | ||
.serviceLevelObjectives(JvmServiceLevelObjectives.MEMORY) | ||
.serviceLevelObjectives(OperatingSystemServiceLevelObjectives.DISK) | ||
.serviceLevelObjectives(this.properties.getApiErrorBudgets().entrySet().stream().map((apiErrorBudget) -> { | ||
String apiEndpoints = '/' + apiErrorBudget.getKey().replace('.', '/'); | ||
|
||
return ServiceLevelObjective.build("api.error.ratio." + apiErrorBudget.getKey()) | ||
.failedMessage("API error ratio exceeded.").baseUnit(BaseUnits.PERCENT) | ||
.tag("uri.matches", apiEndpoints + "/**").tag("error.outcome", "SERVER_ERROR") | ||
.errorRatio( | ||
(s) -> s.name("http.server.requests").tag("uri", (uri) -> uri.startsWith(apiEndpoints)), | ||
(all) -> all.tag("outcome", "SERVER_ERROR")) | ||
.isLessThan(apiErrorBudget.getValue()); | ||
}).toArray(ServiceLevelObjective[]::new)).build(); | ||
|
||
for (ServiceLevelObjective slo : registry.getServiceLevelObjectives()) { | ||
applicationContext.registerBean(this.camelCasedHealthIndicatorNames.name(slo.getName(), Meter.Type.GAUGE), | ||
HealthContributor.class, () -> toHealthContributor(registry, slo)); | ||
} | ||
|
||
return registry; | ||
} | ||
|
||
private HealthContributor toHealthContributor(HealthMeterRegistry registry, ServiceLevelObjective slo) { | ||
if (slo instanceof ServiceLevelObjective.SingleIndicator) { | ||
final NamingConvention tagConvention = this.camelCasedHealthIndicatorNames; | ||
return new AbstractHealthIndicator(slo.getFailedMessage()) { | ||
@Override | ||
protected void doHealthCheck(Health.Builder builder) { | ||
ServiceLevelObjective.SingleIndicator singleIndicator = (ServiceLevelObjective.SingleIndicator) slo; | ||
builder.status(slo.healthy(registry) ? Status.UP : Status.OUT_OF_SERVICE) | ||
.withDetail("value", singleIndicator.getValueAsString(registry)) | ||
.withDetail("mustBe", singleIndicator.getTestDescription()); | ||
|
||
for (Tag tag : slo.getTags()) { | ||
builder.withDetail(tagConvention.tagKey(tag.getKey()), tag.getValue()); | ||
} | ||
|
||
if (slo.getBaseUnit() != null) { | ||
builder.withDetail("unit", slo.getBaseUnit()); | ||
} | ||
} | ||
}; | ||
} | ||
else { | ||
ServiceLevelObjective.MultipleIndicator multipleIndicator = (ServiceLevelObjective.MultipleIndicator) slo; | ||
Map<String, HealthContributor> objectiveIndicators = Arrays.stream(multipleIndicator.getObjectives()) | ||
.collect(Collectors.toMap( | ||
(indicator) -> this.camelCasedHealthIndicatorNames.name(indicator.getName(), Meter.Type.GAUGE), | ||
(indicator) -> toHealthContributor(registry, indicator))); | ||
return CompositeHealthContributor.fromMap(objectiveIndicators); | ||
} | ||
} | ||
|
||
} |
57 changes: 57 additions & 0 deletions
57
...rg/springframework/boot/actuate/autoconfigure/metrics/export/health/HealthProperties.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2012-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.actuate.autoconfigure.metrics.export.health; | ||
|
||
import java.time.Duration; | ||
import java.util.LinkedHashMap; | ||
import java.util.Map; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
|
||
/** | ||
* {@link ConfigurationProperties @ConfigurationProperties} for configuring health | ||
* indicators based on service level objectives. | ||
* | ||
* @author Jon Schneider | ||
* @since 2.4.0 | ||
*/ | ||
@ConfigurationProperties(prefix = "management.metrics.export.health") | ||
public class HealthProperties { | ||
|
||
/** | ||
* Step size (i.e. polling frequency for moving window indicators) to use. | ||
*/ | ||
private Duration step = Duration.ofSeconds(10); | ||
|
||
/** | ||
* Error budgets by API endpoint prefix. The value is a percentage in the range [0,1]. | ||
*/ | ||
private final Map<String, Double> apiErrorBudgets = new LinkedHashMap<>(); | ||
|
||
public Duration getStep() { | ||
return this.step; | ||
} | ||
|
||
public void setStep(Duration step) { | ||
this.step = step; | ||
} | ||
|
||
public Map<String, Double> getApiErrorBudgets() { | ||
return this.apiErrorBudgets; | ||
} | ||
|
||
} |
51 changes: 51 additions & 0 deletions
51
...ework/boot/actuate/autoconfigure/metrics/export/health/HealthPropertiesConfigAdapter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
/* | ||
* Copyright 2012-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.actuate.autoconfigure.metrics.export.health; | ||
|
||
import java.time.Duration; | ||
|
||
import io.micrometer.health.HealthConfig; | ||
|
||
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PropertiesConfigAdapter; | ||
|
||
/** | ||
* Adapter to convert {@link HealthProperties} to a {@link HealthConfig}. | ||
* | ||
* @author Jon Schneider | ||
*/ | ||
class HealthPropertiesConfigAdapter extends PropertiesConfigAdapter<HealthProperties> implements HealthConfig { | ||
|
||
HealthPropertiesConfigAdapter(HealthProperties properties) { | ||
super(properties); | ||
} | ||
|
||
@Override | ||
public String prefix() { | ||
return "management.metrics.export.health"; | ||
} | ||
|
||
@Override | ||
public String get(String k) { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Duration step() { | ||
return get(HealthProperties::getStep, HealthConfig.super::step); | ||
} | ||
|
||
} |
20 changes: 20 additions & 0 deletions
20
...va/org/springframework/boot/actuate/autoconfigure/metrics/export/health/package-info.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/* | ||
* Copyright 2012-2020 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* Support for building health indicators with service level objectives. | ||
*/ | ||
package org.springframework.boot.actuate.autoconfigure.metrics.export.health; |
Oops, something went wrong.