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_I_METRICS_COLLECTOR_H_
21 : #define FIREBOLT_RIALTO_SERVER_I_METRICS_COLLECTOR_H_
22 :
23 : #include "ControlCommon.h"
24 : #include "IMetricsCollectorClient.h"
25 : #include "MediaCommon.h"
26 : #include <cstdint>
27 : #include <memory>
28 : #include <string>
29 :
30 : namespace firebolt::rialto::server
31 : {
32 : /**
33 : * @brief Client-reported metrics data (mirrors proto ClientProcessMetrics).
34 : */
35 : struct ClientMetricsData
36 : {
37 : std::uint64_t sampleId{0};
38 : MetricsSampleReason reason{MetricsSampleReason::UNKNOWN};
39 : std::string appName;
40 : std::uint32_t processId{0};
41 : std::uint64_t monotonicTimeMs{0};
42 : std::uint64_t epochTimeMs{0};
43 : std::uint64_t processCpuTimeMs{0};
44 : std::uint64_t processMemoryKb{0};
45 : };
46 :
47 : class IMetricsCollector;
48 :
49 : /**
50 : * @brief Factory for creating MetricsCollector instances.
51 : */
52 : class IMetricsCollectorFactory
53 : {
54 : public:
55 12 : IMetricsCollectorFactory() = default;
56 12 : virtual ~IMetricsCollectorFactory() = default;
57 :
58 : static std::shared_ptr<IMetricsCollectorFactory> createFactory();
59 :
60 : /**
61 : * @brief Create a new MetricsCollector for a connected client.
62 : *
63 : * @param clientId Unique client identifier.
64 : * @param client Callback interface for requesting samples from the client.
65 : * @param initialApplicationState Application state when the client connected.
66 : *
67 : * @return The new MetricsCollector instance, or nullptr on failure.
68 : */
69 : virtual std::unique_ptr<IMetricsCollector> create(int clientId,
70 : const std::shared_ptr<IMetricsCollectorClient> &client,
71 : ApplicationState initialApplicationState) = 0;
72 : };
73 :
74 : /**
75 : * @brief Collects, aggregates, and reports metrics for a single connected client.
76 : *
77 : * Each instance owns an ITimer (periodic) that drives sampling, and holds
78 : * the aggregation and threshold-checking framework classes.
79 : */
80 : class IMetricsCollector
81 : {
82 : public:
83 7 : IMetricsCollector() = default;
84 7 : virtual ~IMetricsCollector() = default;
85 :
86 : IMetricsCollector(const IMetricsCollector &) = delete;
87 : IMetricsCollector(IMetricsCollector &&) = delete;
88 : IMetricsCollector &operator=(const IMetricsCollector &) = delete;
89 : IMetricsCollector &operator=(IMetricsCollector &&) = delete;
90 :
91 : /**
92 : * @brief Process a metrics report received from the client.
93 : *
94 : * Computes CPU percentages from deltas, feeds aggregators, checks thresholds.
95 : *
96 : * @param metrics The client-reported metrics data.
97 : */
98 : virtual void processMetrics(const ClientMetricsData &metrics) = 0;
99 :
100 : /**
101 : * @brief Notify that a media pipeline's playback state has changed.
102 : *
103 : * Finalizes the old state's aggregator and begins a new one.
104 : */
105 : virtual void notifyPlaybackStateChanged(int sessionId, PlaybackState oldState, PlaybackState newState) = 0;
106 :
107 : /**
108 : * @brief Notify that a WebAudio player's state has changed.
109 : *
110 : * Finalizes the old state's aggregator and begins a new one.
111 : */
112 : virtual void notifyWebAudioPlayerStateChanged(int handle, WebAudioPlayerState oldState,
113 : WebAudioPlayerState newState) = 0;
114 :
115 : /**
116 : * @brief Notify that the application state has changed (RUNNING/INACTIVE).
117 : *
118 : * Finalizes the RUNNING aggregator on transition to INACTIVE.
119 : */
120 : virtual void notifyApplicationStateChanged(ApplicationState oldState, ApplicationState newState) = 0;
121 : };
122 : } // namespace firebolt::rialto::server
123 :
124 : #endif // FIREBOLT_RIALTO_SERVER_I_METRICS_COLLECTOR_H_
|