userver: userver/utils/statistics/system_statistics_collector.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
system_statistics_collector.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/statistics/system_statistics_collector.hpp
4/// @brief @copybrief components::SystemStatisticsCollector
5
6#include <userver/components/component_base.hpp>
7#include <userver/components/component_fwd.hpp>
8#include <userver/concurrent/variable.hpp>
9#include <userver/engine/task/task_processor_fwd.hpp>
10#include <userver/utils/periodic_task.hpp>
11#include <userver/utils/statistics/entry.hpp>
12#include <utils/statistics/system_statistics.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace components {
17
18// clang-format off
19
20/// @ingroup userver_components
21///
22/// @brief Component for system resource usage statistics collection.
23///
24/// Periodically queries resource usage info and reports is as a set of metrics.
25///
26/// ## Static options:
27/// Name | Description | Default value
28/// ---- | ----------- | -------------
29/// fs-task-processor | Task processor to use for statistics gathering | -
30/// with-nginx | Whether to collect and report nginx processes statistics | false
31///
32/// Note that `with-nginx` is a relatively expensive option as it requires full
33/// process list scan.
34///
35/// ## Static configuration example:
36///
37/// @snippet components/common_component_list_test.cpp Sample system statistics component config
38
39// clang-format on
40
41class SystemStatisticsCollector final : public ComponentBase {
42public:
43 /// @ingroup userver_component_names
44 /// @brief The default name of components::SystemStatisticsCollector
45 static constexpr std::string_view kName = "system-statistics-collector";
46
47 SystemStatisticsCollector(const ComponentConfig&, const ComponentContext&);
48 ~SystemStatisticsCollector() override;
49
50 static yaml_config::Schema GetStaticConfigSchema();
51
52private:
53 void ExtendStatistics(utils::statistics::Writer& writer);
54
55 void ProcessTimer();
56
57 struct Data {
58 utils::statistics::impl::SystemStats last_stats{};
59 utils::statistics::impl::SystemStats last_nginx_stats{};
60 };
61
62 const bool with_nginx_;
63 engine::TaskProcessor& fs_task_processor_;
64 utils::statistics::Entry statistics_holder_;
65 concurrent::Variable<Data> data_;
66 utils::PeriodicTask periodic_;
67};
68
69template <>
70inline constexpr bool kHasValidate<SystemStatisticsCollector> = true;
71
72} // namespace components
73
74USERVER_NAMESPACE_END