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/internal_tag_fwd.hpp>
12#include <userver/utils/statistics/fwd.hpp>
13
14USERVER_NAMESPACE_BEGIN
15
16namespace cache {
17
18namespace impl {
19
20struct UpdateStatistics final {
21 std::atomic<std::size_t> update_attempt_count{0};
22 std::atomic<std::size_t> update_no_changes_count{0};
23 std::atomic<std::size_t> update_failures_count{0};
24
25 std::atomic<std::size_t> documents_read_count{0};
26 std::atomic<std::size_t> documents_parse_failures{0};
27
28 std::atomic<std::chrono::steady_clock::time_point> last_update_start_time{{}};
29 std::atomic<std::chrono::steady_clock::time_point>
30 last_successful_update_start_time{{}};
31 std::atomic<std::chrono::milliseconds> last_update_duration{{}};
32};
33
34void DumpMetric(utils::statistics::Writer& writer,
35 const UpdateStatistics& stats);
36
37struct Statistics final {
38 UpdateStatistics full_update;
39 UpdateStatistics incremental_update;
40 std::atomic<std::size_t> documents_current_count{0};
41};
42
43void DumpMetric(utils::statistics::Writer& writer, const Statistics& stats);
44
45enum class UpdateState { kNotFinished, kSuccess, kFailure };
46
47} // namespace impl
48
49/// @brief Allows a specific cache to fill cache statistics during an `Update`
50///
51/// Unless Finish or FinishNoChanges is called, the update is considered to be a
52/// failure.
53class UpdateStatisticsScope final {
54 public:
55 /// @cond
56 // For internal use only
57 UpdateStatisticsScope(impl::Statistics& stats, cache::UpdateType type);
58
59 ~UpdateStatisticsScope();
60
61 impl::UpdateState GetState(utils::InternalTag) const;
62 /// @endcond
63
64 /// @brief Mark that the `Update` has finished with changes
65 /// @param documents_count the new total number of items stored in the cache
66 void Finish(std::size_t total_documents_count);
67
68 /// @brief Mark that the `Update` has finished without changes
70
71 /// @brief Mark that the `Update` failed
73
74 /// @brief Each item received from the data source should be accounted with
75 /// this function
76 /// @note This method can be called multiple times per `Update`
77 /// @param add the number of items (both valid and non-valid) newly received
78 void IncreaseDocumentsReadCount(std::size_t add);
79
80 /// @brief Each received item that failed validation should be accounted with
81 /// this function, in addition to IncreaseDocumentsReadCount
82 /// @note This method can be called multiple times per `Update`
83 /// @param add the number of non-valid items newly received
84 void IncreaseDocumentsParseFailures(std::size_t add);
85
86 private:
87 void DoFinish(impl::UpdateState new_state);
88
89 impl::Statistics& stats_;
90 impl::UpdateStatistics& update_stats_;
91 impl::UpdateState state_{impl::UpdateState::kNotFinished};
92 const std::chrono::steady_clock::time_point update_start_time_;
93};
94
95} // namespace cache
96
97USERVER_NAMESPACE_END