|
| 1 | +/* |
| 2 | + * Copyright 2012-2019 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.condition; |
| 18 | + |
| 19 | +import java.lang.annotation.Annotation; |
| 20 | + |
| 21 | +import org.springframework.boot.autoconfigure.condition.ConditionMessage; |
| 22 | +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; |
| 23 | +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; |
| 24 | +import org.springframework.context.annotation.ConditionContext; |
| 25 | +import org.springframework.core.annotation.AnnotationAttributes; |
| 26 | +import org.springframework.core.env.Environment; |
| 27 | +import org.springframework.core.type.AnnotatedTypeMetadata; |
| 28 | + |
| 29 | +/** |
| 30 | + * Metrics exporter enabled condition. All exporters can be disabled globally via the |
| 31 | + * {@code management.metrics.export.enabled} property or individually via the |
| 32 | + * {@code management.metrics.export.<name>.enabled} property (where {@code <name>} is the |
| 33 | + * name of the exporter. |
| 34 | + * |
| 35 | + * @author Chris Bono |
| 36 | + * @since 2.4.0 |
| 37 | + */ |
| 38 | +public class OnMetricsExportEnabledCondition extends SpringBootCondition { |
| 39 | + |
| 40 | + private static final String PREFIX = "management.metrics.export"; |
| 41 | + |
| 42 | + private static final Class<? extends Annotation> ANNOTATION_TYPE = ConditionalOnEnabledMetricsExport.class; |
| 43 | + |
| 44 | + @Override |
| 45 | + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { |
| 46 | + AnnotationAttributes annotationAttributes = AnnotationAttributes |
| 47 | + .fromMap(metadata.getAnnotationAttributes(this.ANNOTATION_TYPE.getName())); |
| 48 | + String exporterName = annotationAttributes.getString("value"); |
| 49 | + ConditionOutcome outcome = getSpecificExporterOutcome(context, exporterName); |
| 50 | + if (outcome != null) { |
| 51 | + return outcome; |
| 52 | + } |
| 53 | + return getGlobalExporterOutcome(context); |
| 54 | + } |
| 55 | + |
| 56 | + protected ConditionOutcome getSpecificExporterOutcome(ConditionContext context, String exporterName) { |
| 57 | + Environment environment = context.getEnvironment(); |
| 58 | + String enabledProperty = String.format("%s.%s.enabled", this.PREFIX, exporterName); |
| 59 | + if (!environment.containsProperty(enabledProperty)) { |
| 60 | + return null; |
| 61 | + } |
| 62 | + boolean match = environment.getProperty(enabledProperty, Boolean.class); |
| 63 | + return new ConditionOutcome(match, ConditionMessage.forCondition(this.ANNOTATION_TYPE) |
| 64 | + .because(String.format("%s is %b", enabledProperty, match))); |
| 65 | + } |
| 66 | + |
| 67 | + protected ConditionOutcome getGlobalExporterOutcome(ConditionContext context) { |
| 68 | + Environment environment = context.getEnvironment(); |
| 69 | + String enabledProperty = String.format("%s.enabled", this.PREFIX); |
| 70 | + boolean match = environment.getProperty(enabledProperty, Boolean.class, true); |
| 71 | + return new ConditionOutcome(match, ConditionMessage.forCondition(this.ANNOTATION_TYPE) |
| 72 | + .because(String.format("%s is considered %b", enabledProperty, match))); |
| 73 | + } |
| 74 | + |
| 75 | +} |
0 commit comments