22
33#include < logging/dynamic_debug.hpp>
44#include < logging/dynamic_debug_config.hpp>
5+ #include < userver/alerts/source.hpp>
56#include < userver/components/component.hpp>
7+ #include < userver/components/statistics_storage.hpp>
68#include < userver/dynamic_config/storage/component.hpp>
79#include < userver/dynamic_config/value.hpp>
810#include < userver/tracing/tracer.hpp>
@@ -28,14 +30,17 @@ const dynamic_config::Key<logging::DynamicDebugConfig> kDynamicDebugConfig{
2830 }
2931)" }};
3032
33+ alerts::Source kDynamicDebugInvalidLocation {" dynamic_debug_invalid_location" };
34+
3135} // namespace
3236
33- LoggingConfigurator::LoggingConfigurator (const ComponentConfig& config, const ComponentContext& context) {
37+ LoggingConfigurator::LoggingConfigurator (const ComponentConfig& config, const ComponentContext& context)
38+ : metrics_storage_(context.FindComponent<components::StatisticsStorage>().GetMetricsStorage()) {
3439 logging::impl::SetLogLimitedEnable (config[" limited-logging-enable" ].As <bool >());
3540 logging::impl::SetLogLimitedInterval (config[" limited-logging-interval" ].As <std::chrono::milliseconds>());
3641
3742 config_subscription_ = context.FindComponent <components::DynamicConfig>().GetSource ().UpdateAndListen (
38- this , kName , &LoggingConfigurator::OnConfigUpdate
43+ this , kName , &LoggingConfigurator::OnConfigUpdate, ::dynamic_config:: USERVER_NO_LOG_SPANS , kDynamicDebugConfig
3944 );
4045}
4146
@@ -45,38 +50,45 @@ void LoggingConfigurator::OnConfigUpdate(const dynamic_config::Snapshot& config)
4550 (void )this ; // silence clang-tidy
4651 tracing::SetNoLogSpans (tracing::NoLogSpans{config[::dynamic_config::USERVER_NO_LOG_SPANS ]});
4752
48- try {
49- const auto & dd = config[kDynamicDebugConfig ];
50- auto old_dd = dynamic_debug_.Read ();
51- if (!(*old_dd == dd)) {
52- auto lock = dynamic_debug_.StartWrite ();
53- *lock = dd;
54-
55- /* There is a race between multiple AddDynamicDebugLog(), thus some logs
56- * may be logged or not logged by mistake. This is on purpose as logging
57- * locking would be too slow and heavy.
58- */
59-
60- // Flush
61- logging::RemoveAllDynamicDebugLog ();
62-
63- for (const auto & [location, level] : dd.force_disabled ) {
64- const auto [path, line] = logging::SplitLocation (location);
65- logging::EntryState state;
66- state.force_disabled_level_plus_one = logging::GetForceDisabledLevelPlusOne (level);
67- AddDynamicDebugLog (path, line, state);
68- }
69- for (const auto & [location, level] : dd.force_enabled ) {
70- const auto [path, line] = logging::SplitLocation (location);
71- logging::EntryState state;
72- state.force_enabled_level = level;
73- AddDynamicDebugLog (path, line, state);
74- }
75-
76- lock.Commit ();
53+ const auto & dd = config[kDynamicDebugConfig ];
54+
55+ /* There is a race between multiple AddDynamicDebugLog(), thus some logs
56+ * may be logged or not logged by mistake. This is on purpose as logging
57+ * locking would be too slow and heavy.
58+ */
59+
60+ // Flush
61+ logging::RemoveAllDynamicDebugLog ();
62+ bool has_error = false ;
63+
64+ for (const auto & [location, level] : dd.force_disabled ) {
65+ try {
66+ const auto [path, line] = logging::SplitLocation (location);
67+ logging::EntryState state;
68+ state.force_disabled_level_plus_one = logging::GetForceDisabledLevelPlusOne (level);
69+ AddDynamicDebugLog (path, line, state);
70+ } catch (const std::exception& e) {
71+ LOG_ERROR () << " Failed to disable dynamic debug log at '" << location << " ': " << e;
72+ has_error = true ;
73+ }
74+ }
75+
76+ for (const auto & [location, level] : dd.force_enabled ) {
77+ try {
78+ const auto [path, line] = logging::SplitLocation (location);
79+ logging::EntryState state;
80+ state.force_enabled_level = level;
81+ AddDynamicDebugLog (path, line, state);
82+ } catch (const std::exception& e) {
83+ LOG_ERROR () << " Failed to enable dynamic debug log at '" << location << " ': " << e;
84+ has_error = true ;
7785 }
78- } catch (const std::exception& e) {
79- LOG_ERROR () << " Failed to set dynamic debug logs from config: " << e;
86+ }
87+
88+ if (has_error) {
89+ kDynamicDebugInvalidLocation .FireAlert (*metrics_storage_, alerts::Source::kInfiniteDuration );
90+ } else {
91+ kDynamicDebugInvalidLocation .StopAlertNow (*metrics_storage_);
8092 }
8193}
8294
0 commit comments