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 core/src/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};
56
57template <>
58inline constexpr bool kHasValidate<StatisticsStorage> = true;
59
60template <>
61inline constexpr auto kConfigFileMode<StatisticsStorage> = ConfigFileMode::kNotRequired;
62
63} // namespace components
64
65namespace utils::statistics {
66
67/// @brief Registers a writer in the default @ref components::StatisticsStorage.
68/// Call this helper in the component constructor. Registration is deferred until the complete component, including
69/// derived classes, has been constructed. The callback is unregistered before the component destructor starts.
70/// The callback may run concurrently with other code and must be thread-safe.
71///
72/// Uses @ref components::ComponentContext::Scopes for the component automatically.
73/// See @ref resource_scopes_parameter "choosing scopes" for the lifetime rules.
74///
75/// @see utils::statistics::Storage::RegisterWriter
77 const components::ComponentContext&,
78 std::string common_prefix,
79 WriterFunc func,
80 std::vector<Label> add_labels = {}
81);
82
83} // namespace utils::statistics
84
85USERVER_NAMESPACE_END