xref: /aosp_15_r20/external/grpc-grpc-java/services/src/main/java/io/grpc/services/MetricRecorderHelper.java (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1 /*
2  * Copyright 2023 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.services;
18 
19 /**
20  * Utility helper class to check whether values for {@link CallMetricRecorder} and
21  * {@link MetricRecorder} are inside the valid range.
22  */
23 final class MetricRecorderHelper {
24 
25   /**
26    * Return true if the utilization value is in the range [0, 1] and false otherwise.
27    */
isUtilizationValid(double utilization)28   static boolean isUtilizationValid(double utilization) {
29     return utilization >= 0.0 && utilization <= 1.0;
30   }
31 
32   /**
33    * Return true if the cpu utilization or application specific utilization value is in the range
34    * [0, inf) and false otherwise. Occasionally users have over 100% cpu utilization and get a
35    * runaway effect where the backend with highest qps gets more and more qps sent to it. So we
36    * allow cpu utilization > 1.0, similarly for application specific utilization.
37    */
isCpuOrApplicationUtilizationValid(double utilization)38   static boolean isCpuOrApplicationUtilizationValid(double utilization) {
39     return utilization >= 0.0;
40   }
41 
42   /**
43    * Return true if a rate value (such as qps or eps) is in the range [0, inf) and false otherwise.
44    */
isRateValid(double rate)45   static boolean isRateValid(double rate) {
46     return rate >= 0.0;
47   }
48 
49   // Prevent instantiation.
MetricRecorderHelper()50   private MetricRecorderHelper() {
51   }
52 }
53