-
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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; | ||
| } | ||
|
|
||
| } | ||
| 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); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,7 +16,6 @@ | |
| */ | ||
| package org.apache.kafka.common.metrics.stats; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
|
|
@@ -40,7 +39,7 @@ public Rate() { | |
| } | ||
|
|
||
| public Rate(TimeUnit unit) { | ||
| this(unit, new SampledTotal()); | ||
| this(unit, new WindowedSum()); | ||
| } | ||
|
|
||
| public Rate(SampledStat stat) { | ||
|
|
@@ -115,24 +114,10 @@ private double convert(long timeMs) { | |
| } | ||
| } | ||
|
|
||
| public static class SampledTotal extends SampledStat { | ||
|
|
||
| public SampledTotal() { | ||
| super(0.0d); | ||
| } | ||
|
|
||
| @Override | ||
| protected void update(Sample sample, MetricConfig config, double value, long timeMs) { | ||
| sample.value += value; | ||
| } | ||
|
|
||
| @Override | ||
| public double combine(List<Sample> samples, MetricConfig config, long now) { | ||
| double total = 0.0; | ||
| for (Sample sample : samples) | ||
| total += sample.value; | ||
| return total; | ||
| } | ||
|
|
||
| /** | ||
| * @deprecated since 2.4 Use {@link SampledTotal} instead. | ||
|
Contributor
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. The javadoc seems incorrect.
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. Oops! I wonder how that happened... Thanks for catching it. |
||
| */ | ||
| @Deprecated | ||
| public static class SampledTotal extends WindowedSum { | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.