Debian Codegraph @

src/cpp/server/orca/call_metric_recorder.cc

raw
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"
27 #include "xds/data/orca/v3/orca_load_report.upb.h"
28
29 #include <grpcpp/ext/call_metric_recorder.h>
30 #include <grpcpp/impl/codegen/sync.h>
31 #include <grpcpp/support/string_ref.h>
32
33 #include "src/core/ext/filters/client_channel/lb_policy/backend_metric_data.h"
34 #include "src/core/lib/resource_quota/arena.h"
35
36 namespace grpc {
37 namespace experimental {
38
39 CallMetricRecorder::CallMetricRecorder(grpc_core::Arena* arena)
40 : backend_metric_data_(arena->New<grpc_core::BackendMetricData>()) {}
41
42 CallMetricRecorder::~CallMetricRecorder() {
43 backend_metric_data_->~BackendMetricData();
44 }
45
46 CallMetricRecorder& CallMetricRecorder::RecordCpuUtilizationMetric(
47 double value) {
48 internal::MutexLock lock(&mu_);
49 backend_metric_data_->cpu_utilization = value;
50 return *this;
51 }
52
53 CallMetricRecorder& CallMetricRecorder::RecordMemoryUtilizationMetric(
54 double value) {
55 internal::MutexLock lock(&mu_);
56 backend_metric_data_->mem_utilization = value;
57 return *this;
58 }
59
60 CallMetricRecorder& CallMetricRecorder::RecordUtilizationMetric(
61 grpc::string_ref name, double value) {
62 internal::MutexLock lock(&mu_);
63 absl::string_view name_sv(name.data(), name.length());
64 backend_metric_data_->utilization[name_sv] = value;
65 return *this;
66 }
67
68 CallMetricRecorder& CallMetricRecorder::RecordRequestCostMetric(
69 grpc::string_ref name, double value) {
70 internal::MutexLock lock(&mu_);
71 absl::string_view name_sv(name.data(), name.length());
72 backend_metric_data_->request_cost[name_sv] = value;
73 return *this;
74 }
75
76 absl::optional<std::string> CallMetricRecorder::CreateSerializedReport() {
77 upb::Arena arena;
78 internal::MutexLock lock(&mu_);
79 bool has_data = backend_metric_data_->cpu_utilization != -1 ||
80 backend_metric_data_->mem_utilization != -1 ||
81 !backend_metric_data_->utilization.empty() ||
82 !backend_metric_data_->request_cost.empty();
83 if (!has_data) {
84 return absl::nullopt;
85 }
86 xds_data_orca_v3_OrcaLoadReport* response =
87 xds_data_orca_v3_OrcaLoadReport_new(arena.ptr());
88 if (backend_metric_data_->cpu_utilization != -1) {
89 xds_data_orca_v3_OrcaLoadReport_set_cpu_utilization(
90 response, backend_metric_data_->cpu_utilization);
91 }
92 if (backend_metric_data_->mem_utilization != -1) {
93 xds_data_orca_v3_OrcaLoadReport_set_mem_utilization(
94 response, backend_metric_data_->mem_utilization);
95 }
96 for (const auto& p : backend_metric_data_->request_cost) {
97 xds_data_orca_v3_OrcaLoadReport_request_cost_set(
98 response,
99 upb_StringView_FromDataAndSize(p.first.data(), p.first.size()),
100 p.second, arena.ptr());
101 }
102 for (const auto& p : backend_metric_data_->utilization) {
103 xds_data_orca_v3_OrcaLoadReport_utilization_set(
104 response,
105 upb_StringView_FromDataAndSize(p.first.data(), p.first.size()),
106 p.second, arena.ptr());
107 }
108 size_t buf_length;
109 char* buf = xds_data_orca_v3_OrcaLoadReport_serialize(response, arena.ptr(),
110 &buf_length);
111 return std::string(buf, buf_length);
112 }
113
114 } // namespace experimental
115 } // namespace grpc