Kea: observation.h Source File

| Kea 3.0.0

observation.h
Go to the documentation of this file.
1// Copyright (C) 2015-2023 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>
11#include <exceptions/exceptions.h>
12#include <util/bigints.h>
13
14#include <boost/shared_ptr.hpp>
15
16#include <chrono>
17#include <list>
18
19#include <stdint.h>
20
21namespace isc {
22namespace stats {
23
28 class InvalidStatType : public Exception {
29public:
30 InvalidStatType(const char* file, size_t line, const char* what) :
31 isc::Exception(file, line, what) {}
32};
33
38 typedef std::chrono::system_clock SampleClock;
39
43 typedef std::chrono::system_clock::duration StatsDuration;
44
49 inline long toSeconds(const StatsDuration& dur) {
50 return ((std::chrono::duration_cast<std::chrono::seconds>(dur)).count());
51}
52
58
60 typedef std::pair<int64_t, SampleClock::time_point> IntegerSample;
61
63 typedef std::pair<isc::util::int128_t, SampleClock::time_point> BigIntegerSample;
64
66 typedef std::pair<double, SampleClock::time_point> FloatSample;
67
69 typedef std::pair<StatsDuration, SampleClock::time_point> DurationSample;
70
72 typedef std::pair<std::string, SampleClock::time_point> StringSample;
73
75
91 class Observation {
92public:
93
102 enum Type {
103 STAT_INTEGER,
104 STAT_BIG_INTEGER,
105 STAT_FLOAT,
106 STAT_DURATION,
107 STAT_STRING
108 };
109
114 Observation(const std::string& name, const int64_t value);
115
120 Observation(const std::string& name, const isc::util::int128_t& value);
121
126 Observation(const std::string& name, const double value);
127
132 Observation(const std::string& name, const StatsDuration& value);
133
138 Observation(const std::string& name, const std::string& value);
139
155 void setMaxSampleAge(const StatsDuration& duration);
156
169 void setMaxSampleCount(uint32_t max_samples);
170
174 static void setMaxSampleAgeDefault(const StatsDuration& duration);
175
180 static void setMaxSampleCountDefault(uint32_t max_samples);
181
185 static const StatsDuration& getMaxSampleAgeDefault();
186
191 static uint32_t getMaxSampleCountDefault();
192
194
199 void setValue(const int64_t value);
200
205 void setValue(const isc::util::int128_t& value);
206
211 void setValue(const double value);
212
217 void setValue(const StatsDuration& value);
218
223 void setValue(const std::string& value);
224
229 void addValue(const int64_t value);
230
235 void addValue(const isc::util::int128_t& value);
236
241 void addValue(const double value);
242
247 void addValue(const StatsDuration& value);
248
253 void addValue(const std::string& value);
254
258 size_t getSize() const;
259
263 std::pair<bool, StatsDuration> getMaxSampleAge() const;
264
268 std::pair<bool, uint32_t> getMaxSampleCount() const;
269
274 void reset();
275
278 Type getType() const {
279 return (type_);
280 }
281
285 IntegerSample getInteger() const;
286
290 BigIntegerSample getBigInteger() const;
291
295 FloatSample getFloat() const;
296
300 DurationSample getDuration() const;
301
305 StringSample getString() const;
306
310 std::list<IntegerSample> getIntegers() const;
311
315 std::list<BigIntegerSample> getBigIntegers() const;
316
320 std::list<FloatSample> getFloats() const;
321
325 std::list<DurationSample> getDurations() const;
326
330 std::list<StringSample> getStrings() const;
331
334 isc::data::ConstElementPtr getJSON() const;
335
338 static std::string typeToText(Type type);
339
341 std::string getName() const {
342 return (name_);
343 }
344
345private:
346
356 template<typename StorageType>
357 size_t getSizeInternal(StorageType& storage, Type exp_type) const;
358
371 template<typename SampleType, typename StorageType>
372 void setValueInternal(SampleType value, StorageType& storage,
373 Type exp_type);
374
383 template<typename SampleType, typename Storage>
384 SampleType getValueInternal(Storage& storage, Type exp_type) const;
385
394 template<typename SampleType, typename Storage>
395 std::list<SampleType> getValuesInternal(Storage& storage,
396 Type exp_type) const;
397
404 template<typename StorageType>
405 void setMaxSampleAgeInternal(StorageType& storage,
406 const StatsDuration& duration, Type exp_type);
407
414 template<typename StorageType>
415 void setMaxSampleCountInternal(StorageType& storage,
416 uint32_t max_samples, Type exp_type);
417
419 std::string name_;
420
422 Type type_;
423
431 std::pair<bool, uint32_t> max_sample_count_;
432
437 static std::pair<bool, uint32_t> default_max_sample_count_;
438
446 std::pair<bool, StatsDuration> max_sample_age_;
447
452 static std::pair<bool, StatsDuration> default_max_sample_age_;
453
460
462 std::list<IntegerSample> integer_samples_;
463
465 std::list<BigIntegerSample> big_integer_samples_;
466
468 std::list<FloatSample> float_samples_;
469
471 std::list<DurationSample> duration_samples_;
472
474 std::list<StringSample> string_samples_;
476};
477
479 typedef boost::shared_ptr<Observation> ObservationPtr;
480
481}
482}
483
484#endif // OBSERVATION_H
bigints.h
isc::Exception::Exception
Exception(const char *file, size_t line, const char *what)
Constructor for a given type for exceptions with file name and file line number.
Definition exceptions/exceptions.cc:17
isc::Exception::what
virtual const char * what() const
Returns a C-style character string of the cause of the exception.
Definition exceptions/exceptions.cc:32
isc::stats::InvalidStatType::InvalidStatType
InvalidStatType(const char *file, size_t line, const char *what)
Definition observation.h:30
isc::stats::Observation::typeToText
static std::string typeToText(Type type)
Converts statistic type to string.
Definition observation.cc:405
isc::stats::Observation::getMaxSampleAgeDefault
static const StatsDuration & getMaxSampleAgeDefault()
Get default maximum age of samples.
Definition observation.cc:393
isc::stats::Observation::setValue
void setValue(const int64_t value)
@
Definition observation.cc:147
isc::stats::Observation::getType
Type getType() const
Returns statistic type.
Definition observation.h:278
isc::stats::Observation::getFloats
std::list< FloatSample > getFloats() const
Returns observed float samples.
Definition observation.cc:297
isc::stats::Observation::reset
void reset()
Resets statistic.
Definition observation.cc:534
isc::stats::Observation::getName
std::string getName() const
Returns observation name.
Definition observation.h:341
isc::stats::Observation::getFloat
FloatSample getFloat() const
Returns observed float sample.
Definition observation.cc:260
isc::stats::Observation::getMaxSampleCountDefault
static uint32_t getMaxSampleCountDefault()
Get default maximum count of samples.
Definition observation.cc:397
isc::stats::Observation::getMaxSampleAge
std::pair< bool, StatsDuration > getMaxSampleAge() const
Returns both values of max_sample_age_ of statistic.
Definition observation.cc:197
isc::stats::Observation::addValue
void addValue(const int64_t value)
Records incremental integer observation.
Definition observation.cc:122
isc::stats::Observation::getString
StringSample getString() const
Returns observed string sample.
Definition observation.cc:268
isc::stats::Observation::getBigIntegers
std::list< BigIntegerSample > getBigIntegers() const
Returns observed big-integer samples.
Definition observation.cc:293
isc::stats::Observation::getSize
size_t getSize() const
Returns size of observed storage.
Definition observation.cc:167
isc::stats::Observation::Type
Type
Type of available statistics.
Definition observation.h:102
isc::stats::Observation::STAT_BIG_INTEGER
@ STAT_BIG_INTEGER
this statistic is signed 128-bit integer value
Definition observation.h:104
isc::stats::Observation::STAT_INTEGER
@ STAT_INTEGER
this statistic is signed 64-bit integer value
Definition observation.h:103
isc::stats::Observation::STAT_STRING
@ STAT_STRING
this statistic represents a string
Definition observation.h:107
isc::stats::Observation::STAT_FLOAT
@ STAT_FLOAT
this statistic is a floating point value
Definition observation.h:105
isc::stats::Observation::STAT_DURATION
@ STAT_DURATION
this statistic represents time duration
Definition observation.h:106
isc::stats::Observation::getJSON
isc::data::ConstElementPtr getJSON() const
Returns as a JSON structure.
Definition observation.cc:432
isc::stats::Observation::setMaxSampleCount
void setMaxSampleCount(uint32_t max_samples)
Determines how many samples of a given statistic should be kept.
Definition observation.cc:94
isc::stats::Observation::setMaxSampleCountDefault
static void setMaxSampleCountDefault(uint32_t max_samples)
Determines default maximum count of samples.
Definition observation.cc:379
isc::stats::Observation::getInteger
IntegerSample getInteger() const
Returns observed integer sample.
Definition observation.cc:252
isc::stats::Observation::setMaxSampleAgeDefault
static void setMaxSampleAgeDefault(const StatsDuration &duration)
Determines default maximum age of samples.
Definition observation.cc:374
isc::stats::Observation::setMaxSampleAge
void setMaxSampleAge(const StatsDuration &duration)
Determines maximum age of samples.
Definition observation.cc:66
isc::stats::Observation::getBigInteger
BigIntegerSample getBigInteger() const
Returns observed integer sample.
Definition observation.cc:256
isc::stats::Observation::getStrings
std::list< StringSample > getStrings() const
Returns observed string samples.
Definition observation.cc:305
isc::stats::Observation::getMaxSampleCount
std::pair< bool, uint32_t > getMaxSampleCount() const
Returns both values of max_sample_count_ of statistic.
Definition observation.cc:201
isc::stats::Observation::getIntegers
std::list< IntegerSample > getIntegers() const
Returns observed integer samples.
Definition observation.cc:289
isc::stats::Observation::getDurations
std::list< DurationSample > getDurations() const
Returns observed duration samples.
Definition observation.cc:301
isc::stats::Observation::Observation
Observation(const std::string &name, const int64_t value)
Constructor for integer observations.
Definition observation.cc:31
isc::stats::Observation::getDuration
DurationSample getDuration() const
Returns observed duration sample.
Definition observation.cc:264
data.h
exceptions.h
isc::stats::FloatSample
std::pair< double, SampleClock::time_point > FloatSample
Float (implemented as double precision)
Definition observation.h:66
isc::stats::IntegerSample
std::pair< int64_t, SampleClock::time_point > IntegerSample
Integer (implemented as signed 64-bit integer)
Definition observation.h:60
isc::stats::StringSample
std::pair< std::string, SampleClock::time_point > StringSample
String.
Definition observation.h:72
isc::stats::BigIntegerSample
std::pair< isc::util::int128_t, SampleClock::time_point > BigIntegerSample
BigInteger (implemented as signed 128-bit integer)
Definition observation.h:63
isc::stats::DurationSample
std::pair< StatsDuration, SampleClock::time_point > DurationSample
Time Duration.
Definition observation.h:69
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
isc::stats
Definition context.cc:18
isc::stats::ObservationPtr
boost::shared_ptr< Observation > ObservationPtr
Observation pointer.
Definition observation.h:479
isc::stats::SampleClock
std::chrono::system_clock SampleClock
Define clock type.
Definition observation.h:38
isc::stats::toSeconds
long toSeconds(const StatsDuration &dur)
Returns the number of seconds in a duration.
Definition observation.h:49
isc::stats::StatsDuration
std::chrono::system_clock::duration StatsDuration
Defines duration type.
Definition observation.h:43
isc::util::int128_t
boost::multiprecision::checked_int128_t int128_t
Definition bigints.h:19
isc
Defines the logger used by the top-level component of kea-lfc.
Definition agent_parser.cc:148