call_metric_recorder.cc
Go to the documentation of this file.
1 //
2 // Copyright 2022 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 #include <stddef.h>
18 
19 #include <map>
20 #include <string>
21 #include <utility>
22 
23 #include "absl/strings/string_view.h"
24 #include "absl/types/optional.h"
25 #include "upb/upb.h"
26 #include "upb/upb.hpp"
28 
31 #include <grpcpp/support/config.h>
33 
36 
37 namespace grpc {
38 namespace experimental {
39 
41  : backend_metric_data_(arena->New<grpc_core::BackendMetricData>()) {}
42 
44  backend_metric_data_->~BackendMetricData();
45 }
46 
48  double value) {
49  internal::MutexLock lock(&mu_);
50  backend_metric_data_->cpu_utilization = value;
51  return *this;
52 }
53 
55  double value) {
56  internal::MutexLock lock(&mu_);
57  backend_metric_data_->mem_utilization = value;
58  return *this;
59 }
60 
62  grpc::string_ref name, double value) {
63  internal::MutexLock lock(&mu_);
64  absl::string_view name_sv(name.data(), name.length());
65  backend_metric_data_->utilization[name_sv] = value;
66  return *this;
67 }
68 
70  grpc::string_ref name, double value) {
71  internal::MutexLock lock(&mu_);
72  absl::string_view name_sv(name.data(), name.length());
73  backend_metric_data_->request_cost[name_sv] = value;
74  return *this;
75 }
76 
79  internal::MutexLock lock(&mu_);
80  bool has_data = backend_metric_data_->cpu_utilization != -1 ||
81  backend_metric_data_->mem_utilization != -1 ||
82  !backend_metric_data_->utilization.empty() ||
83  !backend_metric_data_->request_cost.empty();
84  if (!has_data) {
85  return absl::nullopt;
86  }
89  if (backend_metric_data_->cpu_utilization != -1) {
91  response, backend_metric_data_->cpu_utilization);
92  }
93  if (backend_metric_data_->mem_utilization != -1) {
95  response, backend_metric_data_->mem_utilization);
96  }
97  for (const auto& p : backend_metric_data_->request_cost) {
99  response,
100  upb_StringView_FromDataAndSize(p.first.data(), p.first.size()),
101  p.second, arena.ptr());
102  }
103  for (const auto& p : backend_metric_data_->utilization) {
105  response,
106  upb_StringView_FromDataAndSize(p.first.data(), p.first.size()),
107  p.second, arena.ptr());
108  }
109  size_t buf_length;
111  &buf_length);
112  return std::string(buf, buf_length);
113 }
114 
115 } // namespace experimental
116 } // namespace grpc
grpc::string_ref
Definition: grpcpp/impl/codegen/string_ref.h:43
orca_load_report.upb.h
grpc::experimental::CallMetricRecorder::RecordRequestCostMetric
CallMetricRecorder & RecordRequestCostMetric(string_ref name, double value)
Definition: call_metric_recorder.cc:69
grpc
Definition: grpcpp/alarm.h:33
grpc_core
Definition: call_metric_recorder.h:31
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
arena.h
New
T * New(Args &&... args)
Definition: third_party/boringssl-with-bazel/src/ssl/internal.h:195
setup.name
name
Definition: setup.py:542
grpc_core::Arena
Definition: src/core/lib/resource_quota/arena.h:45
grpc::internal::MutexLock
Definition: include/grpcpp/impl/codegen/sync.h:86
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
grpc::experimental::CallMetricRecorder::mu_
internal::Mutex mu_
Definition: call_metric_recorder.h:86
backend_metric_data.h
sync.h
xds_data_orca_v3_OrcaLoadReport
struct xds_data_orca_v3_OrcaLoadReport xds_data_orca_v3_OrcaLoadReport
Definition: orca_load_report.upb.h:26
xds_data_orca_v3_OrcaLoadReport_set_cpu_utilization
UPB_INLINE void xds_data_orca_v3_OrcaLoadReport_set_cpu_utilization(xds_data_orca_v3_OrcaLoadReport *msg, double value)
Definition: orca_load_report.upb.h:115
upb.h
absl::optional< std::string >
grpc::experimental::CallMetricRecorder::RecordCpuUtilizationMetric
CallMetricRecorder & RecordCpuUtilizationMetric(double value)
Definition: call_metric_recorder.cc:47
grpc::experimental::CallMetricRecorder::~CallMetricRecorder
~CallMetricRecorder()
Definition: call_metric_recorder.cc:43
call_metric_recorder.h
grpc::experimental::CallMetricRecorder
Definition: call_metric_recorder.h:52
config.h
grpc::experimental::CallMetricRecorder::RecordUtilizationMetric
CallMetricRecorder & RecordUtilizationMetric(string_ref name, double value)
Definition: call_metric_recorder.cc:61
xds_data_orca_v3_OrcaLoadReport_serialize
UPB_INLINE char * xds_data_orca_v3_OrcaLoadReport_serialize(const xds_data_orca_v3_OrcaLoadReport *msg, upb_Arena *arena, size_t *len)
Definition: orca_load_report.upb.h:59
value
const char * value
Definition: hpack_parser_table.cc:165
grpc::experimental::CallMetricRecorder::CallMetricRecorder
CallMetricRecorder(grpc_core::Arena *arena)
Definition: call_metric_recorder.cc:40
upb::Arena
Definition: upb.hpp:68
upb.hpp
grpc::experimental::CallMetricRecorder::RecordMemoryUtilizationMetric
CallMetricRecorder & RecordMemoryUtilizationMetric(double value)
Definition: call_metric_recorder.cc:54
asyncio_get_stats.response
response
Definition: asyncio_get_stats.py:28
backend_metric_data_
BackendMetricData backend_metric_data_
Definition: oob_backend_metric.cc:278
upb_StringView_FromDataAndSize
UPB_INLINE upb_StringView upb_StringView_FromDataAndSize(const char *data, size_t size)
Definition: upb/upb/upb.h:77
grpc::experimental::CallMetricRecorder::CreateSerializedReport
absl::optional< std::string > CreateSerializedReport()
Definition: call_metric_recorder.cc:77
xds_data_orca_v3_OrcaLoadReport_new
UPB_INLINE xds_data_orca_v3_OrcaLoadReport * xds_data_orca_v3_OrcaLoadReport_new(upb_Arena *arena)
Definition: orca_load_report.upb.h:37
xds_data_orca_v3_OrcaLoadReport_set_mem_utilization
UPB_INLINE void xds_data_orca_v3_OrcaLoadReport_set_mem_utilization(xds_data_orca_v3_OrcaLoadReport *msg, double value)
Definition: orca_load_report.upb.h:118
xds_data_orca_v3_OrcaLoadReport_utilization_set
UPB_INLINE bool xds_data_orca_v3_OrcaLoadReport_utilization_set(xds_data_orca_v3_OrcaLoadReport *msg, upb_StringView key, double val, upb_Arena *a)
Definition: orca_load_report.upb.h:135
string_ref.h
xds_data_orca_v3_OrcaLoadReport_request_cost_set
UPB_INLINE bool xds_data_orca_v3_OrcaLoadReport_request_cost_set(xds_data_orca_v3_OrcaLoadReport *msg, upb_StringView key, double val, upb_Arena *a)
Definition: orca_load_report.upb.h:125


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:51