|
1 | 1 | /*
|
2 |
| - * Copyright 2012-2020 the original author or authors. |
| 2 | + * Copyright 2012-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.boot.actuate.autoconfigure.metrics.export;
|
18 | 18 |
|
19 |
| -import org.springframework.boot.actuate.autoconfigure.OnEndpointElementCondition; |
| 19 | +import org.springframework.boot.autoconfigure.condition.ConditionMessage; |
| 20 | +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; |
| 21 | +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; |
20 | 22 | import org.springframework.context.annotation.Condition;
|
| 23 | +import org.springframework.context.annotation.ConditionContext; |
| 24 | +import org.springframework.core.annotation.AnnotationAttributes; |
| 25 | +import org.springframework.core.env.Environment; |
| 26 | +import org.springframework.core.type.AnnotatedTypeMetadata; |
21 | 27 |
|
22 | 28 | /**
|
23 | 29 | * {@link Condition} that checks if a metrics exporter is enabled.
|
24 | 30 | *
|
25 | 31 | * @author Chris Bono
|
| 32 | + * @author Moritz Halbritter |
26 | 33 | */
|
27 |
| -class OnMetricsExportEnabledCondition extends OnEndpointElementCondition { |
| 34 | +class OnMetricsExportEnabledCondition extends SpringBootCondition { |
28 | 35 |
|
29 |
| - protected OnMetricsExportEnabledCondition() { |
30 |
| - super("management.metrics.export.", ConditionalOnEnabledMetricsExport.class); |
| 36 | + private static final String PROPERTY_TEMPLATE = "management.%s.metrics.export.enabled"; |
| 37 | + |
| 38 | + private static final String DEFAULT_PROPERTY_NAME = "management.defaults.metrics.export.enabled"; |
| 39 | + |
| 40 | + @Override |
| 41 | + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { |
| 42 | + AnnotationAttributes annotationAttributes = AnnotationAttributes |
| 43 | + .fromMap(metadata.getAnnotationAttributes(ConditionalOnEnabledMetricsExport.class.getName())); |
| 44 | + String endpointName = annotationAttributes.getString("value"); |
| 45 | + ConditionOutcome outcome = getProductOutcome(context, endpointName); |
| 46 | + if (outcome != null) { |
| 47 | + return outcome; |
| 48 | + } |
| 49 | + return getDefaultOutcome(context); |
| 50 | + } |
| 51 | + |
| 52 | + private ConditionOutcome getProductOutcome(ConditionContext context, String productName) { |
| 53 | + Environment environment = context.getEnvironment(); |
| 54 | + String enabledProperty = PROPERTY_TEMPLATE.formatted(productName); |
| 55 | + if (environment.containsProperty(enabledProperty)) { |
| 56 | + boolean match = environment.getProperty(enabledProperty, Boolean.class, true); |
| 57 | + return new ConditionOutcome(match, ConditionMessage.forCondition(ConditionalOnEnabledMetricsExport.class) |
| 58 | + .because(enabledProperty + " is " + match)); |
| 59 | + } |
| 60 | + return null; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Return the default outcome that should be used if property is not set. By default |
| 65 | + * this method will use the {@link #DEFAULT_PROPERTY_NAME} property, matching if it is |
| 66 | + * {@code true} or if it is not configured. |
| 67 | + * @param context the condition context |
| 68 | + * @return the default outcome |
| 69 | + */ |
| 70 | + private ConditionOutcome getDefaultOutcome(ConditionContext context) { |
| 71 | + boolean match = Boolean.parseBoolean(context.getEnvironment().getProperty(DEFAULT_PROPERTY_NAME, "true")); |
| 72 | + return new ConditionOutcome(match, ConditionMessage.forCondition(ConditionalOnEnabledMetricsExport.class) |
| 73 | + .because(DEFAULT_PROPERTY_NAME + " is considered " + match)); |
31 | 74 | }
|
32 | 75 |
|
33 | 76 | }
|
0 commit comments