Debian Codegraph @

spm-cpp-include/grpcpp/ext/call_metric_recorder.h

raw
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/codegen/sync.h>
29 #include <grpcpp/support/slice.h>
30
31 namespace grpc_core {
32 class Arena;
33 struct BackendMetricData;
34 } // namespace grpc_core
35
36 namespace grpc {
37 class ServerBuilder;
38
39 namespace experimental {
40 class OrcaServerInterceptor;
41
42 // Registers the per-rpc orca load reporter into the \a ServerBuilder.
43 // Once this is done, the server will automatically send the load metrics
44 // after each RPC as they were reported. In order to report load metrics,
45 // call the \a ServerContext::ExperimentalGetCallMetricRecorder() method to
46 // retrieve the recorder for the current call.
47 void EnableCallMetricRecording(ServerBuilder*);
48
49 /// Records call metrics for the purpose of load balancing.
50 /// During an RPC, call \a ServerContext::ExperimentalGetCallMetricRecorder()
51 /// method to retrive the recorder for the current call.
52 class CallMetricRecorder {
53 public:
54 explicit CallMetricRecorder(grpc_core::Arena* arena);
55 ~CallMetricRecorder();
56
57 /// Records a call metric measurement for CPU utilization.
58 /// Multiple calls to this method will override the stored value.
59 CallMetricRecorder& RecordCpuUtilizationMetric(double value);
60
61 /// Records a call metric measurement for memory utilization.
62 /// Multiple calls to this method will override the stored value.
63 CallMetricRecorder& RecordMemoryUtilizationMetric(double value);
64
65 /// Records a call metric measurement for utilization.
66 /// Multiple calls to this method with the same name will
67 /// override the corresponding stored value. The lifetime of the
68 /// name string needs to be longer than the lifetime of the RPC
69 /// itself, since it's going to be sent as trailers after the RPC
70 /// finishes. It is assumed the strings are common names that
71 /// are global constants.
72 CallMetricRecorder& RecordUtilizationMetric(string_ref name, double value);
73
74 /// Records a call metric measurement for request cost.
75 /// Multiple calls to this method with the same name will
76 /// override the corresponding stored value. The lifetime of the
77 /// name string needs to be longer than the lifetime of the RPC
78 /// itself, since it's going to be sent as trailers after the RPC
79 /// finishes. It is assumed the strings are common names that
80 /// are global constants.
81 CallMetricRecorder& RecordRequestCostMetric(string_ref name, double value);
82
83 private:
84 absl::optional<std::string> CreateSerializedReport();
85
86 internal::Mutex mu_;
87 grpc_core::BackendMetricData* backend_metric_data_ ABSL_GUARDED_BY(&mu_);
88 friend class experimental::OrcaServerInterceptor;
89 };
90
91 } // namespace experimental
92 } // namespace grpc
93
94 #endif // GRPCPP_EXT_CALL_METRIC_RECORDER_H