1 //
2 //
3 // Copyright 2022 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18 
19 #ifndef GRPCPP_EXT_CALL_METRIC_RECORDER_H
20 #define GRPCPP_EXT_CALL_METRIC_RECORDER_H
21 
22 #include <memory>
23 #include <string>
24 
25 #include "absl/strings/string_view.h"
26 #include "absl/types/optional.h"
27 
28 #include <grpcpp/impl/sync.h>
29 #include <grpcpp/support/slice.h>
30 
31 namespace grpc {
32 namespace experimental {
33 
34 /// Records call metrics for the purpose of load balancing.
35 /// During an RPC, call \a ServerContext::ExperimentalGetCallMetricRecorder()
36 /// method to retrive the recorder for the current call.
37 class CallMetricRecorder {
38  public:
39   virtual ~CallMetricRecorder() = default;
40 
41   /// Records a call metric measurement for CPU utilization.
42   /// Multiple calls to this method will override the stored value.
43   /// Values may be larger than 1.0 when the usage exceeds the reporter
44   /// dependent notion of soft limits.
45   /// Values outside of the valid range [0, infy] are ignored.
46   virtual CallMetricRecorder& RecordCpuUtilizationMetric(double value) = 0;
47 
48   /// Records a call metric measurement for memory utilization.
49   /// Multiple calls to this method will override the stored value.
50   /// Values outside of the valid range [0, 1] are ignored.
51   virtual CallMetricRecorder& RecordMemoryUtilizationMetric(double value) = 0;
52 
53   /// Records a call metric measurement for application specific utilization.
54   /// Multiple calls to this method will override the stored value.
55   /// Values may be larger than 1.0 when the usage exceeds the reporter
56   /// dependent notion of soft limits.
57   /// Values outside of the valid range [0, infy] are ignored.
58   virtual CallMetricRecorder& RecordApplicationUtilizationMetric(
59       double value) = 0;
60 
61   /// Records a call metric measurement for queries per second.
62   /// Multiple calls to this method will override the stored value.
63   /// Values outside of the valid range [0, infy) are ignored.
64   virtual CallMetricRecorder& RecordQpsMetric(double value) = 0;
65 
66   /// Records a call metric measurement for errors per second.
67   /// Multiple calls to this method will override the stored value.
68   /// Values outside of the valid range [0, infy) are ignored.
69   virtual CallMetricRecorder& RecordEpsMetric(double value) = 0;
70 
71   /// Records a call metric measurement for utilization.
72   /// Multiple calls to this method with the same name will
73   /// override the corresponding stored value. The lifetime of the
74   /// name string needs to be longer than the lifetime of the RPC
75   /// itself, since it's going to be sent as trailers after the RPC
76   /// finishes. It is assumed the strings are common names that
77   /// are global constants.
78   /// Values outside of the valid range [0, 1] are ignored.
79   virtual CallMetricRecorder& RecordUtilizationMetric(string_ref name,
80                                                       double value) = 0;
81 
82   /// Records a call metric measurement for request cost.
83   /// Multiple calls to this method with the same name will
84   /// override the corresponding stored value. The lifetime of the
85   /// name string needs to be longer than the lifetime of the RPC
86   /// itself, since it's going to be sent as trailers after the RPC
87   /// finishes. It is assumed the strings are common names that
88   /// are global constants.
89   virtual CallMetricRecorder& RecordRequestCostMetric(string_ref name,
90                                                       double value) = 0;
91 
92   /// Records an application-specific opaque metric measurement.
93   /// Multiple calls to this method with the same name will
94   /// override the corresponding stored value. The lifetime of the
95   /// name string needs to be longer than the lifetime of the RPC
96   /// itself, since it's going to be sent as trailers after the RPC
97   /// finishes. It is assumed the strings are common names that
98   /// are global constants.
99   virtual CallMetricRecorder& RecordNamedMetric(string_ref name,
100                                                 double value) = 0;
101 };
102 
103 }  // namespace experimental
104 }  // namespace grpc
105 
106 #endif  // GRPCPP_EXT_CALL_METRIC_RECORDER_H
107