Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2026 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #ifndef FIREBOLT_RIALTO_SERVER_METRICS_THRESHOLD_CHECKER_H_
21 : #define FIREBOLT_RIALTO_SERVER_METRICS_THRESHOLD_CHECKER_H_
22 :
23 : #include "IMetricsReporter.h"
24 : #include <cstdint>
25 : #include <string>
26 : #include <vector>
27 :
28 : namespace firebolt::rialto::server
29 : {
30 : /**
31 : * @brief Configuration for a single metric threshold.
32 : */
33 : struct MetricsThreshold
34 : {
35 : std::string metricName;
36 : double warningLevel{0.0};
37 : double criticalLevel{0.0};
38 : };
39 :
40 : /**
41 : * @brief Complete threshold configuration.
42 : */
43 : struct MetricsThresholdConfig
44 : {
45 : MetricsThreshold clientCpu{"client_cpu", 80.0, 95.0};
46 : MetricsThreshold serverCpu{"server_cpu", 80.0, 95.0};
47 : MetricsThreshold combinedCpu{"combined_cpu", 150.0, 190.0};
48 : MetricsThreshold clientMemoryKb{"client_mem_kb", 512000.0, 768000.0};
49 : MetricsThreshold serverMemoryKb{"server_mem_kb", 512000.0, 768000.0};
50 : MetricsThreshold cgroupMemoryPercent{"cgroup_mem_pct", 80.0, 95.0};
51 : };
52 :
53 : /**
54 : * @brief Checks metric samples against configured thresholds with debounce.
55 : *
56 : * An alert fires when a metric exceeds the threshold.
57 : * The alert resets (can fire again) only after the metric drops below the threshold
58 : * for at least kDebounceSamples consecutive samples.
59 : */
60 : class MetricsThresholdChecker
61 : {
62 : public:
63 : explicit MetricsThresholdChecker(MetricsThresholdConfig &config, IMetricsReporter *reporter);
64 7 : ~MetricsThresholdChecker() = default;
65 :
66 : void checkSample(double clientCpu, double serverCpu, double combinedCpu, std::uint64_t clientMemKb,
67 : std::uint64_t serverMemKb, std::uint64_t cgroupUsageKb, std::uint64_t cgroupLimitKb);
68 :
69 : private:
70 : static constexpr int kDebounceSamples{2};
71 :
72 : struct ThresholdState
73 : {
74 : bool warningFired{false};
75 : bool criticalFired{false};
76 : int belowWarningCount{0};
77 : int belowCriticalCount{0};
78 : };
79 :
80 : void checkMetric(const MetricsThreshold &threshold, double value, ThresholdState &state);
81 :
82 : MetricsThresholdConfig m_config;
83 : IMetricsReporter *m_reporter; // non-owning
84 : ThresholdState m_clientCpuState;
85 : ThresholdState m_serverCpuState;
86 : ThresholdState m_combinedCpuState;
87 : ThresholdState m_clientMemState;
88 : ThresholdState m_serverMemState;
89 : ThresholdState m_cgroupMemState;
90 : };
91 : } // namespace firebolt::rialto::server
92 :
93 : #endif // FIREBOLT_RIALTO_SERVER_METRICS_THRESHOLD_CHECKER_H_
|