userver: userver/alerts/source.hpp Source File
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
source.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/alerts/source.hpp
4/// @brief @copybrief alerts::Source
5
6#include <chrono>
7#include <string_view>
8
9#include <userver/utils/statistics/metric_tag.hpp>
10#include <userver/utils/statistics/metrics_storage.hpp>
11
12USERVER_NAMESPACE_BEGIN
13
14namespace alerts {
15
16namespace impl {
17struct SourceData {
18 mutable std::atomic<bool> fired{false};
19 std::atomic<std::chrono::steady_clock::time_point> stop_timepoint{{}};
20
21 bool IsExpired() const;
22};
23
24void DumpMetric(utils::statistics::Writer& writer, const SourceData& m);
25} // namespace impl
26
27/// @brief Alert source instance which is used to fire alerts via metrics for a specified amount of time.
28///
29/// To declare an alert:
30/// @snippet core/src/logging/component.cpp alert_declaration
31///
32/// Tu fire or to stop an alert:
33/// @snippet core/src/logging/component.cpp alert_usage
34///
35/// For non alert-metrics consider using utils::statistics::MetricTag.
36class Source final {
37public:
38 static constexpr std::chrono::seconds kDefaultDuration{120};
39 static constexpr std::chrono::hours kInfiniteDuration{24 * 365 * 10}; // In 10 years, someone should notice.
40
41 /// Constructs an alert source instance that will be reported as non-zero "alerts." + std::string{name} metric
42 /// in case of error
43 explicit Source(std::string_view name);
44
45 /// Fire alert for duration seconds.
46 void FireAlert(utils::statistics::MetricsStorage& storage, std::chrono::seconds duration = kDefaultDuration) const;
47
48 /// Stop fired alert
49 void StopAlertNow(utils::statistics::MetricsStorage& storage) const;
50
51private:
52 utils::statistics::MetricTag<impl::SourceData> tag_;
53};
54
55} // namespace alerts
56
57USERVER_NAMESPACE_END