6#include <unordered_map>
9#include <userver/storages/postgres/detail/time_types.hpp>
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>
19USERVER_NAMESPACE_BEGIN
24template <
typename Counter,
typename PercentileAccumulator>
70template <
typename Counter,
typename MmaAccumulator>
96template <
typename MmaAccumulator>
105template <
typename Counter,
typename PercentileAccumulator,
typename MmaAccumulator>
126using RateCounter = USERVER_NAMESPACE::
utils::statistics::RateCounter;
134struct StatementStatistics
final {
135 Percentile timings{};
136 RateCounter executed{};
137 RateCounter errors{};
139 void Add(
const StatementStatistics& other) {
140 timings.Add(other.timings);
141 executed.Add(other.executed.Load());
142 errors.Add(other.errors.Load());
149 InstanceStatisticsNonatomic() =
default;
151 template <
typename Statistics>
152 InstanceStatisticsNonatomic(
const Statistics& stats) {
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;
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();
188 topology.roundtrip_time = topology_stats.roundtrip_time.GetStatsForPeriod();
189 topology.replication_lag = topology_stats.replication_lag.GetStatsForPeriod();
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();
200 for (
const auto& [statement_name, statement_stats] : stats) {
201 const auto [it, inserted] = per_statement_stats.try_emplace(statement_name, statement_stats);
203 it->second.Add(statement_stats);
210 std::unordered_map<std::string, StatementStatistics> per_statement_stats;