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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
|
// Copyright (C) 2015-2023 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
#ifndef OBSERVATION_H
#define OBSERVATION_H
#include <cc/data.h>
#include <exceptions/exceptions.h>
#include <util/bigints.h>
#include <boost/shared_ptr.hpp>
#include <chrono>
#include <list>
#include <stdint.h>
namespace isc {
namespace stats {
/// @brief Exception thrown if invalid statistic type is used
///
/// For example statistic is of type duration, but methods using
/// it as integer are called.
class InvalidStatType : public Exception {
public:
InvalidStatType(const char* file, size_t line, const char* what) :
isc::Exception(file, line, what) {}
};
/// @brief Define clock type.
///
/// @note: we use the system clock i.e. the wall clock because this
/// clock can be converted from and to standard Unix time (time_t).
typedef std::chrono::system_clock SampleClock;
/// @brief Defines duration type.
///
/// @note: the precision depends on the system,
typedef std::chrono::system_clock::duration StatsDuration;
/// @brief Returns the number of seconds in a duration.
///
/// @param dur The duration.
/// @return The number of seconds in the given duration.
inline long toSeconds(const StatsDuration& dur) {
return ((std::chrono::duration_cast<std::chrono::seconds>(dur)).count());
}
/// @defgroup stat_samples Specifies supported observation types.
///
/// @brief The list covers all supported types of observations.
///
/// @{
/// @brief Integer (implemented as signed 64-bit integer)
typedef std::pair<int64_t, SampleClock::time_point> IntegerSample;
/// @brief BigInteger (implemented as signed 128-bit integer)
typedef std::pair<isc::util::int128_t, SampleClock::time_point> BigIntegerSample;
/// @brief Float (implemented as double precision)
typedef std::pair<double, SampleClock::time_point> FloatSample;
/// @brief Time Duration
typedef std::pair<StatsDuration, SampleClock::time_point> DurationSample;
/// @brief String
typedef std::pair<std::string, SampleClock::time_point> StringSample;
/// @}
/// @brief Represents a single observable characteristic (a 'statistic')
///
/// Currently it supports one of four types: integer (implemented as signed 64
/// bit integer), float (implemented as double), time duration (implemented with
/// millisecond precision) and string. Absolute (setValue) and
/// incremental (addValue) modes are supported. Statistic type is determined
/// during its first use. Once type is set, any additional observations recorded
/// must be of the same type. Attempting to set or extract information about
/// other types will result in InvalidStateType exception.
///
/// Observation can be retrieved in one of @ref getInteger, @ref getFloat,
/// @ref getDuration, @ref getString (appropriate type must be used) or
/// @ref getJSON, which is generic and can be used for all types.
///
/// Since Kea 1.6 multiple samples are stored for the same observation.
class Observation {
public:
/// @brief Type of available statistics
///
/// Note that those will later be exposed using control socket. Therefore
/// an easy to understand names were chosen (integer instead of uint64).
/// To avoid confusion, we will support only one type of integer and only
/// one type of floating points. Initially, these are represented by
/// int64_t and double. If convincing use cases appear to change them
/// to something else, we may change the underlying type.
enum Type {
STAT_INTEGER, ///< this statistic is signed 64-bit integer value
STAT_BIG_INTEGER, ///< this statistic is signed 128-bit integer value
STAT_FLOAT, ///< this statistic is a floating point value
STAT_DURATION, ///< this statistic represents time duration
STAT_STRING ///< this statistic represents a string
};
/// @brief Constructor for integer observations
///
/// @param name observation name
/// @param value integer value observed.
Observation(const std::string& name, const int64_t value);
/// @brief Constructor for big integer observations
///
/// @param name observation name
/// @param value integer value observed.
Observation(const std::string& name, const isc::util::int128_t& value);
/// @brief Constructor for floating point observations
///
/// @param name observation name
/// @param value floating point value observed.
Observation(const std::string& name, const double value);
/// @brief Constructor for duration observations
///
/// @param name observation name
/// @param value duration observed.
Observation(const std::string& name, const StatsDuration& value);
/// @brief Constructor for string observations
///
/// @param name observation name
/// @param value string observed.
Observation(const std::string& name, const std::string& value);
/// @brief Determines maximum age of samples.
///
/// Specifies that statistic name should be stored not as a single value,
/// but rather as a set of values. The duration determines the timespan.
/// Samples older than duration will be discarded. This is time-constrained
/// approach. For sample count constrained approach, see @ref
/// setMaxSampleCount() below.
///
///
/// @param duration determines maximum age of samples
/// Example:
/// To set a statistic to keep observations for the last 5 minutes, call:
/// setMaxSampleAge(std::chrono::minutes(5));
/// To revert statistic to a single value, call:
/// setMaxSampleAge(StatsDuration::zero());
void setMaxSampleAge(const StatsDuration& duration);
/// @brief Determines how many samples of a given statistic should be kept.
///
/// Specifies that statistic name should be stored not as a single value,
/// but rather as a set of values. In this form, at most max_samples will
/// be kept. When adding max_samples + 1 sample, the oldest sample will be
/// discarded.
///
///
/// @param max_samples how many samples of a given statistic should be kept
/// Example:
/// To set a statistic to keep the last 100 observations, call:
/// setMaxSampleCount(100);
void setMaxSampleCount(uint32_t max_samples);
/// @brief Determines default maximum age of samples.
///
/// @param duration default maximum age of samples to keep.
static void setMaxSampleAgeDefault(const StatsDuration& duration);
/// @brief Determines default maximum count of samples.
///
/// @param max_samples default maximum count of samples to keep.
/// (0 means to disable count limit and enable age limit)
static void setMaxSampleCountDefault(uint32_t max_samples);
/// @brief Get default maximum age of samples.
///
/// @return default maximum age of samples to keep.
static const StatsDuration& getMaxSampleAgeDefault();
/// @brief Get default maximum count of samples.
///
/// @return max_samples default maximum count of samples to keep.
/// (0 means that count limit was disabled)
static uint32_t getMaxSampleCountDefault();
/// @
/// @brief Records absolute integer observation
///
/// @param value integer value observed
/// @throw InvalidStatType if statistic is not integer
void setValue(const int64_t value);
/// @brief Records big integer observation
///
/// @param value integer value observed
/// @throw InvalidStatType if statistic is not integer
void setValue(const isc::util::int128_t& value);
/// @brief Records absolute floating point observation
///
/// @param value floating point value observed
/// @throw InvalidStatType if statistic is not fp
void setValue(const double value);
/// @brief Records absolute duration observation
///
/// @param value duration value observed
/// @throw InvalidStatType if statistic is not time duration
void setValue(const StatsDuration& value);
/// @brief Records absolute string observation
///
/// @param value string value observed
/// @throw InvalidStatType if statistic is not a string
void setValue(const std::string& value);
/// @brief Records incremental integer observation
///
/// @param value integer value observed
/// @throw InvalidStatType if statistic is not integer
void addValue(const int64_t value);
/// @brief Records incremental integer observation
///
/// @param value integer value observed
/// @throw InvalidStatType if statistic is not integer
void addValue(const isc::util::int128_t& value);
/// @brief Records incremental floating point observation
///
/// @param value floating point value observed
/// @throw InvalidStatType if statistic is not fp
void addValue(const double value);
/// @brief Records incremental duration observation
///
/// @param value duration value observed
/// @throw InvalidStatType if statistic is not time duration
void addValue(const StatsDuration& value);
/// @brief Records incremental string observation.
///
/// @param value string value observed
/// @throw InvalidStatType if statistic is not a string
void addValue(const std::string& value);
/// @brief Returns size of observed storage
///
/// @return size of storage
size_t getSize() const;
/// @brief Returns both values of max_sample_age_ of statistic.
///
/// @return max_sample_age_.
std::pair<bool, StatsDuration> getMaxSampleAge() const;
/// @brief Returns both values of max_sample_count_ of statistic.
///
/// @return max_sample_count_.
std::pair<bool, uint32_t> getMaxSampleCount() const;
/// @brief Resets statistic.
///
/// Sets statistic to a neutral (0, 0.0 or "") value and
/// clears the underlying storage.
void reset();
/// @brief Returns statistic type
/// @return statistic type
Type getType() const {
return (type_);
}
/// @brief Returns observed integer sample
/// @return observed sample (value + timestamp)
/// @throw InvalidStatType if statistic is not integer
IntegerSample getInteger() const;
/// @brief Returns observed integer sample
/// @return observed sample (value + timestamp)
/// @throw InvalidStatType if statistic is not integer
BigIntegerSample getBigInteger() const;
/// @brief Returns observed float sample
/// @return observed sample (value + timestamp)
/// @throw InvalidStatType if statistic is not fp
FloatSample getFloat() const;
/// @brief Returns observed duration sample
/// @return observed sample (value + timestamp)
/// @throw InvalidStatType if statistic is not time duration
DurationSample getDuration() const;
/// @brief Returns observed string sample
/// @return observed sample (value + timestamp)
/// @throw InvalidStatType if statistic is not a string
StringSample getString() const;
/// @brief Returns observed integer samples
/// @return list of observed samples (value + timestamp)
/// @throw InvalidStatType if statistic is not integer
std::list<IntegerSample> getIntegers() const;
/// @brief Returns observed big-integer samples
/// @return list of observed samples (value + timestamp)
/// @throw InvalidStatType if statistic is not integer
std::list<BigIntegerSample> getBigIntegers() const;
/// @brief Returns observed float samples
/// @return list of observed samples (value + timestamp)
/// @throw InvalidStatType if statistic is not fp
std::list<FloatSample> getFloats() const;
/// @brief Returns observed duration samples
/// @return list of observed samples (value + timestamp)
/// @throw InvalidStatType if statistic is not time duration
std::list<DurationSample> getDurations() const;
/// @brief Returns observed string samples
/// @return list of observed samples (value + timestamp)
/// @throw InvalidStatType if statistic is not a string
std::list<StringSample> getStrings() const;
/// @brief Returns as a JSON structure
/// @return JSON structures representing all observations
isc::data::ConstElementPtr getJSON() const;
/// @brief Converts statistic type to string
/// @return textual name of statistic type
static std::string typeToText(Type type);
/// @brief Returns observation name
std::string getName() const {
return (name_);
}
private:
/// @brief Returns size of observed storage
///
/// This method returns size of observed storage.
/// It is used by public methods to return size of
/// available storages.
/// @tparam Storage type of storage (e.g. list<IntegerSample>)
/// @param storage storage which size will be returned
/// @param exp_type expected observation type (used for sanity checking)
/// @return size of storage
template<typename StorageType>
size_t getSizeInternal(StorageType& storage, Type exp_type) const;
/// @brief Records absolute sample (internal version)
///
/// This method records an absolute value of an observation.
/// It is used by public methods to add sample to one of
/// available storages.
///
/// @tparam SampleType type of sample (e.g. IntegerSample)
/// @tparam StorageType type of storage (e.g. list<IntegerSample>)
/// @param value observation to be recorded
/// @param storage observation will be stored here
/// @param exp_type expected observation type (used for sanity checking)
/// @throw InvalidStatType if observation type mismatches
template<typename SampleType, typename StorageType>
void setValueInternal(SampleType value, StorageType& storage,
Type exp_type);
/// @brief Returns a sample (internal version)
///
/// @tparam SampleType type of sample (e.g. IntegerSample)
/// @tparam StorageType type of storage (e.g. list<IntegerSample>)
/// @param observation storage
/// @param exp_type expected observation type (used for sanity checking)
/// @throw InvalidStatType if observation type mismatches
/// @return Observed sample
template<typename SampleType, typename Storage>
SampleType getValueInternal(Storage& storage, Type exp_type) const;
/// @brief Returns samples (internal version)
///
/// @tparam SampleType type of samples (e.g. IntegerSample)
/// @tparam Storage type of storage (e.g. list<IntegerSample>)
/// @param observation storage
/// @param exp_type expected observation type (used for sanity checking)
/// @throw InvalidStatType if observation type mismatches
/// @return list of observed samples
template<typename SampleType, typename Storage>
std::list<SampleType> getValuesInternal(Storage& storage,
Type exp_type) const;
/// @brief Determines maximum age of samples.
///
/// @tparam Storage type of storage (e.g. list<IntegerSample>)
/// @param storage storage on which limit will be set
/// @param duration determines maximum age of samples
/// @param exp_type expected observation type (used for sanity checking)
template<typename StorageType>
void setMaxSampleAgeInternal(StorageType& storage,
const StatsDuration& duration, Type exp_type);
/// @brief Determines how many samples of a given statistic should be kept.
///
/// @tparam Storage type of storage (e.g. list<IntegerSample>)
/// @param storage storage on which limit will be set
/// @param max_samples determines maximum number of samples
/// @param exp_type expected observation type (used for sanity checking)
template<typename StorageType>
void setMaxSampleCountInternal(StorageType& storage,
uint32_t max_samples, Type exp_type);
/// @brief Observation (statistic) name
std::string name_;
/// @brief Observation (statistic) type)
Type type_;
/// @brief Maximum number of samples
/// The limit is represented as a pair
/// of bool value and uint32_t
/// Only one kind of limit can be active
/// The bool value informs which limit
/// is available
/// True means active limit, false means inactive limit
std::pair<bool, uint32_t> max_sample_count_;
/// @brief Default maximum number of samples
///
/// By default the MaxSampleCount is set to 20
/// and MaxSampleAge is disabled
static std::pair<bool, uint32_t> default_max_sample_count_;
/// @brief Maximum timespan of samples
/// The limit is represented as a pair
/// of bool value and StatsDuration
/// Only one kind of limit can be active
/// The bool value informs which limit
/// is available
/// True means active limit, false means inactive limit
std::pair<bool, StatsDuration> max_sample_age_;
/// @brief Default maximum timespan of samples
///
/// By default the MaxSampleCount is set to 20
/// and MaxSampleAge is disabled
static std::pair<bool, StatsDuration> default_max_sample_age_;
/// @defgroup samples_storage Storage for supported observations
///
/// @brief The following containers serve as a storage for all supported
/// observation types.
///
/// @{
/// @brief Storage for integer samples
std::list<IntegerSample> integer_samples_;
/// @brief Storage for big integer samples
std::list<BigIntegerSample> big_integer_samples_;
/// @brief Storage for floating point samples
std::list<FloatSample> float_samples_;
/// @brief Storage for time duration samples
std::list<DurationSample> duration_samples_;
/// @brief Storage for string samples
std::list<StringSample> string_samples_;
/// @}
};
/// @brief Observation pointer
typedef boost::shared_ptr<Observation> ObservationPtr;
}
}
#endif // OBSERVATION_H
|