1 //
2 // Copyright 2024 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 "src/core/xds/xds_client/xds_backend_metric_propagation.h"
18
19 #include <vector>
20
21 #include "absl/strings/str_cat.h"
22 #include "absl/strings/str_join.h"
23 #include "src/core/util/useful.h"
24
25 namespace grpc_core {
26
AsString() const27 std::string BackendMetricPropagation::AsString() const {
28 std::vector<std::string> parts;
29 if (propagation_bits & kCpuUtilization) parts.push_back("cpu_utilization");
30 if (propagation_bits & kMemUtilization) parts.push_back("mem_utilization");
31 if (propagation_bits & kApplicationUtilization) {
32 parts.push_back("application_utilization");
33 }
34 if (propagation_bits & kNamedMetricsAll) {
35 parts.push_back("named_metrics.*");
36 } else {
37 // Output keys in sorted order for consistency.
38 std::vector<absl::string_view> keys(named_metric_keys.begin(),
39 named_metric_keys.end());
40 std::sort(keys.begin(), keys.end());
41 for (const auto& key : keys) {
42 parts.push_back(absl::StrCat("named_metrics.", key));
43 }
44 }
45 return absl::StrCat("{", absl::StrJoin(parts, ","), "}");
46 }
47
operator <(const BackendMetricPropagation & other) const48 bool BackendMetricPropagation::operator<(
49 const BackendMetricPropagation& other) const {
50 int c = QsortCompare(propagation_bits, other.propagation_bits);
51 if (c != 0) return c == -1;
52 auto other_it = other.named_metric_keys.begin();
53 for (auto it = named_metric_keys.begin(); it != named_metric_keys.end();
54 ++it) {
55 if (other_it == other.named_metric_keys.end()) return false;
56 c = QsortCompare(*it, *other_it);
57 if (c != 0) return c == -1;
58 ++other_it;
59 }
60 return false;
61 }
62
63 } // namespace grpc_core
64