userver: userver/ugrpc/impl/statistics_storage.hpp Source File
Loading...
Searching...
No Matches
statistics_storage.hpp
1#pragma once
2
3#include <string_view>
4#include <unordered_map>
5
6#include <userver/engine/shared_mutex.hpp>
7#include <userver/utils/statistics/entry.hpp>
8
9#include <userver/ugrpc/impl/statistics.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace ugrpc::impl {
14
15/// Clients are created on-the-fly, so we must use a separate stable container
16/// for storing their statistics.
17class StatisticsStorage final {
18 public:
19 explicit StatisticsStorage(utils::statistics::Storage& statistics_storage,
20 std::string_view domain);
21
22 StatisticsStorage(const StatisticsStorage&) = delete;
23 StatisticsStorage& operator=(const StatisticsStorage&) = delete;
24
25 ~StatisticsStorage();
26
27 ugrpc::impl::ServiceStatistics& GetServiceStatistics(
28 const ugrpc::impl::StaticServiceMetadata& metadata);
29
30 // Can only be called on StatisticsStorage for gRPC services (not clients).
31 // Can only be called strictly after all the components are loaded.
32 // gRPC services must not be [un]registered during GetStartedRequests().
33 std::uint64_t GetStartedRequests() const;
34
35 private:
36 // Pointer to service name from its metadata is used as a unique service ID
37 using ServiceId = const char*;
38
39 void ExtendStatistics(utils::statistics::Writer& writer);
40
41 // std::equal_to<const char*> specialized in arcadia/util/str_stl.h
42 // so we need to define our own comparer to use it in map and avoid
43 // specialization after instantiation problem
44 struct ServiceIdComparer final {
45 bool operator()(ServiceId lhs, ServiceId rhs) const { return lhs == rhs; }
46 };
47
48 std::unordered_map<ServiceId, ugrpc::impl::ServiceStatistics,
49 std::hash<ServiceId>, ServiceIdComparer>
50 service_statistics_;
51 engine::SharedMutex mutex_;
52
53 utils::statistics::Entry statistics_holder_;
54};
55
56} // namespace ugrpc::impl
57
58USERVER_NAMESPACE_END