userver: userver/storages/postgres/statistics.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
statistics.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/storages/postgres/statistics.hpp
4/// @brief Statistics helpers
5
6#include <string>
7#include <unordered_map>
8#include <vector>
9
10#include <userver/storages/postgres/detail/time_types.hpp>
11
12#include <userver/congestion_control/controllers/linear.hpp>
13#include <userver/utils/statistics/min_max_avg.hpp>
14#include <userver/utils/statistics/percentile.hpp>
15#include <userver/utils/statistics/rate_counter.hpp>
16#include <userver/utils/statistics/recentperiod.hpp>
17#include <userver/utils/statistics/relaxed_counter.hpp>
18#include <userver/utils/statistics/writer.hpp>
19
20USERVER_NAMESPACE_BEGIN
21
22namespace storages::postgres {
23
24/// @brief Template transaction statistics storage
25template <typename Counter, typename PercentileAccumulator>
27 /// Number of transactions started
28 Counter total = 0;
29 /// Number of transactions committed
30 Counter commit_total = 0;
31 /// Number of transactions rolled back
32 Counter rollback_total = 0;
33 /// Number of out-of-transaction executions
34 Counter out_of_trx_total = 0;
35 /// Number of parsed queries
36 Counter parse_total = 0;
37 /// Number of query executions
38 Counter execute_total = 0;
39 /// Total number of replies
40 Counter reply_total = 0;
41 /// Number of portal bind operations
43 /// Error during query execution
45 /// Timeout while executing query
46 Counter execute_timeout = 0;
47 /// Duplicate prepared statements
48 /// This is not a hard error, the prepared statements are quite reusable due
49 /// to pretty uniqueness of names. Nevertheless we would like to see them to
50 /// diagnose certain kinds of problems
52
53 // TODO pick reasonable resolution for transaction
54 // execution times
55 /// Transaction overall execution time distribution
56 PercentileAccumulator total_percentile;
57 /// Transaction aggregated query execution time distribution
58 PercentileAccumulator busy_percentile;
59 /// Transaction wait for pool time (difference between trx_start_time and
60 /// work_start_time)
61 PercentileAccumulator wait_start_percentile;
62 /// Transaction wait for pool time (difference between last_execute_finish and
63 /// trx_end_time)
64 PercentileAccumulator wait_end_percentile;
65 /// Return to pool percentile (difference between trx_end_time and time the
66 /// connection has been returned to the pool)
67 PercentileAccumulator return_to_pool_percentile;
68};
69
70/// @brief Template connection statistics storage
71template <typename Counter, typename MmaAccumulator>
73 /// Number of connections opened
74 Counter open_total = 0;
75 /// Number of connections dropped
76 Counter drop_total = 0;
77 /// Number of active connections
78 Counter active = 0;
79 /// Number of connections in use
80 Counter used = 0;
81 /// Number of maximum allowed connections
82 Counter maximum = 0;
83 /// Number of waiting requests
84 Counter waiting = 0;
85 /// Error during connection
86 Counter error_total = 0;
87 /// Connection timeouts (timeouts while connecting)
88 Counter error_timeout = 0;
89 /// Number of maximum allowed waiting requests
90 Counter max_queue_size = 0;
91
92 /// Prepared statements count min-max-avg
93 MmaAccumulator prepared_statements;
94};
95
96/// @brief Template instance topology statistics storage
97template <typename MmaAccumulator>
99 /// Roundtrip time min-max-avg
100 MmaAccumulator roundtrip_time;
101 /// Replication lag min-max-avg
102 MmaAccumulator replication_lag;
103};
104
105/// @brief Template instance statistics storage
106template <typename Counter, typename PercentileAccumulator, typename MmaAccumulator>
108 /// Connection statistics
109 ConnectionStatistics<Counter, MmaAccumulator> connection;
110 /// Transaction statistics
111 TransactionStatistics<Counter, PercentileAccumulator> transaction;
112 /// Topology statistics
114 /// Error caused by pool exhaustion
116 /// Error caused by queue size overflow
117 Counter queue_size_errors = 0;
118 /// Connect time percentile
119 PercentileAccumulator connection_percentile;
120 /// Acquire connection percentile
121 PercentileAccumulator acquire_percentile;
122 /// Congestion control statistics
123 std::conditional_t<std::is_same_v<Counter, uint32_t>, std::byte /* NOOP */, congestion_control::v2::Stats>
125};
126
127using RateCounter = USERVER_NAMESPACE::utils::statistics::RateCounter;
128using Percentile = USERVER_NAMESPACE::utils::statistics::Percentile<2048>;
129using MinMaxAvg = USERVER_NAMESPACE::utils::statistics::MinMaxAvg<uint32_t>;
130using InstanceStatistics = InstanceStatisticsTemplate<
131 USERVER_NAMESPACE::utils::statistics::RelaxedCounter<uint32_t>,
132 USERVER_NAMESPACE::utils::statistics::RecentPeriod<Percentile, Percentile, detail::SteadyCoarseClock>,
133 USERVER_NAMESPACE::utils::statistics::RecentPeriod<MinMaxAvg, MinMaxAvg, detail::SteadyCoarseClock>>;
134
135struct StatementStatistics final {
136 Percentile timings{};
137 RateCounter executed{};
138 RateCounter errors{};
139
140 void Add(const StatementStatistics& other) {
141 timings.Add(other.timings);
142 executed.Add(other.executed.Load());
143 errors.Add(other.errors.Load());
144 }
145};
146
147using InstanceStatisticsNonatomicBase = InstanceStatisticsTemplate<uint32_t, Percentile, MinMaxAvg>;
148
149struct InstanceStatisticsNonatomic : InstanceStatisticsNonatomicBase {
150 InstanceStatisticsNonatomic() = default;
151
152 template <typename Statistics>
153 InstanceStatisticsNonatomic(const Statistics& stats) {
154 *this = stats;
155 }
156 InstanceStatisticsNonatomic(InstanceStatisticsNonatomic&&) = default;
158
160 Add(const InstanceStatistics& stats, const decltype(InstanceStatistics::topology)& topology_stats) {
161 connection.open_total = stats.connection.open_total;
162 connection.drop_total = stats.connection.drop_total;
163 connection.active = stats.connection.active;
164 connection.used = stats.connection.used;
165 connection.maximum = stats.connection.maximum;
166 connection.waiting = stats.connection.waiting;
167 connection.error_total = stats.connection.error_total;
168 connection.error_timeout = stats.connection.error_timeout;
169 connection.prepared_statements = stats.connection.prepared_statements.GetStatsForPeriod();
170 connection.max_queue_size = stats.connection.max_queue_size;
171
172 transaction.total = stats.transaction.total;
173 transaction.commit_total = stats.transaction.commit_total;
174 transaction.rollback_total = stats.transaction.rollback_total;
175 transaction.out_of_trx_total = stats.transaction.out_of_trx_total;
176 transaction.parse_total = stats.transaction.parse_total;
177 transaction.execute_total = stats.transaction.execute_total;
178 transaction.reply_total = stats.transaction.reply_total;
179 transaction.portal_bind_total = stats.transaction.portal_bind_total;
180 transaction.error_execute_total = stats.transaction.error_execute_total;
181 transaction.execute_timeout = stats.transaction.execute_timeout;
182 transaction.duplicate_prepared_statements = stats.transaction.duplicate_prepared_statements;
183 transaction.total_percentile = stats.transaction.total_percentile.GetStatsForPeriod();
184 transaction.busy_percentile = stats.transaction.busy_percentile.GetStatsForPeriod();
185 transaction.wait_start_percentile = stats.transaction.wait_start_percentile.GetStatsForPeriod();
186 transaction.wait_end_percentile = stats.transaction.wait_end_percentile.GetStatsForPeriod();
187 transaction.return_to_pool_percentile = stats.transaction.return_to_pool_percentile.GetStatsForPeriod();
188
189 topology.roundtrip_time = topology_stats.roundtrip_time.GetStatsForPeriod();
190 topology.replication_lag = topology_stats.replication_lag.GetStatsForPeriod();
191
192 pool_exhaust_errors = stats.pool_exhaust_errors;
193 queue_size_errors = stats.queue_size_errors;
194 connection_percentile = stats.connection_percentile.GetStatsForPeriod();
195 acquire_percentile = stats.acquire_percentile.GetStatsForPeriod();
196
197 return *this;
198 }
199
200 InstanceStatisticsNonatomic& Add(const std::unordered_map<std::string, StatementStatistics>& stats) {
201 for (const auto& [statement_name, statement_stats] : stats) {
202 const auto [it, inserted] = per_statement_stats.try_emplace(statement_name, statement_stats);
203 if (!inserted) {
204 it->second.Add(statement_stats);
205 }
206 }
207
208 return *this;
209 }
210
211 std::unordered_map<std::string, StatementStatistics> per_statement_stats;
212};
213
214/// @brief Instance statistics with description
216 /// host[:port] of an instance
217 std::string host_port;
218 /// Statistics of an instance
220};
221
222/// @brief Cluster statistics storage
224 /// Connlimit mode auto is on
226 /// Master instance statistics
228 /// Sync slave instance statistics
230 /// Slave instances statistics
232 /// Unknown/unreachable instances statistics
234};
235
236// InstanceStatisticsNonatomic values support for utils::statistics::Writer
237void DumpMetric(USERVER_NAMESPACE::utils::statistics::Writer& writer, const InstanceStatisticsNonatomic& stats);
238
239/// @brief InstanceStatsDescriptor values support for utils::statistics::Writer
240void DumpMetric(USERVER_NAMESPACE::utils::statistics::Writer& writer, const InstanceStatsDescriptor& value);
241
242/// @brief ClusterStatistics values support for utils::statistics::Writer
243void DumpMetric(USERVER_NAMESPACE::utils::statistics::Writer& writer, const ClusterStatistics& value);
244
245using ClusterStatisticsPtr = std::unique_ptr<ClusterStatistics>;
246
247} // namespace storages::postgres
248
249USERVER_NAMESPACE_END