3 * Copyright 2018 gRPC authors.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <grpc/support/port_platform.h>
21 #include "src/core/ext/xds/xds_client_stats.h"
25 #include <grpc/support/atm.h>
26 #include <grpc/support/string_util.h>
28 #include "src/core/ext/xds/xds_client.h"
34 uint64_t GetAndResetCounter(Atomic<uint64_t>* from) {
35 return from->Exchange(0, MemoryOrder::RELAXED);
41 // XdsClusterDropStats
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) {}
53 XdsClusterDropStats::~XdsClusterDropStats() {
54 xds_client_->RemoveClusterDropStats(lrs_server_name_, cluster_name_,
55 eds_service_name_, this);
56 xds_client_.reset(DEBUG_LOCATION, "DropStats");
59 XdsClusterDropStats::Snapshot XdsClusterDropStats::GetSnapshotAndReset() {
61 snapshot.uncategorized_drops = GetAndResetCounter(&uncategorized_drops_);
63 snapshot.categorized_drops = std::move(categorized_drops_);
67 void XdsClusterDropStats::AddUncategorizedDrops() {
68 uncategorized_drops_.FetchAdd(1);
71 void XdsClusterDropStats::AddCallDropped(const std::string& category) {
73 ++categorized_drops_[category];
77 // XdsClusterLocalityStats
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)) {}
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");
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_);
109 void XdsClusterLocalityStats::AddCallStarted() {
110 total_issued_requests_.FetchAdd(1, MemoryOrder::RELAXED);
111 total_requests_in_progress_.FetchAdd(1, MemoryOrder::RELAXED);
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);
121 } // namespace grpc_core