Kea 2.7.6
lib/stats/stats_mgr.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 STATSMGR_H
8#define STATSMGR_H
9
10#include <stats/observation.h>
11#include <stats/context.h>
12#include <util/bigints.h>
13
14#include <boost/noncopyable.hpp>
15#include <boost/scoped_ptr.hpp>
16
17#include <map>
18#include <mutex>
19#include <string>
20#include <vector>
21#include <sstream>
22
23namespace isc {
24namespace stats {
25
65class StatsMgr : public boost::noncopyable {
66public:
67
69 static StatsMgr& instance();
70
76
82 void setValue(const std::string& name, const int64_t value);
83
89 void setValue(const std::string& name, const isc::util::int128_t& value);
90
96 void setValue(const std::string& name, const double value);
97
103 void setValue(const std::string& name, const StatsDuration& value);
104
110 void setValue(const std::string& name, const std::string& value);
111
117 void addValue(const std::string& name, const int64_t value);
118
124 void addValue(const std::string& name, const isc::util::int128_t& value);
125
131 void addValue(const std::string& name, const double value);
132
138 void addValue(const std::string& name, const StatsDuration& value);
139
145 void addValue(const std::string& name, const std::string& value);
146
163 bool setMaxSampleAge(const std::string& name, const StatsDuration& duration);
164
177 bool setMaxSampleCount(const std::string& name, uint32_t max_samples);
178
182 void setMaxSampleAgeAll(const StatsDuration& duration);
183
187 void setMaxSampleCountAll(uint32_t max_samples);
188
192 void setMaxSampleAgeDefault(const StatsDuration& duration);
193
198 void setMaxSampleCountDefault(uint32_t max_samples);
199
204
209 uint32_t getMaxSampleCountDefault() const;
210
212
218
226 bool reset(const std::string& name);
227
232 bool del(const std::string& name);
233
235 void resetAll();
236
239 void removeAll();
240
245 size_t getSize(const std::string& name) const;
246
250 size_t count() const;
251
255 isc::data::ConstElementPtr get(const std::string& name) const;
256
261
263
272 ObservationPtr getObservation(const std::string& name) const;
273
281 ObservationPtr getObservationInternal(const std::string& name) const;
282
297 template<typename Type>
298 static std::string generateName(const std::string& context, Type index,
299 const std::string& stat_name) {
300 std::stringstream name;
301 name << context << "[" << index << "]." << stat_name;
302 return (name.str());
303 }
304
310
326 statisticGetHandler(const std::string& name,
327 const isc::data::ConstElementPtr& params);
328
344 statisticResetHandler(const std::string& name,
345 const isc::data::ConstElementPtr& params);
346
362 statisticRemoveHandler(const std::string& name,
363 const isc::data::ConstElementPtr& params);
364
384 statisticSetMaxSampleAgeHandler(const std::string& name,
385 const isc::data::ConstElementPtr& params);
386
406 statisticSetMaxSampleCountHandler(const std::string& name,
407 const isc::data::ConstElementPtr& params);
408
418 statisticGetAllHandler(const std::string& name,
419 const isc::data::ConstElementPtr& params);
420
430 statisticResetAllHandler(const std::string& name,
431 const isc::data::ConstElementPtr& params);
432
444 statisticRemoveAllHandler(const std::string& name,
445 const isc::data::ConstElementPtr& params);
446
463
481
483
484private:
485
487
492 StatsMgr();
493
495
506 template<typename DataType>
507 void setValueInternal(const std::string& name, DataType value) {
508 // If we want to log each observation, here would be the best place for it.
510 if (stat) {
511 stat->setValue(value);
512 } else {
513 stat.reset(new Observation(name, value));
515 }
516 }
517
519
530 template<typename DataType>
531 void addValueInternal(const std::string& name, DataType value) {
532 // If we want to log each observation, here would be the best place for it.
533 ObservationPtr existing = getObservationInternal(name);
534 if (!existing) {
535 // We tried to add to a non-existing statistic. We can recover from
536 // that. Simply add the new incremental value as a new statistic and
537 // we're done.
538 setValueInternal(name, value);
539 return;
540 } else {
541 // Let's hope it is of correct type. If not, the underlying
542 // addValue() method will throw.
543 existing->addValue(value);
544 }
545 }
546
548
556 void addObservation(const ObservationPtr& stat);
557
559
567 void addObservationInternal(const ObservationPtr& stat);
568
570
577 bool deleteObservation(const std::string& name);
578
580
587 bool deleteObservationInternal(const std::string& name);
588
590
598 bool setMaxSampleAgeInternal(const std::string& name, const StatsDuration& duration);
599
601
609 bool setMaxSampleCountInternal(const std::string& name, uint32_t max_samples);
610
612
618 void setMaxSampleAgeAllInternal(const StatsDuration& duration);
619
621
627 void setMaxSampleCountAllInternal(uint32_t max_samples);
628
630
636 void setMaxSampleAgeDefaultInternal(const StatsDuration& duration);
637
644 void setMaxSampleCountDefaultInternal(uint32_t max_samples);
645
647
653 const StatsDuration& getMaxSampleAgeDefaultInternal() const;
654
661 uint32_t getMaxSampleCountDefaultInternal() const;
662
664
671 bool resetInternal(const std::string& name);
672
674
681 bool delInternal(const std::string& name);
682
684
688 void resetAllInternal();
689
691
695 void removeAllInternal();
696
698
705 size_t getSizeInternal(const std::string& name) const;
706
708
714 size_t countInternal() const;
715
717
723 isc::data::ConstElementPtr getInternal(const std::string& name) const;
724
726
732 isc::data::ConstElementPtr getAllInternal() const;
733
735
748 static bool getStatName(const isc::data::ConstElementPtr& params,
749 std::string& name,
750 std::string& reason);
751
753
770 static bool getStatDuration(const isc::data::ConstElementPtr& params,
771 StatsDuration& duration,
772 std::string& reason);
773
775
791 static bool getStatMaxSamples(const isc::data::ConstElementPtr& params,
792 uint32_t& max_samples,
793 std::string& reason);
794
796 StatContextPtr global_;
797
799 const boost::scoped_ptr<std::mutex> mutex_;
800};
801
802} // namespace stats
803} // namespace isc
804
805#endif // STATS_MGR
Represents a single observable characteristic (a 'statistic')
Definition observation.h:91
Statistics Manager class.
ObservationPtr getObservation(const std::string &name) const
Returns an observation.
ObservationPtr getObservationInternal(const std::string &name) const
Returns an observation in a thread safe context.
static StatsMgr & instance()
Statistics Manager accessor method.
void addObservationInternal(const ObservationPtr &stat)
Adds a new observation in a thread safe context.
void addValueInternal(const std::string &name, DataType value)
Adds specified value to a given statistic (internal version).
void addObservation(const ObservationPtr &stat)
Adds a new observation.
void setValueInternal(const std::string &name, DataType value)
Sets a given statistic to specified value (internal version).
static std::string generateName(const std::string &context, Type index, const std::string &stat_name)
Generates statistic name in a given context.
isc::data::ConstElementPtr statisticSetMaxSampleCountAllHandler(const isc::data::ConstElementPtr &params)
Handles statistic-sample-count-set-all command.
static isc::data::ConstElementPtr statisticResetHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-reset command.
static isc::data::ConstElementPtr statisticGetAllHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-get-all command.
static isc::data::ConstElementPtr statisticRemoveHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-remove command.
static isc::data::ConstElementPtr statisticGetHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-get command.
isc::data::ConstElementPtr statisticSetMaxSampleAgeAllHandler(const isc::data::ConstElementPtr &params)
Handles statistic-sample-age-set-all command.
static isc::data::ConstElementPtr statisticResetAllHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-reset-all command.
static isc::data::ConstElementPtr statisticSetMaxSampleAgeHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-sample-age-set command.
static isc::data::ConstElementPtr statisticRemoveAllHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-remove-all command.
static isc::data::ConstElementPtr statisticSetMaxSampleCountHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-sample-count-set command.
bool reset(const std::string &name)
Resets specified statistic.
void removeAll()
Removes all collected statistics.
void resetAll()
Resets all collected statistics back to zero.
bool del(const std::string &name)
Removes specified statistic.
size_t count() const
Returns number of available statistics.
isc::data::ConstElementPtr getAll() const
Returns all statistics as a JSON structure.
size_t getSize(const std::string &name) const
Returns size of specified statistic.
isc::data::ConstElementPtr get(const std::string &name) const
Returns a single statistic as a JSON structure.
void setMaxSampleCountDefault(uint32_t max_samples)
Set default count limit.
bool setMaxSampleCount(const std::string &name, uint32_t max_samples)
Determines how many samples of a given statistic should be kept.
uint32_t getMaxSampleCountDefault() const
Get default count limit.
void setValue(const std::string &name, const int64_t value)
Records absolute integer observation.
bool setMaxSampleAge(const std::string &name, const StatsDuration &duration)
Determines maximum age of samples.
const StatsDuration & getMaxSampleAgeDefault() const
Get default duration limit.
void setMaxSampleAgeAll(const StatsDuration &duration)
Set duration limit for all collected statistics.
void setMaxSampleCountAll(uint32_t max_samples)
Set count limit for all collected statistics.
void addValue(const std::string &name, const int64_t value)
Records incremental integer observation.
void setMaxSampleAgeDefault(const StatsDuration &duration)
Set default duration limit.
boost::shared_ptr< const Element > ConstElementPtr
Definition data.h:29
boost::shared_ptr< Observation > ObservationPtr
Observation pointer.
boost::shared_ptr< StatContext > StatContextPtr
Pointer to the statistics context.
Definition context.h:85
std::chrono::system_clock::duration StatsDuration
Defines duration type.
Definition observation.h:43
boost::multiprecision::checked_int128_t int128_t
Definition bigints.h:19
Defines the logger used by the top-level component of kea-lfc.