17using namespace std::chrono;
24std::pair<bool, uint32_t>
25Observation::default_max_sample_count_ = std::make_pair(
true, 20);
27std::pair<bool, StatsDuration>
28Observation::default_max_sample_age_ =
29 std::make_pair(
false, StatsDuration::zero());
32 name_(name), type_(STAT_INTEGER),
33 max_sample_count_(default_max_sample_count_),
34 max_sample_age_(default_max_sample_age_) {
39 name_(name), type_(STAT_BIG_INTEGER),
40 max_sample_count_(default_max_sample_count_),
41 max_sample_age_(default_max_sample_age_) {
46 name_(name), type_(STAT_FLOAT),
47 max_sample_count_(default_max_sample_count_),
48 max_sample_age_(default_max_sample_age_) {
53 name_(name), type_(STAT_DURATION),
54 max_sample_count_(default_max_sample_count_),
55 max_sample_age_(default_max_sample_age_) {
60 name_(name), type_(STAT_STRING),
61 max_sample_count_(default_max_sample_count_),
62 max_sample_age_(default_max_sample_age_) {
69 setMaxSampleAgeInternal(integer_samples_, duration,
STAT_INTEGER);
77 setMaxSampleAgeInternal(float_samples_, duration,
STAT_FLOAT);
81 setMaxSampleAgeInternal(duration_samples_, duration,
STAT_DURATION);
85 setMaxSampleAgeInternal(string_samples_, duration,
STAT_STRING);
97 setMaxSampleCountInternal(integer_samples_, max_samples,
STAT_INTEGER);
101 setMaxSampleCountInternal(big_integer_samples_, max_samples,
STAT_BIG_INTEGER);
105 setMaxSampleCountInternal(float_samples_, max_samples,
STAT_FLOAT);
109 setMaxSampleCountInternal(duration_samples_, max_samples,
STAT_DURATION);
113 setMaxSampleCountInternal(string_samples_, max_samples,
STAT_STRING);
148 setValueInternal(value, integer_samples_,
STAT_INTEGER);
156 setValueInternal(value, float_samples_,
STAT_FLOAT);
164 setValueInternal(value, string_samples_,
STAT_STRING);
179 size = getSizeInternal(float_samples_,
STAT_FLOAT);
187 size = getSizeInternal(string_samples_,
STAT_STRING);
198 return (max_sample_age_);
202 return (max_sample_count_);
205template<
typename StorageType>
206size_t Observation::getSizeInternal(StorageType& storage, Type exp_type)
const {
207 if (type_ != exp_type) {
209 <<
typeToText(exp_type) <<
", but the actual type is "
212 return (storage.size());
217template<
typename SampleType,
typename StorageType>
218void Observation::setValueInternal(SampleType value, StorageType& storage,
220 if (type_ != exp_type) {
221 isc_throw(InvalidStatType,
"Invalid statistic type requested: "
222 <<
typeToText(exp_type) <<
", but the actual type is "
226 if (storage.empty()) {
227 storage.push_back(make_pair(value, SampleClock::now()));
230 storage.push_front(make_pair(value, SampleClock::now()));
232 if (max_sample_count_.first) {
235 if (storage.size() > max_sample_count_.second) {
240 storage.front().second - storage.back().second;
243 while (range_of_storage > max_sample_age_.second) {
246 storage.front().second - storage.back().second;
253 return (getValueInternal<IntegerSample>(integer_samples_,
STAT_INTEGER));
257 return (getValueInternal<BigIntegerSample>(big_integer_samples_,
STAT_BIG_INTEGER));
261 return (getValueInternal<FloatSample>(float_samples_,
STAT_FLOAT));
265 return (getValueInternal<DurationSample>(duration_samples_,
STAT_DURATION));
269 return (getValueInternal<StringSample>(string_samples_,
STAT_STRING));
272template<
typename SampleType,
typename Storage>
273SampleType Observation::getValueInternal(Storage& storage, Type exp_type)
const {
274 if (type_ != exp_type) {
276 <<
typeToText(exp_type) <<
", but the actual type is "
280 if (storage.empty()) {
286 return (*storage.begin());
290 return (getValuesInternal<IntegerSample>(integer_samples_,
STAT_INTEGER));
294 return (getValuesInternal<BigIntegerSample>(big_integer_samples_,
STAT_BIG_INTEGER));
298 return (getValuesInternal<FloatSample>(float_samples_,
STAT_FLOAT));
302 return (getValuesInternal<DurationSample>(duration_samples_,
STAT_DURATION));
306 return (getValuesInternal<StringSample>(string_samples_,
STAT_STRING));
309template<
typename SampleType,
typename Storage>
310std::list<SampleType> Observation::getValuesInternal(Storage& storage,
311 Type exp_type)
const {
312 if (type_ != exp_type) {
314 <<
typeToText(exp_type) <<
", but the actual type is "
318 if (storage.empty()) {
327template<
typename StorageType>
328void Observation::setMaxSampleAgeInternal(StorageType& storage,
331 if (type_ != exp_type) {
332 isc_throw(InvalidStatType,
"Invalid statistic type requested: "
333 <<
typeToText(exp_type) <<
", but the actual type is "
337 max_sample_age_.first =
true;
338 max_sample_age_.second = duration;
340 max_sample_count_.first =
false;
343 storage.front().second - storage.back().second;
345 while (range_of_storage > duration) {
348 range_of_storage = storage.front().second - storage.back().second;
352template<
typename StorageType>
353void Observation::setMaxSampleCountInternal(StorageType& storage,
354 uint32_t max_samples,
356 if (type_ != exp_type) {
357 isc_throw(InvalidStatType,
"Invalid statistic type requested: "
358 <<
typeToText(exp_type) <<
", but the actual type is "
363 max_sample_count_.first =
true;
364 max_sample_count_.second = max_samples;
366 max_sample_age_.first =
false;
368 while (storage.size() > max_samples) {
376 default_max_sample_age_.second = duration;
380 if (max_samples == 0) {
382 default_max_sample_count_.first =
false;
383 default_max_sample_age_.first =
true;
386 default_max_sample_count_.second = max_samples;
388 default_max_sample_age_.first =
false;
389 default_max_sample_count_.first =
true;
394 return (default_max_sample_age_.second);
398 if (default_max_sample_count_.first) {
399 return (default_max_sample_count_.second);
406 std::stringstream tmp;
412 tmp <<
"big integer";
427 tmp <<
"(" << type <<
")";
446 for (
auto const& it : s) {
452 entry->add(timestamp);
469 entry->add(timestamp);
480 for (
auto const& it : s) {
486 entry->add(timestamp);
497 for (
auto const& it : s) {
503 entry->add(timestamp);
514 for (
auto const& it : s) {
520 entry->add(timestamp);
537 integer_samples_.clear();
542 big_integer_samples_.clear();
547 float_samples_.clear();
552 duration_samples_.clear();
557 string_samples_.clear();
A generic exception that is thrown when an unexpected error condition occurs.
static ElementPtr create(const Position &pos=ZERO_POSITION())
static ElementPtr createList(const Position &pos=ZERO_POSITION())
Creates an empty ListElement type ElementPtr.
Exception thrown if invalid statistic type is used.
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)
@
std::list< FloatSample > getFloats() const
Returns observed float samples.
void reset()
Resets statistic.
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.
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
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.
DurationSample getDuration() const
Returns observed duration sample.
#define isc_throw(type, stream)
A shortcut macro to insert known values into exception arguments.
std::pair< double, SampleClock::time_point > FloatSample
Float (implemented as double precision)
std::pair< int64_t, SampleClock::time_point > IntegerSample
Integer (implemented as signed 64-bit integer)
std::pair< std::string, SampleClock::time_point > StringSample
String.
std::pair< isc::util::int128_t, SampleClock::time_point > BigIntegerSample
BigInteger (implemented as signed 128-bit integer)
std::pair< StatsDuration, SampleClock::time_point > DurationSample
Time Duration.
boost::shared_ptr< const Element > ConstElementPtr
boost::shared_ptr< Element > ElementPtr
std::chrono::system_clock::duration StatsDuration
Defines duration type.
boost::multiprecision::checked_int128_t int128_t
std::string durationToText(boost::posix_time::time_duration dur, size_t fsecs_precision=MAX_FSECS_PRECISION)
Converts StatsDuration to text.
std::string clockToText(std::chrono::system_clock::time_point t, size_t fsecs_precision)
Converts chrono time point structure to text.
Defines the logger used by the top-level component of kea-lfc.