userver: userver/cache/cache_statistics.hpp Source File
Loading...
Searching...
No Matches
cache_statistics.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/cache/cache_statistics.hpp
4/// @brief Statistics collection for components::CachingComponentBase
5
6#include <atomic>
7#include <chrono>
8#include <cstddef>
9
10#include <userver/cache/update_type.hpp>
11#include <userver/utils/impl/internal_tag.hpp>
12#include <userver/utils/statistics/fwd.hpp>
13#include <userver/utils/statistics/rate_counter.hpp>
14
15USERVER_NAMESPACE_BEGIN
16
17namespace cache {
18
19namespace impl {
20
21struct UpdateStatistics final {
22 utils::statistics::RateCounter update_attempt_count{0};
23 utils::statistics::RateCounter update_no_changes_count{0};
24 utils::statistics::RateCounter update_failures_count{0};
25
26 utils::statistics::RateCounter documents_read_count{0};
27 utils::statistics::RateCounter documents_parse_failures{0};
28
29 std::atomic<std::chrono::steady_clock::time_point> last_update_start_time{{}};
30 std::atomic<std::chrono::steady_clock::time_point>
31 last_successful_update_start_time{{}};
32 std::atomic<std::chrono::milliseconds> last_update_duration{{}};
33};
34
35void DumpMetric(utils::statistics::Writer& writer,
36 const UpdateStatistics& stats);
37
38struct Statistics final {
39 UpdateStatistics full_update;
40 UpdateStatistics incremental_update;
41 std::atomic<std::size_t> documents_current_count{0};
42};
43
44void DumpMetric(utils::statistics::Writer& writer, const Statistics& stats);
45
46enum class UpdateState { kNotFinished, kSuccess, kFailure };
47
48} // namespace impl
49
50/// @brief Allows a specific cache to fill cache statistics during an `Update`.
51///
52/// If `Update` returns without throwing an exception and without calling one
53/// of the `Finish*` methods, the behavior is undefined.
54///
55/// See components::CachingComponentBase::Set() for information on actual cache
56/// update, rather than statistics update.
57class UpdateStatisticsScope final {
58 public:
59 /// @cond
60 // For internal use only
61 UpdateStatisticsScope(impl::Statistics& stats, cache::UpdateType type);
62
63 ~UpdateStatisticsScope();
64
65 // For internal use only
66 impl::UpdateState GetState(utils::impl::InternalTag) const;
67 /// @endcond
68
69 /// @brief Mark that the `Update` has finished with changes
70 /// @param documents_count the new total number of items stored in the cache
71 void Finish(std::size_t total_documents_count);
72
73 /// @brief Mark that the `Update` has finished without changes
75
76 /// @brief Mark that the `Update` failed
78
79 /// @brief Each item received from the data source should be accounted with
80 /// this function
81 /// @note This method can be called multiple times per `Update`
82 /// @param add the number of items (both valid and non-valid) newly received
83 void IncreaseDocumentsReadCount(std::size_t add);
84
85 /// @brief Each received item that failed validation should be accounted with
86 /// this function, in addition to IncreaseDocumentsReadCount
87 /// @note This method can be called multiple times per `Update`
88 /// @param add the number of non-valid items newly received
89 void IncreaseDocumentsParseFailures(std::size_t add);
90
91 private:
92 void DoFinish(impl::UpdateState new_state);
93
94 impl::Statistics& stats_;
95 impl::UpdateStatistics& update_stats_;
96 impl::UpdateState state_{impl::UpdateState::kNotFinished};
97 const std::chrono::steady_clock::time_point update_start_time_;
98};
99
100} // namespace cache
101
102USERVER_NAMESPACE_END