-
Notifications
You must be signed in to change notification settings - Fork 40.9k
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
Add tests to verify that every config setting for each auto-configured meter registry is exposed as a configuration property #33743
Closed
Closed
Changes from all commits
Commits
Show all changes
4 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 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
109 changes: 109 additions & 0 deletions
109
...gframework/boot/actuate/autoconfigure/metrics/export/TestConfigsToPropertiesExposure.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,109 @@ | ||
/* | ||
* Copyright 2012-2023 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; | ||
|
||
import java.lang.reflect.Method; | ||
import java.time.Duration; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
|
||
import org.junit.Assert; | ||
|
||
import org.springframework.boot.actuate.autoconfigure.metrics.export.properties.PropertiesConfigAdapter; | ||
|
||
/** | ||
* Utility to test that all Micrometer config values are exposed via properties and | ||
* settable using the corresponding {@link PropertiesConfigAdapter} implementation. | ||
* | ||
* @author Mirko Sobeck | ||
*/ | ||
public final class TestConfigsToPropertiesExposure { | ||
|
||
private TestConfigsToPropertiesExposure() { | ||
|
||
} | ||
|
||
private static final List<Class<?>> classesForPropertiesList = List.of(Boolean.class, Byte.class, Character.class, | ||
Short.class, Integer.class, Long.class, Double.class, Float.class, String.class, Duration.class); | ||
|
||
/** | ||
* Assertion to test if default methods of a given config are overridden by the | ||
* adapter which implements it. This can be an indicator for micrometer config fields, | ||
* that have been forgotten to expose via spring properties. Not overridden default | ||
* methods in adapters are the most common cause of forgotten field exposure, because | ||
* they do not for force an override. | ||
* @param config micrometer config | ||
* @param adapter adapter for properties {@link PropertiesConfigAdapter} | ||
* @param excludedConfigMethods config methods that should be excluded for the | ||
* assertion | ||
*/ | ||
public static void assertThatAllConfigDefaultMethodsAreOverriddenByAdapter(Class<?> config, | ||
Class<? extends PropertiesConfigAdapter<?>> adapter, String... excludedConfigMethods) { | ||
List<String> configDefaultMethodNames = Arrays.stream(config.getDeclaredMethods()) | ||
.filter((method) -> method.isDefault() && isSettableUsingProperties(method.getReturnType())) | ||
.map(Method::getName).collect(Collectors.toList()); | ||
|
||
configDefaultMethodNames.removeAll(Arrays.stream(excludedConfigMethods).toList()); | ||
List<String> notOverriddenDefaultMethods = new ArrayList<>(configDefaultMethodNames); | ||
|
||
Class<?> currentClass = adapter; | ||
// loop through adapter class and superclasses | ||
// to find not overridden config methods | ||
while (!Object.class.equals(currentClass)) { | ||
List<String> overriddenClassDefaultMethods = Arrays.stream(currentClass.getDeclaredMethods()) | ||
.map(Method::getName).filter(configDefaultMethodNames::contains).toList(); | ||
|
||
notOverriddenDefaultMethods.removeAll(overriddenClassDefaultMethods); | ||
currentClass = currentClass.getSuperclass(); | ||
} | ||
|
||
if (notOverriddenDefaultMethods.size() >= 1) { | ||
Assert.fail( | ||
"Found config default methods that are not overridden by the related PropertiesConfigAdapter: \n" | ||
+ notOverriddenDefaultMethods + "\n" | ||
+ "This could be an indicator for not exposed properties fields.\n" | ||
+ "Please check if the fields are meant to be exposed and if not, " | ||
+ "exclude them from this test by providing them to the method."); | ||
} | ||
} | ||
|
||
/** | ||
* Guess if a class can be set using properties. This will only catch the basic use | ||
* cases regarding the micrometer configs to filter out methods that are not likely to | ||
* be designed to be set via properties. <pre> | ||
* isSettableUsingProperties(String.class) = true | ||
* isSettableUsingProperties(boolean.class) = true | ||
* isSettableUsingProperties(Object.class) = false | ||
* </pre> | ||
* @param clazz Class | ||
* @return is likely to be settable using properties | ||
*/ | ||
private static boolean isSettableUsingProperties(Class<?> clazz) { | ||
if (Void.TYPE.equals(clazz)) { | ||
return false; | ||
} | ||
|
||
if (clazz.isPrimitive()) { | ||
return true; | ||
} | ||
|
||
return classesForPropertiesList.contains(clazz); | ||
} | ||
|
||
} |
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
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
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
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
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
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
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
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
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
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add
Enum
too?We have quite a few use-cases, e.g.:
HistogramFlavor
forPrometheusConfig
orDynatraceApiVersion
forDynatraceConfig
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for taking a look, @jonatan-ivanov. I've removed the allow-list approach in my polishing. We now consider all default methods with no parameters, except those that are deprecated or that return Validated. The code that does that is here.