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,
106 typename MmaAccumulator>
128using RateCounter = USERVER_NAMESPACE::utils::statistics::RateCounter;
138struct StatementStatistics
final {
139 Percentile timings{};
140 RateCounter executed{};
141 RateCounter errors{};
143 void Add(
const StatementStatistics& other) {
144 timings.Add(other.timings);
145 executed.Add(other.executed.Load());
146 errors.Add(other.errors.Load());
154 InstanceStatisticsNonatomic() =
default;
156 template <
typename Statistics>
157 InstanceStatisticsNonatomic(
const Statistics& stats) {
165 const InstanceStatistics& stats,
166 const decltype(InstanceStatistics::topology)& topology_stats) {
167 connection.open_total = stats.connection.open_total;
168 connection.drop_total = stats.connection.drop_total;
169 connection.active = stats.connection.active;
170 connection.used = stats.connection.used;
171 connection.maximum = stats.connection.maximum;
172 connection.waiting = stats.connection.waiting;
173 connection.error_total = stats.connection.error_total;
174 connection.error_timeout = stats.connection.error_timeout;
175 connection.prepared_statements =
176 stats.connection.prepared_statements.GetStatsForPeriod();
177 connection.max_queue_size = stats.connection.max_queue_size;
179 transaction.total = stats.transaction.total;
180 transaction.commit_total = stats.transaction.commit_total;
181 transaction.rollback_total = stats.transaction.rollback_total;
182 transaction.out_of_trx_total = stats.transaction.out_of_trx_total;
183 transaction.parse_total = stats.transaction.parse_total;
184 transaction.execute_total = stats.transaction.execute_total;
185 transaction.reply_total = stats.transaction.reply_total;
186 transaction.portal_bind_total = stats.transaction.portal_bind_total;
187 transaction.error_execute_total = stats.transaction.error_execute_total;
188 transaction.execute_timeout = stats.transaction.execute_timeout;
189 transaction.duplicate_prepared_statements =
190 stats.transaction.duplicate_prepared_statements;
191 transaction.total_percentile =
192 stats.transaction.total_percentile.GetStatsForPeriod();
193 transaction.busy_percentile =
194 stats.transaction.busy_percentile.GetStatsForPeriod();
195 transaction.wait_start_percentile =
196 stats.transaction.wait_start_percentile.GetStatsForPeriod();
197 transaction.wait_end_percentile =
198 stats.transaction.wait_end_percentile.GetStatsForPeriod();
199 transaction.return_to_pool_percentile =
200 stats.transaction.return_to_pool_percentile.GetStatsForPeriod();
202 topology.roundtrip_time = topology_stats.roundtrip_time.GetStatsForPeriod();
203 topology.replication_lag =
204 topology_stats.replication_lag.GetStatsForPeriod();
206 pool_exhaust_errors = stats.pool_exhaust_errors;
207 queue_size_errors = stats.queue_size_errors;
208 connection_percentile = stats.connection_percentile.GetStatsForPeriod();
209 acquire_percentile = stats.acquire_percentile.GetStatsForPeriod();
215 const std::unordered_map<std::string, StatementStatistics>& stats) {
216 for (
const auto& [statement_name, statement_stats] : stats) {
217 const auto [it, inserted] =
218 per_statement_stats.try_emplace(statement_name, statement_stats);
220 it->second.Add(statement_stats);
227 std::unordered_map<std::string, StatementStatistics> per_statement_stats;
251void DumpMetric(USERVER_NAMESPACE::utils::statistics::Writer& writer,
255void DumpMetric(USERVER_NAMESPACE::utils::statistics::Writer& writer,
259void DumpMetric(USERVER_NAMESPACE::utils::statistics::Writer& writer,