Kea 3.3.3
observation.h
Go to the documentation of this file.
1// Copyright (C) 2015-2026 Internet Systems Consortium, Inc. ("ISC")
2//
3// This Source Code Form is subject to the terms of the Mozilla Public
4// License, v. 2.0. If a copy of the MPL was not distributed with this
5// file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7#ifndef OBSERVATION_H
8#define OBSERVATION_H
9
10#include <cc/data.h>
12#include <util/bigints.h>
13
14#include <chrono>
15#include <cstdint>
16#include <list>
17
18#include <boost/shared_ptr.hpp>
19
20namespace isc {
21namespace stats {
22
27class InvalidStatType : public Exception {
28public:
29 InvalidStatType(const char* file, size_t line, const char* what) :
30 isc::Exception(file, line, what) {}
31};
32
37typedef std::chrono::system_clock SampleClock;
38
42typedef std::chrono::system_clock::duration StatsDuration;
43
48inline long toSeconds(const StatsDuration& dur) {
49 return ((std::chrono::duration_cast<std::chrono::seconds>(dur)).count());
50}
51
57
59typedef std::pair<int64_t, SampleClock::time_point> IntegerSample;
60
62typedef std::pair<isc::util::int128_t, SampleClock::time_point> BigIntegerSample;
63
65typedef std::pair<double, SampleClock::time_point> FloatSample;
66
68typedef std::pair<StatsDuration, SampleClock::time_point> DurationSample;
69
71typedef std::pair<std::string, SampleClock::time_point> StringSample;
72
74
91public:
92
108
113 Observation(const std::string& name, const int64_t value);
114
119 Observation(const std::string& name, const isc::util::int128_t& value);
120
125 Observation(const std::string& name, const double value);
126
131 Observation(const std::string& name, const StatsDuration& value);
132
137 Observation(const std::string& name, const std::string& value);
138
154 void setMaxSampleAge(const StatsDuration& duration);
155
168 void setMaxSampleCount(uint32_t max_samples);
169
173 static void setMaxSampleAgeDefault(const StatsDuration& duration);
174
179 static void setMaxSampleCountDefault(uint32_t max_samples);
180
185
190 static uint32_t getMaxSampleCountDefault();
191
193
198 void setValue(const int64_t value);
199
204 void setValue(const isc::util::int128_t& value);
205
210 void setValue(const double value);
211
216 void setValue(const StatsDuration& value);
217
222 void setValue(const std::string& value);
223
228 void addValue(const int64_t value);
229
234 void addValue(const isc::util::int128_t& value);
235
240 void addValue(const double value);
241
246 void addValue(const StatsDuration& value);
247
252 void addValue(const std::string& value);
253
257 size_t getSize() const;
258
262 std::pair<bool, StatsDuration> getMaxSampleAge() const;
263
267 std::pair<bool, uint32_t> getMaxSampleCount() const;
268
273 void reset();
274
277 Type getType() const {
278 return (type_);
279 }
280
285
290
294 FloatSample getFloat() const;
295
300
304 StringSample getString() const;
305
309 std::list<IntegerSample> getIntegers() const;
310
314 std::list<BigIntegerSample> getBigIntegers() const;
315
319 std::list<FloatSample> getFloats() const;
320
324 std::list<DurationSample> getDurations() const;
325
329 std::list<StringSample> getStrings() const;
330
334
337 static std::string typeToText(Type type);
338
340 std::string getName() const {
341 return (name_);
342 }
343
344private:
345
355 template<typename StorageType>
356 size_t getSizeInternal(StorageType& storage, Type exp_type) const;
357
370 template<typename SampleType, typename StorageType>
371 void setValueInternal(SampleType value, StorageType& storage,
372 Type exp_type);
373
382 template<typename SampleType, typename Storage>
383 SampleType getValueInternal(Storage& storage, Type exp_type) const;
384
393 template<typename SampleType, typename Storage>
394 std::list<SampleType> getValuesInternal(Storage& storage,
395 Type exp_type) const;
396
403 template<typename StorageType>
404 void setMaxSampleAgeInternal(StorageType& storage,
405 const StatsDuration& duration, Type exp_type);
406
413 template<typename StorageType>
414 void setMaxSampleCountInternal(StorageType& storage,
415 uint32_t max_samples, Type exp_type);
416
418 std::string name_;
419
421 Type type_;
422
430 std::pair<bool, uint32_t> max_sample_count_;
431
436 static std::pair<bool, uint32_t> default_max_sample_count_;
437
445 std::pair<bool, StatsDuration> max_sample_age_;
446
451 static std::pair<bool, StatsDuration> default_max_sample_age_;
452
459
461 std::list<IntegerSample> integer_samples_;
462
464 std::list<BigIntegerSample> big_integer_samples_;
465
467 std::list<FloatSample> float_samples_;
468
470 std::list<DurationSample> duration_samples_;
471
473 std::list<StringSample> string_samples_;
475};
476
478typedef boost::shared_ptr<Observation> ObservationPtr;
479
480}
481}
482
483#endif // OBSERVATION_H
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
InvalidStatType(const char *file, size_t line, const char *what)
Definition observation.h:29
static std::string typeToText(Type type)
Converts statistic type to string.
static const StatsDuration & getMaxSampleAgeDefault()
Get default maximum age of samples.
void setValue(const int64_t value)
@
Type getType() const
Returns statistic type.
std::list< FloatSample > getFloats() const
Returns observed float samples.
void reset()
Resets statistic.
std::string getName() const
Returns observation name.
FloatSample getFloat() const
Returns observed float sample.
static uint32_t getMaxSampleCountDefault()
Get default maximum count of samples.
std::pair< bool, StatsDuration > getMaxSampleAge() const
Returns both values of max_sample_age_ of statistic.
void addValue(const int64_t value)
Records incremental integer observation.
StringSample getString() const
Returns observed string sample.
std::list< BigIntegerSample > getBigIntegers() const
Returns observed big-integer samples.
size_t getSize() const
Returns size of observed storage.
isc::data::ConstElementPtr getJSON() const
Returns as a JSON structure.
void setMaxSampleCount(uint32_t max_samples)
Determines how many samples of a given statistic should be kept.
static void setMaxSampleCountDefault(uint32_t max_samples)
Determines default maximum count of samples.
IntegerSample getInteger() const
Returns observed integer sample.
static void setMaxSampleAgeDefault(const StatsDuration &duration)
Determines default maximum age of samples.
void setMaxSampleAge(const StatsDuration &duration)
Determines maximum age of samples.
BigIntegerSample getBigInteger() const
Returns observed integer sample.
std::list< StringSample > getStrings() const
Returns observed string samples.
std::pair< bool, uint32_t > getMaxSampleCount() const
Returns both values of max_sample_count_ of statistic.
std::list< IntegerSample > getIntegers() const
Returns observed integer samples.
std::list< DurationSample > getDurations() const
Returns observed duration samples.
Observation(const std::string &name, const int64_t value)
Constructor for integer observations.
Type
Type of available statistics.
@ STAT_BIG_INTEGER
this statistic is signed 128-bit integer value
@ STAT_INTEGER
this statistic is signed 64-bit integer value
@ STAT_STRING
this statistic represents a string
@ STAT_FLOAT
this statistic is a floating point value
@ STAT_DURATION
this statistic represents time duration
DurationSample getDuration() const
Returns observed duration sample.
std::pair< double, SampleClock::time_point > FloatSample
Float (implemented as double precision).
Definition observation.h:65
std::pair< int64_t, SampleClock::time_point > IntegerSample
Integer (implemented as signed 64-bit integer).
Definition observation.h:59
std::pair< std::string, SampleClock::time_point > StringSample
String.
Definition observation.h:71
std::pair< isc::util::int128_t, SampleClock::time_point > BigIntegerSample
BigInteger (implemented as signed 128-bit integer).
Definition observation.h:62
std::pair< StatsDuration, SampleClock::time_point > DurationSample
Time Duration.
Definition observation.h:68
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:30
boost::shared_ptr< Observation > ObservationPtr
Observation pointer.
std::chrono::system_clock SampleClock
Define clock type.
Definition observation.h:37
long toSeconds(const StatsDuration &dur)
Returns the number of seconds in a duration.
Definition observation.h:48
std::chrono::system_clock::duration StatsDuration
Defines duration type.
Definition observation.h:42
boost::multiprecision::checked_int128_t int128_t
Definition bigints.h:19
Defines the logger used by the top-level component of kea-lfc.