Kea: observation.h Source File

| Kea 2.2.0

observation.h
Go to the documentation of this file.
1// Copyright (C) 2015-2020 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 <boost/shared_ptr.hpp>
13#include <chrono>
14#include <list>
15#include <stdint.h>
16
17namespace isc {
18namespace stats {
19
24 class InvalidStatType : public Exception {
25public:
26 InvalidStatType(const char* file, size_t line, const char* what) :
27 isc::Exception(file, line, what) {}
28};
29
34 typedef std::chrono::system_clock SampleClock;
35
39 typedef std::chrono::system_clock::duration StatsDuration;
40
45 inline long toSeconds(const StatsDuration& dur) {
46 return ((std::chrono::duration_cast<std::chrono::seconds>(dur)).count());
47}
48
54
56 typedef std::pair<int64_t, SampleClock::time_point> IntegerSample;
57
59 typedef std::pair<double, SampleClock::time_point> FloatSample;
60
62 typedef std::pair<StatsDuration, SampleClock::time_point> DurationSample;
63
65 typedef std::pair<std::string, SampleClock::time_point> StringSample;
66
68
84 class Observation {
85public:
86
95 enum Type {
96 STAT_INTEGER,
97 STAT_FLOAT,
98 STAT_DURATION,
99 STAT_STRING
100 };
101
106 Observation(const std::string& name, const int64_t value);
107
112 Observation(const std::string& name, const double value);
113
118 Observation(const std::string& name, const StatsDuration& value);
119
124 Observation(const std::string& name, const std::string& value);
125
141 void setMaxSampleAge(const StatsDuration& duration);
142
155 void setMaxSampleCount(uint32_t max_samples);
156
160 static void setMaxSampleAgeDefault(const StatsDuration& duration);
161
166 static void setMaxSampleCountDefault(uint32_t max_samples);
167
171 static const StatsDuration& getMaxSampleAgeDefault();
172
177 static uint32_t getMaxSampleCountDefault();
178
180
185 void setValue(const int64_t value);
186
191 void setValue(const double value);
192
197 void setValue(const StatsDuration& value);
198
203 void setValue(const std::string& value);
204
209 void addValue(const int64_t value);
210
215 void addValue(const double value);
216
221 void addValue(const StatsDuration& value);
222
227 void addValue(const std::string& value);
228
232 size_t getSize() const;
233
237 std::pair<bool, StatsDuration> getMaxSampleAge() const;
238
242 std::pair<bool, uint32_t> getMaxSampleCount() const;
243
248 void reset();
249
252 Type getType() const {
253 return (type_);
254 }
255
259 IntegerSample getInteger() const;
260
264 FloatSample getFloat() const;
265
269 DurationSample getDuration() const;
270
274 StringSample getString() const;
275
279 std::list<IntegerSample> getIntegers() const;
280
284 std::list<FloatSample> getFloats() const;
285
289 std::list<DurationSample> getDurations() const;
290
294 std::list<StringSample> getStrings() const;
295
298 isc::data::ConstElementPtr getJSON() const;
299
302 static std::string typeToText(Type type);
303
305 std::string getName() const {
306 return (name_);
307 }
308
309private:
310
320 template<typename StorageType>
321 size_t getSizeInternal(StorageType& storage, Type exp_type) const;
322
335 template<typename SampleType, typename StorageType>
336 void setValueInternal(SampleType value, StorageType& storage,
337 Type exp_type);
338
347 template<typename SampleType, typename Storage>
348 SampleType getValueInternal(Storage& storage, Type exp_type) const;
349
358 template<typename SampleType, typename Storage>
359 std::list<SampleType> getValuesInternal(Storage& storage,
360 Type exp_type) const;
361
368 template<typename StorageType>
369 void setMaxSampleAgeInternal(StorageType& storage,
370 const StatsDuration& duration, Type exp_type);
371
378 template<typename StorageType>
379 void setMaxSampleCountInternal(StorageType& storage,
380 uint32_t max_samples, Type exp_type);
381
383 std::string name_;
384
386 Type type_;
387
395 std::pair<bool, uint32_t> max_sample_count_;
396
401 static std::pair<bool, uint32_t> default_max_sample_count_;
402
410 std::pair<bool, StatsDuration> max_sample_age_;
411
416 static std::pair<bool, StatsDuration> default_max_sample_age_;
417
424
426 std::list<IntegerSample> integer_samples_;
427
429 std::list<FloatSample> float_samples_;
430
432 std::list<DurationSample> duration_samples_;
433
435 std::list<StringSample> string_samples_;
437};
438
440 typedef boost::shared_ptr<Observation> ObservationPtr;
441
442}
443}
444
445#endif // OBSERVATION_H
isc::Exception
This is a base class for exceptions thrown from the DNS library module.
Definition: exceptions/exceptions.h:23
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
Exception thrown if invalid statistic type is used.
Definition: observation.h:24
isc::stats::InvalidStatType::InvalidStatType
InvalidStatType(const char *file, size_t line, const char *what)
Definition: observation.h:26
isc::stats::Observation
Represents a single observable characteristic (a 'statistic')
Definition: observation.h:84
isc::stats::Observation::typeToText
static std::string typeToText(Type type)
Converts statistic type to string.
Definition: observation.cc:367
isc::stats::Observation::getMaxSampleAgeDefault
static const StatsDuration & getMaxSampleAgeDefault()
Get default maximum age of samples.
Definition: observation.cc:355
isc::stats::Observation::setValue
void setValue(const int64_t value)
@
Definition: observation.cc:125
isc::stats::Observation::getType
Type getType() const
Returns statistic type.
Definition: observation.h:252
isc::stats::Observation::getFloats
std::list< FloatSample > getFloats() const
Returns observed float samples.
Definition: observation.cc:259
isc::stats::Observation::reset
void reset()
Resets statistic.
Definition: observation.cc:477
isc::stats::Observation::getName
std::string getName() const
Returns observation name.
Definition: observation.h:305
isc::stats::Observation::getFloat
FloatSample getFloat() const
Returns observed float sample.
Definition: observation.cc:226
isc::stats::Observation::getMaxSampleCountDefault
static uint32_t getMaxSampleCountDefault()
Get default maximum count of samples.
Definition: observation.cc:359
isc::stats::Observation::getMaxSampleAge
std::pair< bool, StatsDuration > getMaxSampleAge() const
Returns both values of max_sample_age_ of statistic.
Definition: observation.cc:167
isc::stats::Observation::addValue
void addValue(const int64_t value)
Records incremental integer observation.
Definition: observation.cc:105
isc::stats::Observation::getString
StringSample getString() const
Returns observed string sample.
Definition: observation.cc:234
isc::stats::Observation::getSize
size_t getSize() const
Returns size of observed storage.
Definition: observation.cc:141
isc::stats::Observation::Type
Type
Type of available statistics.
Definition: observation.h:95
isc::stats::Observation::STAT_INTEGER
@ STAT_INTEGER
this statistic is unsigned 64-bit integer value
Definition: observation.h:96
isc::stats::Observation::STAT_STRING
@ STAT_STRING
this statistic represents a string
Definition: observation.h:99
isc::stats::Observation::STAT_FLOAT
@ STAT_FLOAT
this statistic is a floating point value
Definition: observation.h:97
isc::stats::Observation::STAT_DURATION
@ STAT_DURATION
this statistic represents time duration
Definition: observation.h:98
isc::stats::Observation::getJSON
isc::data::ConstElementPtr getJSON() const
Returns as a JSON structure.
Definition: observation.cc:391
isc::stats::Observation::setMaxSampleCount
void setMaxSampleCount(uint32_t max_samples)
Determines how many samples of a given statistic should be kept.
Definition: observation.cc:81
isc::stats::Observation::setMaxSampleCountDefault
static void setMaxSampleCountDefault(uint32_t max_samples)
Determines default maximum count of samples.
Definition: observation.cc:341
isc::stats::Observation::getInteger
IntegerSample getInteger() const
Returns observed integer sample.
Definition: observation.cc:222
isc::stats::Observation::setMaxSampleAgeDefault
static void setMaxSampleAgeDefault(const StatsDuration &duration)
Determines default maximum age of samples.
Definition: observation.cc:336
isc::stats::Observation::setMaxSampleAge
void setMaxSampleAge(const StatsDuration &duration)
Determines maximum age of samples.
Definition: observation.cc:57
isc::stats::Observation::getStrings
std::list< StringSample > getStrings() const
Returns observed string samples.
Definition: observation.cc:267
isc::stats::Observation::getMaxSampleCount
std::pair< bool, uint32_t > getMaxSampleCount() const
Returns both values of max_sample_count_ of statistic.
Definition: observation.cc:171
isc::stats::Observation::getIntegers
std::list< IntegerSample > getIntegers() const
Returns observed integer samples.
Definition: observation.cc:255
isc::stats::Observation::getDurations
std::list< DurationSample > getDurations() const
Returns observed duration samples.
Definition: observation.cc:263
isc::stats::Observation::Observation
Observation(const std::string &name, const int64_t value)
Constructor for integer observations.
Definition: observation.cc:29
isc::stats::Observation::getDuration
DurationSample getDuration() const
Returns observed duration sample.
Definition: observation.cc:230
data.h
exceptions.h
isc::stats::FloatSample
std::pair< double, SampleClock::time_point > FloatSample
Float (implemented as double precision)
Definition: observation.h:59
isc::stats::IntegerSample
std::pair< int64_t, SampleClock::time_point > IntegerSample
Integer (implemented as signed 64-bit integer)
Definition: observation.h:56
isc::stats::StringSample
std::pair< std::string, SampleClock::time_point > StringSample
String.
Definition: observation.h:65
isc::stats::DurationSample
std::pair< StatsDuration, SampleClock::time_point > DurationSample
Time Duration.
Definition: observation.h:62
isc::data::ConstElementPtr
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
isc::stats::ObservationPtr
boost::shared_ptr< Observation > ObservationPtr
Observation pointer.
Definition: observation.h:440
isc::stats::SampleClock
std::chrono::system_clock SampleClock
Define clock type.
Definition: observation.h:34
isc::stats::toSeconds
long toSeconds(const StatsDuration &dur)
Returns the number of seconds in a duration.
Definition: observation.h:45
isc::stats::StatsDuration
std::chrono::system_clock::duration StatsDuration
Defines duration type.
Definition: observation.h:39
isc
Defines the logger used by the top-level component of kea-lfc.
Definition: agent_parser.cc:143