From 7aa94ae7d4fddbb3586a39997a137d989e747235 Mon Sep 17 00:00:00 2001 From: seriouscoder43 <55918427+seriouscoder43@users.noreply.github.com> Date: Sun, 28 Jun 2026 03:54:38 +0400 Subject: [PATCH] fix core: convert SystemStatisticsCollector to PIMPL avoid including internal headers in public header --- .../system_statistics_collector.hpp | 24 +++++----- .../system_statistics_collector.cpp | 48 ++++++++++++++----- 2 files changed, 47 insertions(+), 25 deletions(-) diff --git a/core/include/userver/utils/statistics/system_statistics_collector.hpp b/core/include/userver/utils/statistics/system_statistics_collector.hpp index 59eed687ba64..40aaf8979c3e 100644 --- a/core/include/userver/utils/statistics/system_statistics_collector.hpp +++ b/core/include/userver/utils/statistics/system_statistics_collector.hpp @@ -3,15 +3,19 @@ /// @file userver/utils/statistics/system_statistics_collector.hpp /// @brief @copybrief components::SystemStatisticsCollector +#include + #include #include -#include -#include -#include -#include USERVER_NAMESPACE_BEGIN +namespace utils::statistics { + +class Writer; + +} // namespace utils::statistics + namespace components { /// @ingroup userver_components @@ -36,6 +40,7 @@ class SystemStatisticsCollector final : public ComponentBase { static constexpr std::string_view kName = "system-statistics-collector"; SystemStatisticsCollector(const ComponentConfig&, const ComponentContext&); + ~SystemStatisticsCollector() override; static yaml_config::Schema GetStaticConfigSchema(); @@ -44,15 +49,8 @@ class SystemStatisticsCollector final : public ComponentBase { void ProcessTimer(); - struct Data { - utils::statistics::impl::SystemStats last_stats{}; - utils::statistics::impl::SystemStats last_nginx_stats{}; - }; - - const bool with_nginx_; - engine::TaskProcessor& fs_task_processor_; - concurrent::Variable data_; - utils::PeriodicTask periodic_; + struct Impl; + std::unique_ptr impl_; }; template <> diff --git a/core/src/utils/statistics/system_statistics_collector.cpp b/core/src/utils/statistics/system_statistics_collector.cpp index 1527ad6f0ffc..b14227804ed4 100644 --- a/core/src/utils/statistics/system_statistics_collector.cpp +++ b/core/src/utils/statistics/system_statistics_collector.cpp @@ -1,10 +1,16 @@ #include +#include + #include #include +#include #include +#include #include +#include + #ifndef ARCADIA_ROOT #include "generated/src/utils/statistics/system_statistics_collector.yaml.hpp" // Y_IGNORE #endif @@ -13,40 +19,58 @@ USERVER_NAMESPACE_BEGIN namespace components { +struct SystemStatisticsCollector::Impl { + struct Data { + utils::statistics::impl::SystemStats last_stats{}; + utils::statistics::impl::SystemStats last_nginx_stats{}; + }; + + Impl(SystemStatisticsCollector& owner, const ComponentConfig& config, const ComponentContext& context) + : with_nginx(config["with-nginx"].As(false)), + fs_task_processor(GetFsTaskProcessor(config, context)), + periodic( + "system_statistics_collector", + {std::chrono::seconds(10), {utils::PeriodicTask::Flags::kNow}}, + [&owner] { owner.ProcessTimer(); } + ) + {} + + const bool with_nginx; + engine::TaskProcessor& fs_task_processor; + concurrent::Variable data; + utils::PeriodicTask periodic; +}; + SystemStatisticsCollector::SystemStatisticsCollector(const ComponentConfig& config, const ComponentContext& context) : ComponentBase(config, context), - with_nginx_(config["with-nginx"].As(false)), - fs_task_processor_(GetFsTaskProcessor(config, context)), - periodic_( - "system_statistics_collector", - {std::chrono::seconds(10), {utils::PeriodicTask::Flags::kNow}}, - [this] { ProcessTimer(); } - ) + impl_(std::make_unique(*this, config, context)) { utils::statistics::RegisterWriterScope(context, "", [this](utils::statistics::Writer& writer) { ExtendStatistics(writer); }); } +SystemStatisticsCollector::~SystemStatisticsCollector() = default; + void SystemStatisticsCollector::ProcessTimer() { - engine::CriticalAsyncNoTracing(fs_task_processor_, [&] { + engine::CriticalAsyncNoTracing(impl_->fs_task_processor, [&] { auto self = utils::statistics::impl::GetSelfSystemStatistics(); utils::statistics::impl::SystemStats nginx; - if (with_nginx_) { + if (impl_->with_nginx) { nginx = utils::statistics::impl::GetSystemStatisticsByExeName("nginx"); } - auto data = data_.UniqueLock(); + auto data = impl_->data.UniqueLock(); data->last_stats = self; data->last_nginx_stats = nginx; }).Get(); } void SystemStatisticsCollector::ExtendStatistics(utils::statistics::Writer& writer) { - auto data = data_.Lock(); + auto data = impl_->data.Lock(); DumpMetric(writer, data->last_stats); - if (with_nginx_) { + if (impl_->with_nginx) { writer.ValueWithLabels(data->last_nginx_stats, {"application", "nginx"}); } }