-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathMetrics.h
271 lines (228 loc) · 10.5 KB
/
Metrics.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
microsoft-oms-auditd-plugin
Copyright (c) Microsoft Corporation
All rights reserved.
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef AUOMS_METRICS_H
#define AUOMS_METRICS_H
#include "RunBase.h"
#include "EventQueue.h"
#include "PriorityQueue.h"
#include "Logger.h"
#include <atomic>
#include <mutex>
#include <chrono>
#include <list>
#include <limits>
#include <cmath>
enum class MetricPeriod: int {
SECOND = 1000,
MINUTE = 60000,
HOUR = 3600000,
};
enum class MetricType {
METRIC_BY_ACCUMULATION,
METRIC_BY_FILL,
METRIC_FROM_TOTAL,
};
struct MetricAggregateSnapshot {
std::string namespace_name;
std::string name;
std::chrono::system_clock::time_point start_time;
std::chrono::system_clock::time_point end_time;
uint64_t sample_period;
uint64_t num_samples;
double min;
double max;
double avg;
};
class MetricData {
public:
MetricData(std::chrono::system_clock::time_point start_time, MetricPeriod sample_period, MetricPeriod agg_period):
_start_time(start_time), _sample_period(sample_period), _agg_period(agg_period), _counts(), _last_index(-1) {
if (static_cast<long>(agg_period) < static_cast<long>(sample_period)) {
_agg_period = _sample_period;
}
_counts.resize(static_cast<long>(_agg_period)/static_cast<long>(_sample_period), 0.0);
}
inline void Add(int idx, double value) {
_counts.at(idx) += value;
_last_index = idx;
}
inline void Set(int idx, double value) {
_counts.at(idx) = value;
_last_index = idx;
}
std::chrono::system_clock::time_point _start_time;
MetricPeriod _sample_period;
MetricPeriod _agg_period;
std::vector<double> _counts;
int _last_index;
};
class Metric {
public:
Metric(const std::string& namespace_name, const std::string& name, MetricPeriod sample_period, MetricPeriod agg_period):
_nsname(namespace_name), _name(name), _sample_period(sample_period), _agg_period(agg_period), _data() {
// We need the stead_clock for calculating where in the sample period we are
// but we need the system_clock to report the metric start/end times.
// We cannot convert between system_clock and steady_clock
// so we get both, and keep trying until the delta between the two is small enough
// most of the time the delta will be very small, but it is possible for the thread
// to get delayed between s1 and t or between t and s2 due to the scheduler.
std::chrono::steady_clock::time_point s1, s2;
std::chrono::system_clock::time_point t;
do {
s1 = std::chrono::steady_clock::now();
t = std::chrono::system_clock::now();
s2 = std::chrono::steady_clock::now();
} while (std::chrono::duration_cast<std::chrono::milliseconds>(s2-s1).count() > 2);
_agg_start_time = t;
_agg_start_steady = s1;
_agg_period_size = std::chrono::milliseconds(static_cast<long>(_agg_period));
_current_data = std::make_shared<MetricData>(_agg_start_time, _sample_period, _agg_period);
}
virtual void Update(double value) = 0;
bool GetAggregateSnapshot(MetricAggregateSnapshot *snap) {
std::lock_guard<std::mutex> lock(_mutex);
// The side effect of GetCountsIdx is that _current_data is pushed to _data if _current_data has "expired"
// If _current_data has any values set (_last_index > -1) then call GetCountsIdx() just-in-case it has "expired".
if (_current_data->_last_index > -1) {
GetCountsIdx();
}
if (_data.empty()) {
return false;
} else {
auto data = _data.front();
_data.pop_front();
snap->namespace_name = _nsname;
snap->name = _name;
snap->start_time = data->_start_time;
snap->end_time = data->_start_time + std::chrono::milliseconds(static_cast<long>(data->_agg_period));
snap->sample_period = static_cast<uint64_t>(data->_sample_period);
snap->num_samples = data->_counts.size();
double div = static_cast<double>(snap->num_samples);
double min = std::numeric_limits<double>::max();
double max = std::numeric_limits<double>::min();
double total = 0;
for (auto c : data->_counts) {
if (min > c) {
min = c;
}
if ( max < c) {
max = c;
}
total += c/div;
}
snap->min = min;
snap->max = max;
snap->avg = total;
return total > 0;
}
}
protected:
inline long GetCountsIdx() {
auto now = std::chrono::steady_clock::now();
auto idx = std::lround(static_cast<double>(std::chrono::duration_cast<std::chrono::milliseconds>(now-_agg_start_steady).count())/static_cast<double>(_sample_period));
if (idx >= _current_data->_counts.size()) {
auto nagg = idx / _current_data->_counts.size();
auto inc = _agg_period_size * nagg;
_agg_start_time += std::chrono::duration_cast<std::chrono::system_clock::time_point::duration>(inc);
_agg_start_steady += std::chrono::duration_cast<std::chrono::steady_clock::time_point::duration>(inc);
idx -= nagg * _current_data->_counts.size();
_data.emplace_back(_current_data);
_current_data = std::make_shared<MetricData>(_agg_start_time, _sample_period, _agg_period);
}
return idx;
}
std::string _nsname;
std::string _name;
MetricPeriod _sample_period;
MetricPeriod _agg_period;
std::chrono::milliseconds _agg_period_size;
std::mutex _mutex;
std::chrono::system_clock::time_point _agg_start_time;
std::chrono::steady_clock::time_point _agg_start_steady;
std::shared_ptr<MetricData> _current_data;
std::list<std::shared_ptr<MetricData>> _data;
};
// The update value is added to any previous value in the sample time slot
class AccumulatorMetric: public Metric {
public:
AccumulatorMetric(const std::string& namespace_name, const std::string& name, MetricPeriod sample_period, MetricPeriod agg_period):
Metric(namespace_name, name, sample_period, agg_period) {}
void Update(double value) override {
std::lock_guard<std::mutex> lock(_mutex);
auto idx = GetCountsIdx();
_current_data->Add(idx, value);
}
};
// The update value replaces any previous sample time slot value
class FillMetric: public Metric {
public:
FillMetric(const std::string& namespace_name, const std::string& name, MetricPeriod sample_period, MetricPeriod agg_period):
Metric(namespace_name, name, sample_period, agg_period) {}
void Update(double value) override {
std::lock_guard<std::mutex> lock(_mutex);
auto idx = GetCountsIdx();
_current_data->Set(idx, value);
}
};
/*
* Some system metrics (e.g. /proc/self/io) only increase for the live of the process
* Each update calculates the value delta and the value/sample_period.
*/
class MetricFromTotal: public Metric {
public:
MetricFromTotal(const std::string& namespace_name, const std::string& name, MetricPeriod sample_period, MetricPeriod agg_period):
Metric(namespace_name, name, sample_period, agg_period), _last_total(0.0), _last_total_index(-1) {}
void Update(double value) override {
std::lock_guard<std::mutex> lock(_mutex);
auto idx = GetCountsIdx();
if (_last_total_index >= 0 && value < _last_total) {
auto subtotal = value - _last_total;
auto sample_time =
_agg_start_steady + std::chrono::milliseconds(idx * static_cast<long>(_sample_period));
auto last_sample_time = _last_total_time_steady + std::chrono::milliseconds(
_last_total_index * static_cast<long>(_sample_period));
auto num_samples =
(sample_time - last_sample_time) / std::chrono::milliseconds(static_cast<long>(_sample_period));
double part = 0.0;
if (num_samples <= 1) {
part = subtotal;
} else {
part = subtotal / static_cast<double>(num_samples);
}
for (auto i = _last_total_index + 1; i <= idx; i++) {
_current_data->Set(i, part);
}
}
_last_total = value;
_last_total_index = idx;
_last_total_time_steady = _agg_start_steady;
}
private:
double _last_total;
long _last_total_index;
std::chrono::steady_clock::time_point _last_total_time_steady;
};
class Metrics: public RunBase {
public:
explicit Metrics(const std::string& proc_name, std::shared_ptr<EventBuilder> builder): _proc_name(proc_name), _builder(std::move(builder)) {}
explicit Metrics(const std::string& proc_name, std::shared_ptr<PriorityQueue> queue): _proc_name(proc_name), _builder(std::make_shared<EventBuilder>(std::make_shared<EventQueue>(std::move(queue)), nullptr)) {}
std::shared_ptr<Metric> AddMetric(MetricType metric_type, const std::string& namespace_name, const std::string& name, MetricPeriod sample_period, MetricPeriod agg_period);
void FlushLogMetrics() { send_log_metrics(true); }
protected:
void run() override;
private:
bool send_metrics();
bool send_log_metrics(bool flush_all);
std::string _proc_name;
std::shared_ptr<EventBuilder> _builder;
std::mutex _mutex;
std::unordered_map<std::string, std::shared_ptr<Metric>> _metrics;
};
#endif //AUOMS_METRICS_H