xref: /aosp_15_r20/external/grpc-grpc/src/core/load_balancing/grpclb/grpclb_client_stats.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 //
2 //
3 // Copyright 2017 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_SRC_CORE_LOAD_BALANCING_GRPCLB_GRPCLB_CLIENT_STATS_H
20 #define GRPC_SRC_CORE_LOAD_BALANCING_GRPCLB_GRPCLB_CLIENT_STATS_H
21 
22 #include <grpc/support/port_platform.h>
23 
24 #include <stdint.h>
25 
26 #include <memory>
27 #include <utility>
28 
29 #include "absl/base/thread_annotations.h"
30 #include "absl/container/inlined_vector.h"
31 
32 #include <grpc/support/atm.h>
33 
34 #include "src/core/lib/gprpp/memory.h"
35 #include "src/core/lib/gprpp/ref_counted.h"
36 #include "src/core/lib/gprpp/sync.h"
37 
38 namespace grpc_core {
39 
40 class GrpcLbClientStats final : public RefCounted<GrpcLbClientStats> {
41  public:
42   struct DropTokenCount {
43     UniquePtr<char> token;
44     int64_t count;
45 
DropTokenCountDropTokenCount46     DropTokenCount(UniquePtr<char> token, int64_t count)
47         : token(std::move(token)), count(count) {}
48   };
49 
50   typedef absl::InlinedVector<DropTokenCount, 10> DroppedCallCounts;
51 
52   void AddCallStarted();
53   void AddCallFinished(bool finished_with_client_failed_to_send,
54                        bool finished_known_received);
55 
56   void AddCallDropped(const char* token);
57 
58   void Get(int64_t* num_calls_started, int64_t* num_calls_finished,
59            int64_t* num_calls_finished_with_client_failed_to_send,
60            int64_t* num_calls_finished_known_received,
61            std::unique_ptr<DroppedCallCounts>* drop_token_counts);
62 
63   // A destruction function to use as the user_data key when attaching
64   // client stats to a grpc_mdelem.
Destroy(void * arg)65   static void Destroy(void* arg) {
66     static_cast<GrpcLbClientStats*>(arg)->Unref();
67   }
68 
69  private:
70   gpr_atm num_calls_started_ = 0;
71   gpr_atm num_calls_finished_ = 0;
72   gpr_atm num_calls_finished_with_client_failed_to_send_ = 0;
73   gpr_atm num_calls_finished_known_received_ = 0;
74   Mutex drop_count_mu_;  // Guards drop_token_counts_.
75   std::unique_ptr<DroppedCallCounts> drop_token_counts_
76       ABSL_GUARDED_BY(drop_count_mu_);
77 };
78 
79 }  // namespace grpc_core
80 
81 #endif  // GRPC_SRC_CORE_LOAD_BALANCING_GRPCLB_GRPCLB_CLIENT_STATS_H
82