GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/telemetry_aggregator.h
Date: 2025-08-31 02:39:21
Exec Total Coverage
Lines: 10 10 100.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 *
4 * TelemetryAggregator class manages a thread that snapshots the internal
5 * counters of cvmfs statistic object. A custom telemetry class is needed to
6 * perform the step of manipulating and sending/storing the counters.
7 */
8
9 #ifndef CVMFS_TELEMETRY_AGGREGATOR_H_
10 #define CVMFS_TELEMETRY_AGGREGATOR_H_
11
12 #include <pthread.h>
13 #include <stdint.h>
14
15 #include <cstring>
16 #include <map>
17 #include <string>
18 #include <vector>
19
20 #include "duplex_testing.h"
21 #include "mountpoint.h"
22 #include "options.h"
23 #include "statistics.h"
24 #include "util/single_copy.h"
25
26 class MountPoint;
27
28 namespace perf {
29
30 // Return values of telemetry classes (including custom classes)
31 enum TelemetryReturn {
32 kTelemetrySuccess = 0,
33 kTelemetryFailHostAddress,
34 kTelemetryFailPort,
35 kTelemetryFailSocket,
36 kTelemetryFailSend
37 };
38
39 // List of available custom telemetry classes
40 enum TelemetrySelector {
41 kTelemetryInflux
42 };
43
44 class TelemetryAggregator : SingleCopy {
45 FRIEND_TEST(T_TelemetryAggregator, EmptyCounters);
46 FRIEND_TEST(T_TelemetryAggregator, FailCreate);
47 FRIEND_TEST(T_TelemetryAggregator, ExtraFields_Tags);
48 FRIEND_TEST(T_TelemetryAggregator, UpdateCounters_WithExtraFields_Tags);
49 FRIEND_TEST(T_TelemetryAggregator, SendDeltaDisabled);
50 FRIEND_TEST(T_TelemetryAggregator, SendDeltaEnabledByDefault);
51 FRIEND_TEST(T_TelemetryAggregator, SendDeltaExplicitlyEnabled);
52
53 public:
54 /**
55 * Creates the requested telemetry aggregator. This function is also used to
56 * register new classes to aggregate telemetry.
57 *
58 * Returns the newly created TelemetryAggregator or NULL if the creation
59 * was not successful.
60 */
61 static TelemetryAggregator *Create(Statistics *statistics,
62 int send_rate,
63 OptionsManager *options_mgr,
64 MountPoint *mount_point,
65 const std::string &fqrn,
66 const TelemetrySelector type);
67 virtual ~TelemetryAggregator();
68
69 /**
70 * Spawns the telemetry thread.
71 */
72 void Spawn();
73
74 protected:
75 Statistics *statistics_;
76 const int send_rate_sec_;
77 MountPoint *mount_point_;
78 int pipe_terminate_[2];
79 pthread_t thread_telemetry_;
80 std::string fqrn_;
81 // State of constructed object. Used in custom telemetry classes to
82 // specify that the object was correctly constructed.
83 bool is_zombie_;
84
85 uint64_t timestamp_;
86 std::map<std::string, int64_t> counters_;
87
88 /**
89 * Main loop executed by the telemetry thread.
90 * Checks every x seconds if the telemetry thread should continue running.
91 * If yes, takes a snapshot of all statistic counters that are not 0 and
92 * calls PushMetrics()
93 *
94 * PushMetrics() is defined by the custom telemetry classes and performs all
95 * operation on the statistic counters to send/store them.
96 *
97 */
98 static void *MainTelemetry(void *data);
99
100 /**
101 * Base constructor taking care of threading infrastructure.
102 * Must always be called in the constructor of the custom telemetry classes.
103 */
104 301 TelemetryAggregator(Statistics *statistics, int send_rate_sec,
105 MountPoint *mount_point, const std::string &fqrn)
106 602 : statistics_(statistics)
107 301 , send_rate_sec_(send_rate_sec)
108 301 , mount_point_(mount_point)
109 301 , fqrn_(fqrn)
110 301 , is_zombie_(true)
111 301 , timestamp_(0) {
112 301 pipe_terminate_[0] = pipe_terminate_[1] = -1;
113 301 memset(&thread_telemetry_, 0, sizeof(thread_telemetry_));
114 301 }
115
116 /**
117 * PushMetrics is called after the snapshot of the counters.
118 * It should perform all manipulations needed for the counters and the
119 * sending/storing of the counters.
120 *
121 * Needs to be implemented in the custom telemetry class.
122 */
123 virtual void PushMetrics() = 0;
124
125 private:
126 /**
127 * Some counters must be manually set to get the current value.
128 * Copied from talk.cc:MainResponder()
129 */
130 void ManuallyUpdateSelectedCounters();
131 };
132
133 } // namespace perf
134
135 #endif // CVMFS_TELEMETRY_AGGREGATOR_H_
136