- 
                Notifications
    You must be signed in to change notification settings 
- Fork 41.6k
Health indicators based on Service Level Objectives #21311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            jkschneider
  wants to merge
  2
  commits into
  spring-projects:main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
jkschneider:health-slos
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or 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
    
  
  
    
              
        
          
          
            150 changes: 150 additions & 0 deletions
          
          150 
        
  ...oot/actuate/autoconfigure/metrics/export/health/HealthMetricsExportAutoConfiguration.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or 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,150 @@ | ||
| /* | ||
| * 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 hidden or 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 hidden or 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 hidden or 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; | ||
| There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just to know - use of this file? | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.