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 "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 |
|
|
|
50 |
|
|
public: |
51 |
|
|
/** |
52 |
|
|
* Creates the requested telemetry aggregator. This function is also used to |
53 |
|
|
* register new classes to aggregate telemetry. |
54 |
|
|
* |
55 |
|
|
* Returns the newly created TelemetryAggregator or NULL if the creation |
56 |
|
|
* was not successful. |
57 |
|
|
*/ |
58 |
|
|
static TelemetryAggregator* Create(Statistics* statistics, |
59 |
|
|
int send_rate, |
60 |
|
|
OptionsManager *options_mgr, |
61 |
|
|
MountPoint* mount_point, |
62 |
|
|
const std::string &fqrn, |
63 |
|
|
const TelemetrySelector type); |
64 |
|
|
virtual ~TelemetryAggregator(); |
65 |
|
|
|
66 |
|
|
/** |
67 |
|
|
* Spawns the telemetry thread. |
68 |
|
|
*/ |
69 |
|
|
void Spawn(); |
70 |
|
|
|
71 |
|
|
protected: |
72 |
|
|
Statistics* statistics_; |
73 |
|
|
const int send_rate_sec_; |
74 |
|
|
MountPoint* mount_point_; |
75 |
|
|
int pipe_terminate_[2]; |
76 |
|
|
pthread_t thread_telemetry_; |
77 |
|
|
std::string fqrn_; |
78 |
|
|
// State of constructed object. Used in custom telemetry classes to |
79 |
|
|
// specify that the object was correctly constructed. |
80 |
|
|
bool is_zombie_; |
81 |
|
|
|
82 |
|
|
uint64_t timestamp_; |
83 |
|
|
std::map<std::string, int64_t> counters_; |
84 |
|
|
|
85 |
|
|
/** |
86 |
|
|
* Main loop executed by the telemetry thread. |
87 |
|
|
* Checks every x seconds if the telemetry thread should continue running. |
88 |
|
|
* If yes, takes a snapshot of all statistic counters that are not 0 and |
89 |
|
|
* calls PushMetrics() |
90 |
|
|
* |
91 |
|
|
* PushMetrics() is defined by the custom telemetry classes and performs all |
92 |
|
|
* operation on the statistic counters to send/store them. |
93 |
|
|
* |
94 |
|
|
*/ |
95 |
|
|
static void *MainTelemetry(void *data); |
96 |
|
|
|
97 |
|
|
/** |
98 |
|
|
* Base constructor taking care of threading infrastructure. |
99 |
|
|
* Must always be called in the constructor of the custom telemetry classes. |
100 |
|
|
*/ |
101 |
|
4 |
TelemetryAggregator(Statistics* statistics, int send_rate_sec, |
102 |
|
4 |
MountPoint* mount_point, const std::string &fqrn) : |
103 |
|
4 |
statistics_(statistics), |
104 |
|
4 |
send_rate_sec_(send_rate_sec), |
105 |
|
4 |
mount_point_(mount_point), |
106 |
|
4 |
fqrn_(fqrn), |
107 |
|
4 |
is_zombie_(true), |
108 |
|
4 |
timestamp_(0) { |
109 |
|
4 |
pipe_terminate_[0] = pipe_terminate_[1] = -1; |
110 |
|
4 |
memset(&thread_telemetry_, 0, sizeof(thread_telemetry_)); |
111 |
|
4 |
} |
112 |
|
|
|
113 |
|
|
/** |
114 |
|
|
* PushMetrics is called after the snapshot of the counters. |
115 |
|
|
* It should perform all manipulations needed for the counters and the |
116 |
|
|
* sending/storing of the counters. |
117 |
|
|
* |
118 |
|
|
* Needs to be implemented in the custom telemetry class. |
119 |
|
|
*/ |
120 |
|
|
virtual void PushMetrics() = 0; |
121 |
|
|
|
122 |
|
|
private: |
123 |
|
|
/** |
124 |
|
|
* Some counters must be manually set to get the current value. |
125 |
|
|
* Copied from talk.cc:MainResponder() |
126 |
|
|
*/ |
127 |
|
|
void ManuallyUpdateSelectedCounters(); |
128 |
|
|
}; |
129 |
|
|
|
130 |
|
|
} // namespace perf |
131 |
|
|
|
132 |
|
|
#endif // CVMFS_TELEMETRY_AGGREGATOR_H_ |
133 |
|
|
|