-
Notifications
You must be signed in to change notification settings - Fork 14.7k
KAFKA-19684: Move Gauge#value to MetricValueProvider #20543
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
majialoong
wants to merge
2
commits into
apache:trunk
Choose a base branch
from
majialoong:KAFKA-19684
base: trunk
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.
+61
−28
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
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 |
---|---|---|
|
@@ -20,6 +20,8 @@ | |
import org.apache.kafka.common.MetricName; | ||
import org.apache.kafka.common.utils.Time; | ||
|
||
import java.util.Objects; | ||
|
||
public final class KafkaMetric implements Metric { | ||
|
||
private final MetricName metricName; | ||
|
@@ -41,9 +43,7 @@ public KafkaMetric(Object lock, MetricName metricName, MetricValueProvider<?> va | |
MetricConfig config, Time time) { | ||
this.metricName = metricName; | ||
this.lock = lock; | ||
if (!(valueProvider instanceof Measurable) && !(valueProvider instanceof Gauge)) | ||
throw new IllegalArgumentException("Unsupported metric value provider of class " + valueProvider.getClass()); | ||
this.metricValueProvider = valueProvider; | ||
this.metricValueProvider = Objects.requireNonNull(valueProvider, "valueProvider must not be null"); | ||
this.config = config; | ||
this.time = time; | ||
} | ||
|
@@ -67,20 +67,15 @@ public MetricName metricName() { | |
} | ||
|
||
/** | ||
* Take the metric and return the value, which could be a {@link Measurable} or a {@link Gauge} | ||
* Take the metric and return the value via {@link MetricValueProvider#value(MetricConfig, long)}. | ||
* | ||
* @return Return the metric value | ||
* @throws IllegalStateException if the underlying metric is not a {@link Measurable} or a {@link Gauge}. | ||
*/ | ||
@Override | ||
public Object metricValue() { | ||
long now = time.milliseconds(); | ||
synchronized (this.lock) { | ||
if (isMeasurable()) | ||
return ((Measurable) metricValueProvider).measure(config, now); | ||
else if (this.metricValueProvider instanceof Gauge) | ||
return ((Gauge<?>) metricValueProvider).value(config, now); | ||
else | ||
throw new IllegalStateException("Not a valid metric: " + this.metricValueProvider.getClass()); | ||
return metricValueProvider.value(config, now); | ||
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. Can we update the docs so they match these changes? 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. Thanks for the reminder, I will update this doc. |
||
} | ||
} | ||
|
||
|
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
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
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
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.
The
Gauge
type seems redundant at this stage. Would it be worth filing a KIP to deprecate it?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.
Keeping the Gauge type can improves code readability. Readers immediately understand that the monitoring metric represents an instantaneous value, and it can also be equated with the concept of Gauge in mainstream monitoring systems. Directly using the MetricValueProvider may be a bit too abstract.
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.
Another follow-up (KIP) could be removing
addMetric(MetricName metricName, Measurable measurable)
, which would let us drop the explicit type from the lambdabefore
after
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.
This is a good suggestion, I've created a JIRA to track this issue:https://issues.apache.org/jira/browse/KAFKA-19729