7#include <unordered_map>
10#include <userver/storages/postgres/detail/time_types.hpp>
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>
20USERVER_NAMESPACE_BEGIN
25template <
typename Counter,
typename PercentileAccumulator>
71template <
typename Counter,
typename MmaAccumulator>
97template <
typename MmaAccumulator>
106template <
typename Counter,
typename PercentileAccumulator,
typename MmaAccumulator>
123 std::conditional_t<std::is_same_v<Counter, uint32_t>, std::byte , congestion_control::
v2::
Stats>
127using RateCounter = USERVER_NAMESPACE::
utils::statistics::RateCounter;
128using Percentile = USERVER_NAMESPACE::
utils::statistics::Percentile<2048>;
129using MinMaxAvg = USERVER_NAMESPACE::
utils::statistics::MinMaxAvg<uint32_t>;
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>>;
135struct StatementStatistics
final {
136 Percentile timings{};
137 RateCounter executed{};
138 RateCounter errors{};
140 void Add(
const StatementStatistics& other) {
141 timings.Add(other.timings);
142 executed.Add(other.executed.Load());
143 errors.Add(other.errors.Load());
150 InstanceStatisticsNonatomic() =
default;
152 template <
typename Statistics>
153 InstanceStatisticsNonatomic(
const Statistics& stats) {
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;
170 connection.max_queue_size = stats.connection.max_queue_size;
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;
185 transaction.wait_start_percentile = stats.transaction.wait_start_percentile
.GetStatsForPeriod();
187 transaction.return_to_pool_percentile = stats.transaction.return_to_pool_percentile
.GetStatsForPeriod();
192 pool_exhaust_errors = stats.pool_exhaust_errors;
193 queue_size_errors = stats.queue_size_errors;
201 for (
const auto& [statement_name, statement_stats] : stats) {
202 const auto [it, inserted] = per_statement_stats.try_emplace(statement_name, statement_stats);
204 it->second.Add(statement_stats);
211 std::unordered_map<std::string, StatementStatistics> per_statement_stats;