GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/telemetry_aggregator.h
Date: 2024-04-28 02:33:07
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 "gtest/gtest_prod.h"
21 #include "options.h"
22 #include "statistics.h"
23 #include "util/single_copy.h"
24
25 namespace perf {
26
27 // Return values of telemetry classes (including custom classes)
28 enum TelemetryReturn {
29 kTelemetrySuccess = 0,
30 kTelemetryFailHostAddress,
31 kTelemetryFailPort,
32 kTelemetryFailSocket,
33 kTelemetryFailSend
34 };
35
36 // List of available custom telemetry classes
37 enum TelemetrySelector {
38 kTelemetryInflux
39 };
40
41 class TelemetryAggregator : SingleCopy {
42 FRIEND_TEST(T_TelemetryAggregator, EmptyCounters);
43 FRIEND_TEST(T_TelemetryAggregator, FailCreate);
44 FRIEND_TEST(T_TelemetryAggregator, ExtraFields_Tags);
45 FRIEND_TEST(T_TelemetryAggregator, UpdateCounters_WithExtraFields_Tags);
46
47 public:
48 /**
49 * Creates the requested telemetry aggregator. This function is also used to
50 * register new classes to aggregate telemetry.
51 *
52 * Returns the newly created TelemetryAggregator or NULL if the creation
53 * was not successful.
54 */
55 static TelemetryAggregator* Create(Statistics* statistics,
56 int send_rate,
57 OptionsManager *options_mgr,
58 const std::string &fqrn,
59 const TelemetrySelector type);
60 virtual ~TelemetryAggregator();
61
62 /**
63 * Spawns the telemetry thread.
64 */
65 void Spawn();
66
67 protected:
68 Statistics* statistics_;
69 const int send_rate_sec_;
70 int pipe_terminate_[2];
71 pthread_t thread_telemetry_;
72 std::string fqrn_;
73 // State of constructed object. Used in custom telemetry classes to
74 // specify that the object was correctly constructed.
75 bool is_zombie_;
76
77 uint64_t timestamp_;
78 std::map<std::string, int64_t> counters_;
79
80 /**
81 * Main loop executed by the telemetry thread.
82 * Checks every x seconds if the telemetry thread should continue running.
83 * If yes, takes a snapshot of all statistic counters that are not 0 and
84 * calls PushMetrics()
85 *
86 * PushMetrics() is defined by the custom telemetry classes and performs all
87 * operation on the statistic counters to send/store them.
88 *
89 */
90 static void *MainTelemetry(void *data);
91
92 /**
93 * Base constructor taking care of threading infrastructure.
94 * Must always be called in the constructor of the custom telemetry classes.
95 */
96 4 TelemetryAggregator(Statistics* statistics, int send_rate_sec,
97 4 const std::string &fqrn) :
98 4 statistics_(statistics),
99 4 send_rate_sec_(send_rate_sec),
100 4 fqrn_(fqrn),
101 4 is_zombie_(true),
102 4 timestamp_(0) {
103 4 pipe_terminate_[0] = pipe_terminate_[1] = -1;
104 4 memset(&thread_telemetry_, 0, sizeof(thread_telemetry_));
105 4 }
106
107 /**
108 * PushMetrics is called after the snapshot of the counters.
109 * It should perform all manipulations needed for the counters and the
110 * sending/storing of the counters.
111 *
112 * Needs to be implemented in the custom telemetry class.
113 */
114 virtual void PushMetrics() = 0;
115 };
116
117 } // namespace perf
118
119 #endif // CVMFS_TELEMETRY_AGGREGATOR_H_
120