-
Notifications
You must be signed in to change notification settings - Fork 15.4k
KAFKA-8696: clean up Sum/Count/Total metrics #7057
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
Merged
guozhangwang
merged 2 commits into
apache:trunk
from
vvcephei:MINOR-fix-total-count-sum-metrics
Jul 23, 2019
Merged
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
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
50 changes: 50 additions & 0 deletions
50
clients/src/main/java/org/apache/kafka/common/metrics/stats/CumulativeSum.java
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 |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You 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 | ||
| * | ||
| * http://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.apache.kafka.common.metrics.stats; | ||
|
|
||
| import org.apache.kafka.common.metrics.MeasurableStat; | ||
| import org.apache.kafka.common.metrics.MetricConfig; | ||
|
|
||
| /** | ||
| * An non-sampled cumulative total maintained over all time. | ||
| * This is a non-sampled version of {@link WindowedSum}. | ||
| * | ||
| * See also {@link CumulativeCount} if you just want to increment the value by 1 on each recording. | ||
| */ | ||
| public class CumulativeSum implements MeasurableStat { | ||
|
vvcephei marked this conversation as resolved.
Outdated
|
||
|
|
||
| private double total; | ||
|
|
||
| public CumulativeSum() { | ||
| total = 0.0; | ||
| } | ||
|
|
||
| public CumulativeSum(double value) { | ||
| total = value; | ||
| } | ||
|
|
||
| @Override | ||
| public void record(MetricConfig config, double value, long now) { | ||
| total += value; | ||
| } | ||
|
|
||
| @Override | ||
| public double measure(MetricConfig config, long now) { | ||
| return total; | ||
| } | ||
|
|
||
| } | ||
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 |
|---|---|---|
|
|
@@ -23,48 +23,46 @@ | |
| import org.apache.kafka.common.MetricName; | ||
| import org.apache.kafka.common.metrics.CompoundStat; | ||
| import org.apache.kafka.common.metrics.MetricConfig; | ||
| import org.apache.kafka.common.metrics.stats.Rate.SampledTotal; | ||
|
|
||
|
|
||
| /** | ||
| * A compound stat that includes a rate metric and a cumulative total metric. | ||
| */ | ||
| public class Meter implements CompoundStat { | ||
|
|
||
| private final MetricName rateMetricName; | ||
| private final MetricName totalMetricName; | ||
| private final Rate rate; | ||
| private final Total total; | ||
| private final CumulativeSum total; | ||
|
|
||
| /** | ||
| * Construct a Meter with seconds as time unit and {@link SampledTotal} stats for Rate | ||
| * Construct a Meter with seconds as time unit | ||
| */ | ||
| public Meter(MetricName rateMetricName, MetricName totalMetricName) { | ||
| this(TimeUnit.SECONDS, new SampledTotal(), rateMetricName, totalMetricName); | ||
| this(TimeUnit.SECONDS, new WindowedSum(), rateMetricName, totalMetricName); | ||
| } | ||
|
|
||
| /** | ||
| * Construct a Meter with provided time unit and {@link SampledTotal} stats for Rate | ||
| * Construct a Meter with provided time unit | ||
| */ | ||
| public Meter(TimeUnit unit, MetricName rateMetricName, MetricName totalMetricName) { | ||
| this(unit, new SampledTotal(), rateMetricName, totalMetricName); | ||
| this(unit, new WindowedSum(), rateMetricName, totalMetricName); | ||
| } | ||
|
|
||
| /** | ||
| * Construct a Meter with seconds as time unit and provided {@link SampledStat} stats for Rate | ||
| * Construct a Meter with seconds as time unit | ||
| */ | ||
| public Meter(SampledStat rateStat, MetricName rateMetricName, MetricName totalMetricName) { | ||
| this(TimeUnit.SECONDS, rateStat, rateMetricName, totalMetricName); | ||
| } | ||
|
|
||
| /** | ||
| * Construct a Meter with provided time unit and provided {@link SampledStat} stats for Rate | ||
| * Construct a Meter with provided time unit | ||
| */ | ||
| public Meter(TimeUnit unit, SampledStat rateStat, MetricName rateMetricName, MetricName totalMetricName) { | ||
| if (!(rateStat instanceof SampledTotal) && !(rateStat instanceof Count)) { | ||
| throw new IllegalArgumentException("Meter is supported only for SampledTotal and Count"); | ||
| if (!(rateStat instanceof WindowedSum)) { | ||
|
Contributor
Author
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. note that WindowedCount is a subclass of WindowedSum, so we just need one condition to check both (and also to permit any of the deprecated classes, as they all also subclass WindowedSum). |
||
| throw new IllegalArgumentException("Meter is supported only for WindowedCount or WindowedSum."); | ||
| } | ||
| this.total = new Total(); | ||
| this.total = new CumulativeSum(); | ||
| this.rate = new Rate(unit, rateStat); | ||
| this.rateMetricName = rateMetricName; | ||
| this.totalMetricName = totalMetricName; | ||
|
|
@@ -81,7 +79,7 @@ public List<NamedMeasurable> stats() { | |
| public void record(MetricConfig config, double value, long timeMs) { | ||
| rate.record(config, value, timeMs); | ||
| // Total metrics with Count stat should record 1.0 (as recorded in the count) | ||
| double totalValue = (rate.stat instanceof Count) ? 1.0 : value; | ||
| double totalValue = (rate.stat instanceof WindowedCount) ? 1.0 : value; | ||
| total.record(config, totalValue, timeMs); | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.