userver: userver/components/statistics_storage.hpp Source File
Loading...
Searching...
No Matches
statistics_storage.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/components/statistics_storage.hpp
4/// @brief @copybrief components::StatisticsStorage
5
6#include <userver/components/component_fwd.hpp>
7#include <userver/components/raw_component_base.hpp>
8#include <userver/utils/statistics/metrics_storage.hpp>
9#include <userver/utils/statistics/storage.hpp>
10
11USERVER_NAMESPACE_BEGIN
12
13namespace components {
14
15/// @ingroup userver_components
16///
17/// @brief Component that keeps a @ref utils::statistics::Storage storage for metrics.
18///
19/// Returned references to @ref utils::statistics::Storage live for a lifetime
20/// of the component and are safe for concurrent use.
21///
22/// The component does **not** have any options for service config.
23///
24/// ## Static configuration example:
25///
26/// @snippet components/common_component_list_test.cpp Sample statistics storage component config
27class StatisticsStorage final : public RawComponentBase {
28public:
29 /// @ingroup userver_component_names
30 /// @brief The default name of components::StatisticsStorage component
31 static constexpr std::string_view kName = "statistics-storage";
32
33 StatisticsStorage(const ComponentConfig& config, const ComponentContext& context);
34
35 ~StatisticsStorage() override;
36
37 void OnAllComponentsLoaded() override;
38
39 utils::statistics::Storage& GetStorage() { return storage_; }
40
41 const utils::statistics::Storage& GetStorage() const { return storage_; }
42
43 utils::statistics::MetricsStoragePtr GetMetricsStorage() { return metrics_storage_; }
44
45 utils::statistics::MetricsStorage& GetMetricsStorageRef() {
46 UASSERT(metrics_storage_ != nullptr);
47 return *metrics_storage_;
48 }
49
50 static yaml_config::Schema GetStaticConfigSchema();
51
52private:
53 utils::statistics::Storage storage_;
54 utils::statistics::MetricsStoragePtr metrics_storage_;
55 std::vector<utils::statistics::Entry> metrics_storage_registration_;
56};
57
58template <>
59inline constexpr bool kHasValidate<StatisticsStorage> = true;
60
61template <>
62inline constexpr auto kConfigFileMode<StatisticsStorage> = ConfigFileMode::kNotRequired;
63
64} // namespace components
65
66namespace utils::statistics {
67
68/// @brief Add a writer function to @ref Storage from @ref components::StatisticsStorage.
69/// It automatically calls @ref utils::statistics::Storage::RegisterWriter() just after the component
70/// construction and @ref utils::statistics::Entry::Unregister() just before the component
71/// destructor.
72///
73/// @see @ref Storage::RegisterWriter.
74void RegisterWriterScope(
75 const components::ComponentContext&,
76 std::string common_prefix,
77 WriterFunc func,
78 std::vector<Label> add_labels = {}
79);
80
81} // namespace utils::statistics
82
83USERVER_NAMESPACE_END