-
Notifications
You must be signed in to change notification settings - Fork 69
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
Closes #1565: [Bug] - No default sample-mode for auto tracing settings #1566
Closes #1565: [Bug] - No default sample-mode for auto tracing settings #1566
Conversation
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.
Reviewed 2 of 3 files at r1, all commit messages.
Reviewable status: 2 of 3 files reviewed, 1 unresolved discussion (waiting on @heiko-holz)
inspectit-ocelot-core/src/main/java/rocks/inspectit/ocelot/core/instrumentation/config/MethodHookConfigurationResolver.java
line 185 at r1 (raw file):
.anyMatch(instrumentationRule -> RuleTracingSettings.DEFAULT_SAMPLE_MODE != instrumentationRule.getTracing() .getSampleMode()) ? getAndDetectConflicts(matchedRules, r -> r.getTracing() .getSampleMode(), n -> RuleTracingSettings.DEFAULT_SAMPLE_MODE != n, "the trace sample mode") : RuleTracingSettings.DEFAULT_SAMPLE_MODE);
That code is pretty dense and hard to understand IMHO. Especially the ternary operator doesn't make it easier to read.
I propose the following alternative. We overload getAndDetectConflicts
to return a default value if there is no element satisfying the filter. The original method simply forwards to the new one with null
as default value to return.
You can see below what I have in mind.
Code snippet:
builder.sampleMode(getAndDetectConflicts(matchedRules, r -> r.getTracing.getSampleMode(), n -> RuleTracingSettings.DEFAULT_SAMPLE_MODE != n, "the trace sample mode", RuleTracingSettings.DEFAULT_SAMPLE_MODE);
...
/**
* Utility function for merging configurations from multiple rules and detecting conflicts.
* <p>
* The same as calling getAndDetectConflicts (rules, getter, filter, exceptionMessage, null);
* </p>
*/
private <T> T getAndDetectConflicts(Collection<InstrumentationRule> rules, Function<InstrumentationRule, T> getter, Predicate<? super T> filter, String exceptionMessage) throws ConflictingDefinitionsException {
return getAndDetectConflicts(rules, getter, filter, exceptionMessage, null);
}
/**
* Utility function for merging configurations from multiple rules and detecting conflicts.
* This method first calls the given getter on all specified rules and filters the results using the given filter.
* <p>
* It then ensures that all provided values are equal, otherwise raises an exception with the given message
*
* @param rules the rules on which the getter will be called
* @param getter the getter function to call on each rule
* @param filter the predicate to filter the results of the getters with, e.g. Objects#nonNull
* @param exceptionMessage the name of the setting to print in an exception message
* @param defaultValue the value which is returned, if filter does not match for any element of rules
* @param <T> the type of the value which is being queried
*
* @return null if none of the rules have a setting matching the given filter. Otherwise returns the setting found.
*
* @throws ConflictingDefinitionsException thrown if a conflicting setting is detected
*/
private <T> T getAndDetectConflicts(Collection<InstrumentationRule> rules, Function<InstrumentationRule, T> getter, Predicate<? super T> filter, String exceptionMessage, T defaultValue) throws ConflictingDefinitionsException {
Optional<InstrumentationRule> firstMatch = rules.stream().filter(r -> filter.test(getter.apply(r))).findFirst();
if (firstMatch.isPresent()) {
T value = getter.apply(firstMatch.get());
Optional<InstrumentationRule> secondMatch = rules.stream()
.filter(r -> r != firstMatch.get())
.filter(r -> filter.test(getter.apply(r)))
.filter(r -> !Objects.equals(getter.apply(r), value))
.findFirst();
if (secondMatch.isPresent()) {
throw new ConflictingDefinitionsException(firstMatch.get(), secondMatch.get(), exceptionMessage);
} else {
return value;
}
} else {
return defaultValue;
}
}
Previously, quandor (Jochen Just) wrote…
Thanks for your suggestion, I agree! |
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #1566 +/- ##
============================================
- Coverage 78.28% 78.22% -0.06%
+ Complexity 2404 2403 -1
============================================
Files 246 246
Lines 7909 7911 +2
Branches 936 936
============================================
- Hits 6191 6188 -3
- Misses 1303 1308 +5
Partials 415 415
|
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.
Reviewed 1 of 1 files at r2, all commit messages.
Reviewable status:complete! all files reviewed, all discussions resolved (waiting on @heiko-holz)
Closes #1565
This change is![Reviewable](https://camo.githubusercontent.com/23b05f5fb48215c989e92cc44cf6512512d083132bd3daf689867c8d9d386888/68747470733a2f2f72657669657761626c652e696f2f7265766965775f627574746f6e2e737667)