Imported Upstream version 1.33.1
[platform/upstream/grpc.git] / src / core / ext / xds / xds_client_stats.cc
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 <string.h>
24
25 #include <grpc/support/atm.h>
26 #include <grpc/support/string_util.h>
27
28 #include "src/core/ext/xds/xds_client.h"
29
30 namespace grpc_core {
31
32 namespace {
33
34 uint64_t GetAndResetCounter(Atomic<uint64_t>* from) {
35   return from->Exchange(0, MemoryOrder::RELAXED);
36 }
37
38 }  // namespace
39
40 //
41 // XdsClusterDropStats
42 //
43
44 XdsClusterDropStats::XdsClusterDropStats(RefCountedPtr<XdsClient> xds_client,
45                                          absl::string_view lrs_server_name,
46                                          absl::string_view cluster_name,
47                                          absl::string_view eds_service_name)
48     : xds_client_(std::move(xds_client)),
49       lrs_server_name_(lrs_server_name),
50       cluster_name_(cluster_name),
51       eds_service_name_(eds_service_name) {}
52
53 XdsClusterDropStats::~XdsClusterDropStats() {
54   xds_client_->RemoveClusterDropStats(lrs_server_name_, cluster_name_,
55                                       eds_service_name_, this);
56   xds_client_.reset(DEBUG_LOCATION, "DropStats");
57 }
58
59 XdsClusterDropStats::Snapshot XdsClusterDropStats::GetSnapshotAndReset() {
60   Snapshot snapshot;
61   snapshot.uncategorized_drops = GetAndResetCounter(&uncategorized_drops_);
62   MutexLock lock(&mu_);
63   snapshot.categorized_drops = std::move(categorized_drops_);
64   return snapshot;
65 }
66
67 void XdsClusterDropStats::AddUncategorizedDrops() {
68   uncategorized_drops_.FetchAdd(1);
69 }
70
71 void XdsClusterDropStats::AddCallDropped(const std::string& category) {
72   MutexLock lock(&mu_);
73   ++categorized_drops_[category];
74 }
75
76 //
77 // XdsClusterLocalityStats
78 //
79
80 XdsClusterLocalityStats::XdsClusterLocalityStats(
81     RefCountedPtr<XdsClient> xds_client, absl::string_view lrs_server_name,
82     absl::string_view cluster_name, absl::string_view eds_service_name,
83     RefCountedPtr<XdsLocalityName> name)
84     : xds_client_(std::move(xds_client)),
85       lrs_server_name_(lrs_server_name),
86       cluster_name_(cluster_name),
87       eds_service_name_(eds_service_name),
88       name_(std::move(name)) {}
89
90 XdsClusterLocalityStats::~XdsClusterLocalityStats() {
91   xds_client_->RemoveClusterLocalityStats(lrs_server_name_, cluster_name_,
92                                           eds_service_name_, name_, this);
93   xds_client_.reset(DEBUG_LOCATION, "LocalityStats");
94 }
95
96 XdsClusterLocalityStats::Snapshot
97 XdsClusterLocalityStats::GetSnapshotAndReset() {
98   Snapshot snapshot = {GetAndResetCounter(&total_successful_requests_),
99                        // Don't reset total_requests_in_progress because it's
100                        // not related to a single reporting interval.
101                        total_requests_in_progress_.Load(MemoryOrder::RELAXED),
102                        GetAndResetCounter(&total_error_requests_),
103                        GetAndResetCounter(&total_issued_requests_)};
104   MutexLock lock(&backend_metrics_mu_);
105   snapshot.backend_metrics = std::move(backend_metrics_);
106   return snapshot;
107 }
108
109 void XdsClusterLocalityStats::AddCallStarted() {
110   total_issued_requests_.FetchAdd(1, MemoryOrder::RELAXED);
111   total_requests_in_progress_.FetchAdd(1, MemoryOrder::RELAXED);
112 }
113
114 void XdsClusterLocalityStats::AddCallFinished(bool fail) {
115   Atomic<uint64_t>& to_increment =
116       fail ? total_error_requests_ : total_successful_requests_;
117   to_increment.FetchAdd(1, MemoryOrder::RELAXED);
118   total_requests_in_progress_.FetchAdd(-1, MemoryOrder::ACQ_REL);
119 }
120
121 }  // namespace grpc_core