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