xds_client_stats.h
Go to the documentation of this file.
1 /*
2  *
3  * Copyright 2018 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 GRPC_CORE_EXT_XDS_XDS_CLIENT_STATS_H
20 #define GRPC_CORE_EXT_XDS_XDS_CLIENT_STATS_H
21 
23 
24 #include <atomic>
25 #include <cstdint>
26 #include <map>
27 #include <string>
28 #include <utility>
29 
30 #include "absl/base/thread_annotations.h"
31 #include "absl/strings/str_format.h"
32 #include "absl/strings/string_view.h"
33 
39 
40 namespace grpc_core {
41 
42 // Forward declaration to avoid circular dependency.
43 class XdsClient;
44 
45 // Locality name.
46 class XdsLocalityName : public RefCounted<XdsLocalityName> {
47  public:
48  struct Less {
49  bool operator()(const XdsLocalityName* lhs,
50  const XdsLocalityName* rhs) const {
51  if (lhs == nullptr || rhs == nullptr) return QsortCompare(lhs, rhs);
52  return lhs->Compare(*rhs) < 0;
53  }
54 
56  const RefCountedPtr<XdsLocalityName>& rhs) const {
57  return (*this)(lhs.get(), rhs.get());
58  }
59  };
60 
62  : region_(std::move(region)),
63  zone_(std::move(zone)),
65 
66  bool operator==(const XdsLocalityName& other) const {
67  return region_ == other.region_ && zone_ == other.zone_ &&
68  sub_zone_ == other.sub_zone_;
69  }
70 
71  bool operator!=(const XdsLocalityName& other) const {
72  return !(*this == other);
73  }
74 
75  int Compare(const XdsLocalityName& other) const {
76  int cmp_result = region_.compare(other.region_);
77  if (cmp_result != 0) return cmp_result;
78  cmp_result = zone_.compare(other.zone_);
79  if (cmp_result != 0) return cmp_result;
80  return sub_zone_.compare(other.sub_zone_);
81  }
82 
83  const std::string& region() const { return region_; }
84  const std::string& zone() const { return zone_; }
85  const std::string& sub_zone() const { return sub_zone_; }
86 
88  if (human_readable_string_.empty()) {
90  absl::StrFormat("{region=\"%s\", zone=\"%s\", sub_zone=\"%s\"}",
92  }
94  }
95 
96  private:
101 };
102 
103 // Drop stats for an xds cluster.
104 class XdsClusterDropStats : public RefCounted<XdsClusterDropStats> {
105  public:
106  // The total number of requests dropped for any reason is the sum of
107  // uncategorized_drops, and dropped_requests map.
109  struct Snapshot {
111  // The number of requests dropped for the specific drop categories
112  // outlined in the drop_overloads field in the EDS response.
114 
115  Snapshot& operator+=(const Snapshot& other) {
117  for (const auto& p : other.categorized_drops) {
118  categorized_drops[p.first] += p.second;
119  }
120  return *this;
121  }
122 
123  bool IsZero() const {
124  if (uncategorized_drops != 0) return false;
125  for (const auto& p : categorized_drops) {
126  if (p.second != 0) return false;
127  }
128  return true;
129  }
130  };
131 
133  const XdsBootstrap::XdsServer& lrs_server,
136  ~XdsClusterDropStats() override;
137 
138  // Returns a snapshot of this instance and resets all the counters.
139  Snapshot GetSnapshotAndReset();
140 
141  void AddUncategorizedDrops();
142  void AddCallDropped(const std::string& category);
143 
144  private:
149  std::atomic<uint64_t> uncategorized_drops_{0};
150  // Protects categorized_drops_. A mutex is necessary because the length of
151  // dropped_requests can be accessed by both the picker (from data plane
152  // mutex) and the load reporting thread (from the control plane combiner).
154  CategorizedDropsMap categorized_drops_ ABSL_GUARDED_BY(mu_);
155 };
156 
157 // Locality stats for an xds cluster.
158 class XdsClusterLocalityStats : public RefCounted<XdsClusterLocalityStats> {
159  public:
160  struct BackendMetric {
163 
168  return *this;
169  }
170 
171  bool IsZero() const {
173  }
174  };
175 
176  struct Snapshot {
181  std::map<std::string, BackendMetric> backend_metrics;
182 
183  Snapshot& operator+=(const Snapshot& other) {
188  for (const auto& p : other.backend_metrics) {
189  backend_metrics[p.first] += p.second;
190  }
191  return *this;
192  }
193 
194  bool IsZero() const {
197  return false;
198  }
199  for (const auto& p : backend_metrics) {
200  if (!p.second.IsZero()) return false;
201  }
202  return true;
203  }
204  };
205 
211  ~XdsClusterLocalityStats() override;
212 
213  // Returns a snapshot of this instance and resets all the counters.
214  Snapshot GetSnapshotAndReset();
215 
216  void AddCallStarted();
217  void AddCallFinished(bool fail = false);
218 
219  private:
225 
226  std::atomic<uint64_t> total_successful_requests_{0};
227  std::atomic<uint64_t> total_requests_in_progress_{0};
228  std::atomic<uint64_t> total_error_requests_{0};
229  std::atomic<uint64_t> total_issued_requests_{0};
230 
231  // Protects backend_metrics_. A mutex is necessary because the length of
232  // backend_metrics_ can be accessed by both the callback intercepting the
233  // call's recv_trailing_metadata (not from the control plane work serializer)
234  // and the load reporting thread (from the control plane work serializer).
236  std::map<std::string, BackendMetric> backend_metrics_
238 };
239 
240 } // namespace grpc_core
241 
242 #endif /* GRPC_CORE_EXT_XDS_XDS_CLIENT_STATS_H */
grpc_core::XdsLocalityName::sub_zone
const std::string & sub_zone() const
Definition: xds_client_stats.h:85
grpc_core::XdsClusterLocalityStats::Snapshot::IsZero
bool IsZero() const
Definition: xds_client_stats.h:194
grpc_core::XdsClusterDropStats::AddCallDropped
void AddCallDropped(const std::string &category)
Definition: xds_client_stats.cc:87
absl::StrFormat
ABSL_MUST_USE_RESULT std::string StrFormat(const FormatSpec< Args... > &format, const Args &... args)
Definition: abseil-cpp/absl/strings/str_format.h:338
grpc_core::XdsLocalityName::region
const std::string & region() const
Definition: xds_client_stats.h:83
grpc_core::XdsClusterDropStats::eds_service_name_
absl::string_view eds_service_name_
Definition: xds_client_stats.h:148
grpc_core::RefCountedPtr::get
T * get() const
Definition: ref_counted_ptr.h:146
grpc_core::XdsLocalityName::Less::operator()
bool operator()(const RefCountedPtr< XdsLocalityName > &lhs, const RefCountedPtr< XdsLocalityName > &rhs) const
Definition: xds_client_stats.h:55
grpc_core
Definition: call_metric_recorder.h:31
cluster_name
std::string cluster_name
Definition: xds_cluster_resolver.cc:91
grpc_core::XdsClusterLocalityStats::BackendMetric::num_requests_finished_with_metric
uint64_t num_requests_finished_with_metric
Definition: xds_client_stats.h:161
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
grpc_core::XdsClusterDropStats::cluster_name_
absl::string_view cluster_name_
Definition: xds_client_stats.h:147
grpc_core::XdsLocalityName::zone
const std::string & zone() const
Definition: xds_client_stats.h:84
grpc_core::XdsClusterLocalityStats::AddCallFinished
void AddCallFinished(bool fail=false)
Definition: xds_client_stats.cc:152
useful.h
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
grpc_core::XdsClusterLocalityStats::~XdsClusterLocalityStats
~XdsClusterLocalityStats() override
Definition: xds_client_stats.cc:118
grpc_core::XdsClusterLocalityStats::Snapshot::total_error_requests
uint64_t total_error_requests
Definition: xds_client_stats.h:179
grpc_core::XdsLocalityName::sub_zone_
std::string sub_zone_
Definition: xds_client_stats.h:99
grpc_core::XdsClusterLocalityStats::lrs_server_
const XdsBootstrap::XdsServer & lrs_server_
Definition: xds_client_stats.h:221
setup.name
name
Definition: setup.py:542
grpc_core::XdsLocalityName::operator==
bool operator==(const XdsLocalityName &other) const
Definition: xds_client_stats.h:66
grpc_core::XdsClusterLocalityStats::BackendMetric::operator+=
BackendMetric & operator+=(const BackendMetric &other)
Definition: xds_client_stats.h:164
grpc_core::XdsClusterDropStats::Snapshot::uncategorized_drops
uint64_t uncategorized_drops
Definition: xds_client_stats.h:110
map
zval * map
Definition: php/ext/google/protobuf/encode_decode.c:480
grpc_core::XdsLocalityName
Definition: xds_client_stats.h:46
grpc_core::XdsClusterDropStats::AddUncategorizedDrops
void AddUncategorizedDrops()
Definition: xds_client_stats.cc:83
grpc_core::XdsClusterLocalityStats::XdsClusterLocalityStats
XdsClusterLocalityStats(RefCountedPtr< XdsClient > xds_client, const XdsBootstrap::XdsServer &lrs_server_, absl::string_view cluster_name, absl::string_view eds_service_name, RefCountedPtr< XdsLocalityName > name)
Definition: xds_client_stats.cc:96
grpc_core::XdsClusterDropStats::Snapshot::categorized_drops
CategorizedDropsMap categorized_drops
Definition: xds_client_stats.h:113
grpc_core::XdsClusterDropStats::Snapshot::IsZero
bool IsZero() const
Definition: xds_client_stats.h:123
grpc_core::XdsLocalityName::human_readable_string_
std::string human_readable_string_
Definition: xds_client_stats.h:100
grpc_core::XdsClusterLocalityStats::Snapshot::total_successful_requests
uint64_t total_successful_requests
Definition: xds_client_stats.h:177
grpc_core::XdsClusterDropStats::XdsClusterDropStats
XdsClusterDropStats(RefCountedPtr< XdsClient > xds_client, const XdsBootstrap::XdsServer &lrs_server, absl::string_view cluster_name, absl::string_view eds_service_name)
Definition: xds_client_stats.cc:43
grpc_core::RefCountedPtr
Definition: ref_counted_ptr.h:35
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc_core::XdsClusterLocalityStats::Snapshot
Definition: xds_client_stats.h:176
grpc_core::XdsLocalityName::region_
std::string region_
Definition: xds_client_stats.h:97
grpc_core::XdsClusterDropStats::Snapshot
Definition: xds_client_stats.h:109
grpc_core::XdsLocalityName::Less::operator()
bool operator()(const XdsLocalityName *lhs, const XdsLocalityName *rhs) const
Definition: xds_client_stats.h:49
xds_bootstrap.h
grpc_core::fail
Poll< absl::StatusOr< std::tuple< T... > > > fail()
Definition: try_join_test.cc:45
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
grpc_core::XdsClusterLocalityStats::total_successful_requests_
std::atomic< uint64_t > total_successful_requests_
Definition: xds_client_stats.h:226
grpc_core::XdsClusterLocalityStats::Snapshot::total_requests_in_progress
uint64_t total_requests_in_progress
Definition: xds_client_stats.h:178
grpc_core::XdsClusterLocalityStats::Snapshot::backend_metrics
std::map< std::string, BackendMetric > backend_metrics
Definition: xds_client_stats.h:181
grpc_core::XdsClusterLocalityStats::total_issued_requests_
std::atomic< uint64_t > total_issued_requests_
Definition: xds_client_stats.h:229
grpc_core::XdsClusterLocalityStats
Definition: xds_client_stats.h:158
grpc_core::XdsClusterLocalityStats::name_
RefCountedPtr< XdsLocalityName > name_
Definition: xds_client_stats.h:224
grpc_core::RefCounted
Definition: ref_counted.h:280
grpc_core::XdsClusterDropStats::ABSL_GUARDED_BY
CategorizedDropsMap categorized_drops_ ABSL_GUARDED_BY(mu_)
grpc_core::XdsClusterLocalityStats::AddCallStarted
void AddCallStarted()
Definition: xds_client_stats.cc:147
grpc_core::XdsClusterDropStats::CategorizedDropsMap
std::map< std::string, uint64_t > CategorizedDropsMap
Definition: xds_client_stats.h:108
grpc_core::XdsClusterLocalityStats::total_requests_in_progress_
std::atomic< uint64_t > total_requests_in_progress_
Definition: xds_client_stats.h:227
grpc_core::XdsClusterDropStats
Definition: xds_client_stats.h:104
grpc_core::XdsLocalityName::operator!=
bool operator!=(const XdsLocalityName &other) const
Definition: xds_client_stats.h:71
grpc_core::XdsClusterDropStats::GetSnapshotAndReset
Snapshot GetSnapshotAndReset()
Definition: xds_client_stats.cc:75
grpc_core::XdsClusterDropStats::Snapshot::operator+=
Snapshot & operator+=(const Snapshot &other)
Definition: xds_client_stats.h:115
grpc_core::XdsLocalityName::zone_
std::string zone_
Definition: xds_client_stats.h:98
grpc_core::Mutex
Definition: src/core/lib/gprpp/sync.h:61
grpc_core::XdsLocalityName::XdsLocalityName
XdsLocalityName(std::string region, std::string zone, std::string sub_zone)
Definition: xds_client_stats.h:61
grpc_core::XdsBootstrap::XdsServer
Definition: xds_bootstrap.h:52
eds_service_name
std::string eds_service_name
Definition: xds_cluster_resolver.cc:99
grpc_core::XdsClusterLocalityStats::Snapshot::operator+=
Snapshot & operator+=(const Snapshot &other)
Definition: xds_client_stats.h:183
grpc_core::QsortCompare
int QsortCompare(const T &a, const T &b)
Definition: useful.h:95
ref_counted.h
grpc_core::XdsLocalityName::Compare
int Compare(const XdsLocalityName &other) const
Definition: xds_client_stats.h:75
grpc_core::XdsClusterLocalityStats::Snapshot::total_issued_requests
uint64_t total_issued_requests
Definition: xds_client_stats.h:180
grpc_core::XdsClusterDropStats::mu_
Mutex mu_
Definition: xds_client_stats.h:153
grpc_core::XdsClusterLocalityStats::BackendMetric::total_metric_value
double total_metric_value
Definition: xds_client_stats.h:162
std
Definition: grpcpp/impl/codegen/async_unary_call.h:407
grpc_core::XdsClusterDropStats::xds_client_
RefCountedPtr< XdsClient > xds_client_
Definition: xds_client_stats.h:145
grpc_core::XdsClusterLocalityStats::cluster_name_
absl::string_view cluster_name_
Definition: xds_client_stats.h:222
ref_counted_ptr.h
grpc_core::XdsLocalityName::AsHumanReadableString
const std::string & AsHumanReadableString()
Definition: xds_client_stats.h:87
grpc_core::XdsClusterLocalityStats::eds_service_name_
absl::string_view eds_service_name_
Definition: xds_client_stats.h:223
grpc_core::XdsClusterLocalityStats::ABSL_GUARDED_BY
std::map< std::string, BackendMetric > backend_metrics_ ABSL_GUARDED_BY(backend_metrics_mu_)
grpc_core::XdsLocalityName::Less
Definition: xds_client_stats.h:48
grpc_core::XdsClusterDropStats::lrs_server_
const XdsBootstrap::XdsServer & lrs_server_
Definition: xds_client_stats.h:146
grpc_core::XdsClusterDropStats::~XdsClusterDropStats
~XdsClusterDropStats() override
Definition: xds_client_stats.cc:62
grpc_core::XdsClusterLocalityStats::backend_metrics_mu_
Mutex backend_metrics_mu_
Definition: xds_client_stats.h:235
grpc_core::XdsClusterLocalityStats::BackendMetric
Definition: xds_client_stats.h:160
grpc_core::XdsClusterLocalityStats::GetSnapshotAndReset
Snapshot GetSnapshotAndReset()
Definition: xds_client_stats.cc:133
sync.h
grpc_core::XdsClusterDropStats::uncategorized_drops_
std::atomic< uint64_t > uncategorized_drops_
Definition: xds_client_stats.h:149
grpc_core::XdsClusterLocalityStats::total_error_requests_
std::atomic< uint64_t > total_error_requests_
Definition: xds_client_stats.h:228
grpc_core::XdsClusterLocalityStats::BackendMetric::IsZero
bool IsZero() const
Definition: xds_client_stats.h:171
grpc_core::XdsClusterLocalityStats::xds_client_
RefCountedPtr< XdsClient > xds_client_
Definition: xds_client_stats.h:220
port_platform.h


grpc
Author(s):
autogenerated on Thu Mar 13 2025 03:01:55