Debian Codegraph @

src/core/ext/xds/xds_client_stats.cc

raw
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 #include <grpc/support/port_platform.h>
20
21 #include "src/core/ext/xds/xds_client_stats.h"
22
23 #include <grpc/support/log.h>
24
25 #include "src/core/ext/xds/xds_client.h"
26 #include "src/core/lib/debug/trace.h"
27 #include "src/core/lib/gprpp/debug_location.h"
28
29 namespace grpc_core {
30
31 namespace {
32
33 uint64_t GetAndResetCounter(std::atomic<uint64_t>* from) {
34 return from->exchange(0, std::memory_order_relaxed);
35 }
36
37 } // namespace
38
39 //
40 // XdsClusterDropStats
41 //
42
43 XdsClusterDropStats::XdsClusterDropStats(
44 RefCountedPtr<XdsClient> xds_client,
45 const XdsBootstrap::XdsServer& lrs_server, absl::string_view cluster_name,
46 absl::string_view eds_service_name)
47 : RefCounted(GRPC_TRACE_FLAG_ENABLED(grpc_xds_client_refcount_trace)
48 ? "XdsClusterDropStats"
49 : nullptr),
50 xds_client_(std::move(xds_client)),
51 lrs_server_(lrs_server),
52 cluster_name_(cluster_name),
53 eds_service_name_(eds_service_name) {
54 if (GRPC_TRACE_FLAG_ENABLED(grpc_xds_client_trace)) {
55 gpr_log(GPR_INFO, "[xds_client %p] created drop stats %p for {%s, %s, %s}",
56 xds_client_.get(), this, lrs_server_.server_uri().c_str(),
57 std::string(cluster_name_).c_str(),
58 std::string(eds_service_name_).c_str());
59 }
60 }
61
62 XdsClusterDropStats::~XdsClusterDropStats() {
63 if (GRPC_TRACE_FLAG_ENABLED(grpc_xds_client_trace)) {
64 gpr_log(GPR_INFO,
65 "[xds_client %p] destroying drop stats %p for {%s, %s, %s}",
66 xds_client_.get(), this, lrs_server_.server_uri().c_str(),
67 std::string(cluster_name_).c_str(),
68 std::string(eds_service_name_).c_str());
69 }
70 xds_client_->RemoveClusterDropStats(lrs_server_, cluster_name_,
71 eds_service_name_, this);
72 xds_client_.reset(DEBUG_LOCATION, "DropStats");
73 }
74
75 XdsClusterDropStats::Snapshot XdsClusterDropStats::GetSnapshotAndReset() {
76 Snapshot snapshot;
77 snapshot.uncategorized_drops = GetAndResetCounter(&uncategorized_drops_);
78 MutexLock lock(&mu_);
79 snapshot.categorized_drops = std::move(categorized_drops_);
80 return snapshot;
81 }
82
83 void XdsClusterDropStats::AddUncategorizedDrops() {
84 uncategorized_drops_.fetch_add(1);
85 }
86
87 void XdsClusterDropStats::AddCallDropped(const std::string& category) {
88 MutexLock lock(&mu_);
89 ++categorized_drops_[category];
90 }
91
92 //
93 // XdsClusterLocalityStats
94 //
95
96 XdsClusterLocalityStats::XdsClusterLocalityStats(
97 RefCountedPtr<XdsClient> xds_client,
98 const XdsBootstrap::XdsServer& lrs_server, absl::string_view cluster_name,
99 absl::string_view eds_service_name, RefCountedPtr<XdsLocalityName> name)
100 : RefCounted(GRPC_TRACE_FLAG_ENABLED(grpc_xds_client_refcount_trace)
101 ? "XdsClusterLocalityStats"
102 : nullptr),
103 xds_client_(std::move(xds_client)),
104 lrs_server_(lrs_server),
105 cluster_name_(cluster_name),
106 eds_service_name_(eds_service_name),
107 name_(std::move(name)) {
108 if (GRPC_TRACE_FLAG_ENABLED(grpc_xds_client_trace)) {
109 gpr_log(GPR_INFO,
110 "[xds_client %p] created locality stats %p for {%s, %s, %s, %s}",
111 xds_client_.get(), this, lrs_server_.server_uri().c_str(),
112 std::string(cluster_name_).c_str(),
113 std::string(eds_service_name_).c_str(),
114 name_->AsHumanReadableString().c_str());
115 }
116 }
117
118 XdsClusterLocalityStats::~XdsClusterLocalityStats() {
119 if (GRPC_TRACE_FLAG_ENABLED(grpc_xds_client_trace)) {
120 gpr_log(GPR_INFO,
121 "[xds_client %p] destroying locality stats %p for {%s, %s, %s, %s}",
122 xds_client_.get(), this, lrs_server_.server_uri().c_str(),
123 std::string(cluster_name_).c_str(),
124 std::string(eds_service_name_).c_str(),
125 name_->AsHumanReadableString().c_str());
126 }
127 xds_client_->RemoveClusterLocalityStats(lrs_server_, cluster_name_,
128 eds_service_name_, name_, this);
129 xds_client_.reset(DEBUG_LOCATION, "LocalityStats");
130 }
131
132 XdsClusterLocalityStats::Snapshot
133 XdsClusterLocalityStats::GetSnapshotAndReset() {
134 Snapshot snapshot = {
135 GetAndResetCounter(&total_successful_requests_),
136 // Don't reset total_requests_in_progress because it's
137 // not related to a single reporting interval.
138 total_requests_in_progress_.load(std::memory_order_relaxed),
139 GetAndResetCounter(&total_error_requests_),
140 GetAndResetCounter(&total_issued_requests_),
141 {}};
142 MutexLock lock(&backend_metrics_mu_);
143 snapshot.backend_metrics = std::move(backend_metrics_);
144 return snapshot;
145 }
146
147 void XdsClusterLocalityStats::AddCallStarted() {
148 total_issued_requests_.fetch_add(1, std::memory_order_relaxed);
149 total_requests_in_progress_.fetch_add(1, std::memory_order_relaxed);
150 }
151
152 void XdsClusterLocalityStats::AddCallFinished(bool fail) {
153 std::atomic<uint64_t>& to_increment =
154 fail ? total_error_requests_ : total_successful_requests_;
155 to_increment.fetch_add(1, std::memory_order_relaxed);
156 total_requests_in_progress_.fetch_add(-1, std::memory_order_acq_rel);
157 }
158
159 } // namespace grpc_core