GRPC C++  1.66.0
server_metric_recorder.h
Go to the documentation of this file.
1 //
2 //
3 // Copyright 2023 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_SERVER_METRIC_RECORDER_H
20 #define GRPCPP_EXT_SERVER_METRIC_RECORDER_H
21 
22 #include <functional>
23 #include <map>
24 #include <memory>
25 
26 #include <grpcpp/impl/sync.h>
28 
29 namespace grpc_core {
30 struct BackendMetricData;
31 } // namespace grpc_core
32 
33 namespace grpc {
34 class BackendMetricState;
35 
36 namespace experimental {
43  public:
44  // Factory method. Use this to create.
45  static std::unique_ptr<ServerMetricRecorder> Create();
50  void SetCpuUtilization(double value);
54  void SetMemoryUtilization(double value);
58  void SetApplicationUtilization(double value);
62  void SetQps(double value);
66  void SetEps(double value);
73  void SetNamedUtilization(string_ref name, double value);
78  void SetAllNamedUtilization(std::map<string_ref, double> named_utilization);
79 
81  void ClearCpuUtilization();
87  void ClearQps();
89  void ClearEps();
92 
93  private:
94  // To access GetMetrics().
96  friend class OrcaService;
97 
98  struct BackendMetricDataState;
99 
100  // No direct creation, use the factory method Create() above.
102 
103  // Updates the metric state by applying `updater` to the data and incrementing
104  // the sequence number.
105  void UpdateBackendMetricDataState(
106  std::function<void(grpc_core::BackendMetricData*)> updater);
107 
108  grpc_core::BackendMetricData GetMetrics() const;
109  // Returned metric data is guaranteed to be identical between two calls if the
110  // sequence numbers match.
111  std::shared_ptr<const BackendMetricDataState> GetMetricsIfChanged() const;
112 
113  mutable grpc::internal::Mutex mu_;
114  std::shared_ptr<const BackendMetricDataState> metric_state_
115  ABSL_GUARDED_BY(mu_);
116 };
117 
118 } // namespace experimental
119 } // namespace grpc
120 
121 #endif // GRPCPP_EXT_SERVER_METRIC_RECORDER_H
grpc::string_ref
This class is a non owning reference to a string.
Definition: string_ref.h:41
grpc::experimental::ServerMetricRecorder
Records server wide metrics to be reported to the client.
Definition: server_metric_recorder.h:42
grpc
An Alarm posts the user-provided tag to its associated completion queue or invokes the user-provided ...
Definition: alarm.h:33
grpc_core
Definition: grpc_audit_logging.h:31
grpc::experimental::ServerMetricRecorder::SetCpuUtilization
void SetCpuUtilization(double value)
Records the server CPU utilization in the range [0, infy).
grpc::experimental::ServerMetricRecorder::Create
static std::unique_ptr< ServerMetricRecorder > Create()
grpc::experimental::ServerMetricRecorder::SetAllNamedUtilization
void SetAllNamedUtilization(std::map< string_ref, double > named_utilization)
Replaces all named resource utilization values.
grpc::experimental::ServerMetricRecorder::BackendMetricState
friend class grpc::BackendMetricState
Definition: server_metric_recorder.h:95
grpc::experimental::ServerMetricRecorder::ClearNamedUtilization
void ClearNamedUtilization(string_ref name)
Clears a named utilization value if exists.
grpc::experimental::ServerMetricRecorder::ClearQps
void ClearQps()
Clears number of queries per second to the server if recorded.
grpc::experimental::ServerMetricRecorder::SetMemoryUtilization
void SetMemoryUtilization(double value)
Records the server memory utilization in the range [0, 1].
grpc::experimental::ServerMetricRecorder::ClearApplicationUtilization
void ClearApplicationUtilization()
Clears the application specific utilization if recorded.
grpc::experimental::ServerMetricRecorder::SetNamedUtilization
void SetNamedUtilization(string_ref name, double value)
Records a named resource utilization value in the range [0, 1].
grpc::experimental::ServerMetricRecorder::ClearCpuUtilization
void ClearCpuUtilization()
Clears the server CPU utilization if recorded.
grpc::experimental::ServerMetricRecorder::SetApplicationUtilization
void SetApplicationUtilization(double value)
Records the application specific utilization in the range [0, infy].
grpc::experimental::ServerMetricRecorder::SetQps
void SetQps(double value)
Records number of queries per second to the server in the range [0, infy).
grpc::experimental::ServerMetricRecorder::OrcaService
friend class OrcaService
Definition: server_metric_recorder.h:96
grpc::internal::Mutex
Definition: sync.h:58
sync.h
grpc::experimental::ServerMetricRecorder::SetEps
void SetEps(double value)
Records number of errors per second to the server in the range [0, infy).
string_ref.h
grpc::experimental::ServerMetricRecorder::ClearEps
void ClearEps()
Clears number of errors per second to the server if recorded.
grpc::experimental::ServerMetricRecorder::ClearMemoryUtilization
void ClearMemoryUtilization()
Clears the server memory utilization if recorded.