maedhroz commented on code in PR #2534:
URL: https://github.com/apache/cassandra/pull/2534#discussion_r1311145605


##########
src/java/org/apache/cassandra/metrics/RatioGaugeSet.java:
##########
@@ -0,0 +1,119 @@
+/*
+ * 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.cassandra.metrics;
+
+import java.util.function.DoubleSupplier;
+
+import com.codahale.metrics.Metered;
+import com.codahale.metrics.RatioGauge;
+import org.apache.cassandra.metrics.CassandraMetricsRegistry.MetricName;
+
+import static org.apache.cassandra.metrics.CassandraMetricsRegistry.Metrics;
+
+public class RatioGaugeSet
+{
+    public final RatioGauge oneMinuteRatio;
+    public final RatioGauge fiveMinuteRatio;
+    public final RatioGauge fifteenMinuteRatio;
+    public final RatioGauge allTimeRatio;
+
+    public RatioGaugeSet(Metered numerator, Metered denominator, 
MetricNameFactory factory, String namePattern)
+    {
+        this.oneMinuteRatio = 
ratioGauge(factory.createMetricName(String.format(namePattern, 
"OneMinuteRatio")), numerator::getOneMinuteRate, denominator::getOneMinuteRate);
+        this.fiveMinuteRatio = 
ratioGauge(factory.createMetricName(String.format(namePattern, 
"FiveMinuteRatio")), numerator::getFiveMinuteRate, 
denominator::getFiveMinuteRate);
+        this.fifteenMinuteRatio = 
ratioGauge(factory.createMetricName(String.format(namePattern, 
"FifteenMinuteRatio")), numerator::getFifteenMinuteRate, 
denominator::getFifteenMinuteRate);
+        this.allTimeRatio = 
ratioGauge(factory.createMetricName(String.format(namePattern, 
"AllTimeRatio")), numerator::getMeanRate, denominator::getMeanRate);
+    }
+
+    private static RatioGauge ratioGauge(DoubleSupplier numerator, 
DoubleSupplier denominator)
+    {
+        return new RatioGauge()
+        {
+            protected Ratio getRatio()
+            {
+                return Ratio.of(numerator.getAsDouble(), 
denominator.getAsDouble());
+            }
+        };
+    }
+
+    private RatioGauge ratioGauge(MetricName name, DoubleSupplier numerator, 
DoubleSupplier denominator)
+    {
+        return Metrics.register(name, ratioGauge(numerator, denominator));
+    }
+
+    public static Metered sum(Metered... meters)
+    {
+        return new SumMeter(meters);
+    }
+
+    private static class SumMeter implements Metered
+    {
+        private final Metered[] meters;
+
+        public SumMeter(Metered... meters)
+        {
+            this.meters = meters;
+        }
+
+        @Override
+        public long getCount()
+        {
+            long count = 0;
+            for (Metered meter : meters)
+                count += meter.getCount();
+            return count;
+        }

Review Comment:
   You could cut down on some of the repetition here w/ something like...
   
   ```
   @Override
   public long getCount()
   {
       return getMetric(Metered::getCount);
   }
   ...
   private long getMetric(Function<Metered, Long> f)
   {
       long count = 0;
       for (Metered meter : meters)
           count += f.apply(meter);
       return count;
   }
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to