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 private:
31 // Pointer to service name from its metadata is used as a unique service ID
32 using ServiceId = const char*;
33
34 void ExtendStatistics(utils::statistics::Writer& writer);
35
36 // std::equal_to<const char*> specialized in arcadia/util/str_stl.h
37 // so we need to define our own comparer to use it in map and avoid
38 // specialization after instantiation problem
39 struct ServiceIdComparer final {
40 bool operator()(ServiceId lhs, ServiceId rhs) const { return lhs == rhs; }
41 };
42
43 std::unordered_map<ServiceId, ugrpc::impl::ServiceStatistics,
44 std::hash<ServiceId>, ServiceIdComparer>
45 service_statistics_;
46 engine::SharedMutex mutex_;
47
48 utils::statistics::Entry statistics_holder_;
49};
50
51} // namespace ugrpc::impl
52
53USERVER_NAMESPACE_END