Kea  2.1.7-git
lib/stats/stats_mgr.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 STATSMGR_H
8 #define STATSMGR_H
9 
10 #include <stats/observation.h>
11 #include <stats/context.h>
12 #include <boost/noncopyable.hpp>
13 #include <boost/scoped_ptr.hpp>
14 
15 #include <map>
16 #include <mutex>
17 #include <string>
18 #include <vector>
19 #include <sstream>
20 
21 namespace isc {
22 namespace stats {
23 
63 class StatsMgr : public boost::noncopyable {
64 public:
65 
67  static StatsMgr& instance();
68 
74 
80  void setValue(const std::string& name, const int64_t value);
81 
87  void setValue(const std::string& name, const double value);
88 
94  void setValue(const std::string& name, const StatsDuration& value);
95 
101  void setValue(const std::string& name, const std::string& value);
102 
108  void addValue(const std::string& name, const int64_t value);
109 
115  void addValue(const std::string& name, const double value);
116 
122  void addValue(const std::string& name, const StatsDuration& value);
123 
129  void addValue(const std::string& name, const std::string& value);
130 
147  bool setMaxSampleAge(const std::string& name, const StatsDuration& duration);
148 
161  bool setMaxSampleCount(const std::string& name, uint32_t max_samples);
162 
166  void setMaxSampleAgeAll(const StatsDuration& duration);
167 
171  void setMaxSampleCountAll(uint32_t max_samples);
172 
176  void setMaxSampleAgeDefault(const StatsDuration& duration);
177 
182  void setMaxSampleCountDefault(uint32_t max_samples);
183 
187  const StatsDuration& getMaxSampleAgeDefault() const;
188 
193  uint32_t getMaxSampleCountDefault() const;
194 
196 
202 
210  bool reset(const std::string& name);
211 
216  bool del(const std::string& name);
217 
219  void resetAll();
220 
223  void removeAll();
224 
229  size_t getSize(const std::string& name) const;
230 
234  size_t count() const;
235 
239  isc::data::ConstElementPtr get(const std::string& name) const;
240 
245 
247 
256  ObservationPtr getObservation(const std::string& name) const;
257 
265  ObservationPtr getObservationInternal(const std::string& name) const;
266 
281  template<typename Type>
282  static std::string generateName(const std::string& context, Type index,
283  const std::string& stat_name) {
284  std::stringstream name;
285  name << context << "[" << index << "]." << stat_name;
286  return (name.str());
287  }
288 
294 
310  statisticGetHandler(const std::string& name,
311  const isc::data::ConstElementPtr& params);
312 
328  statisticResetHandler(const std::string& name,
329  const isc::data::ConstElementPtr& params);
330 
346  statisticRemoveHandler(const std::string& name,
347  const isc::data::ConstElementPtr& params);
348 
368  statisticSetMaxSampleAgeHandler(const std::string& name,
369  const isc::data::ConstElementPtr& params);
370 
390  statisticSetMaxSampleCountHandler(const std::string& name,
391  const isc::data::ConstElementPtr& params);
392 
402  statisticGetAllHandler(const std::string& name,
403  const isc::data::ConstElementPtr& params);
404 
414  statisticResetAllHandler(const std::string& name,
415  const isc::data::ConstElementPtr& params);
416 
428  statisticRemoveAllHandler(const std::string& name,
429  const isc::data::ConstElementPtr& params);
430 
447 
465 
467 
468 private:
469 
471 
476  StatsMgr();
477 
479 
490  template<typename DataType>
491  void setValueInternal(const std::string& name, DataType value) {
492  // If we want to log each observation, here would be the best place for it.
494  if (stat) {
495  stat->setValue(value);
496  } else {
497  stat.reset(new Observation(name, value));
499  }
500  }
501 
503 
514  template<typename DataType>
515  void addValueInternal(const std::string& name, DataType value) {
516  // If we want to log each observation, here would be the best place for it.
517  ObservationPtr existing = getObservationInternal(name);
518  if (!existing) {
519  // We tried to add to a non-existing statistic. We can recover from
520  // that. Simply add the new incremental value as a new statistic and
521  // we're done.
522  setValueInternal(name, value);
523  return;
524  } else {
525  // Let's hope it is of correct type. If not, the underlying
526  // addValue() method will throw.
527  existing->addValue(value);
528  }
529  }
530 
532 
540  void addObservation(const ObservationPtr& stat);
541 
543 
551  void addObservationInternal(const ObservationPtr& stat);
552 
554 
561  bool deleteObservation(const std::string& name);
562 
564 
571  bool deleteObservationInternal(const std::string& name);
572 
574 
582  bool setMaxSampleAgeInternal(const std::string& name, const StatsDuration& duration);
583 
585 
593  bool setMaxSampleCountInternal(const std::string& name, uint32_t max_samples);
594 
596 
602  void setMaxSampleAgeAllInternal(const StatsDuration& duration);
603 
605 
611  void setMaxSampleCountAllInternal(uint32_t max_samples);
612 
614 
620  void setMaxSampleAgeDefaultInternal(const StatsDuration& duration);
621 
628  void setMaxSampleCountDefaultInternal(uint32_t max_samples);
629 
631 
637  const StatsDuration& getMaxSampleAgeDefaultInternal() const;
638 
645  uint32_t getMaxSampleCountDefaultInternal() const;
646 
648 
655  bool resetInternal(const std::string& name);
656 
658 
665  bool delInternal(const std::string& name);
666 
668 
672  void resetAllInternal();
673 
675 
679  void removeAllInternal();
680 
682 
689  size_t getSizeInternal(const std::string& name) const;
690 
692 
698  size_t countInternal() const;
699 
701 
707  isc::data::ConstElementPtr getInternal(const std::string& name) const;
708 
710 
716  isc::data::ConstElementPtr getAllInternal() const;
717 
719 
732  static bool getStatName(const isc::data::ConstElementPtr& params,
733  std::string& name,
734  std::string& reason);
735 
737 
754  static bool getStatDuration(const isc::data::ConstElementPtr& params,
755  StatsDuration& duration,
756  std::string& reason);
757 
759 
775  static bool getStatMaxSamples(const isc::data::ConstElementPtr& params,
776  uint32_t& max_samples,
777  std::string& reason);
778 
780  StatContextPtr global_;
781 
783  const boost::scoped_ptr<std::mutex> mutex_;
784 };
785 
786 } // namespace stats
787 } // namespace isc
788 
789 #endif // STATS_MGR
boost::shared_ptr< StatContext > StatContextPtr
Pointer to the statistics context.
Definition: context.h:85
static isc::data::ConstElementPtr statisticGetHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-get command.
ObservationPtr getObservation(const std::string &name) const
Returns an observation.
size_t getSize(const std::string &name) const
Returns size of specified statistic.
static isc::data::ConstElementPtr statisticSetMaxSampleAgeHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-sample-age-set command.
void addObservation(const ObservationPtr &stat)
Adds a new observation.
isc::data::ConstElementPtr statisticSetMaxSampleCountAllHandler(const isc::data::ConstElementPtr &params)
Handles statistic-sample-count-set-all command.
void setMaxSampleCountDefault(uint32_t max_samples)
Set default count limit.
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).
size_t count() const
Returns number of available statistics.
void setMaxSampleCountAll(uint32_t max_samples)
Set count limit for all collected statistics.
static isc::data::ConstElementPtr statisticGetAllHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-get-all command.
bool setMaxSampleAge(const std::string &name, const StatsDuration &duration)
Determines maximum age of samples.
static isc::data::ConstElementPtr statisticResetHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-reset command.
static StatsMgr & instance()
Statistics Manager accessor method.
Statistics Manager class.
void removeAll()
Removes all collected statistics.
std::chrono::system_clock::duration StatsDuration
Defines duration type.
Definition: observation.h:39
void setMaxSampleAgeAll(const StatsDuration &duration)
Set duration limit for all collected statistics.
static isc::data::ConstElementPtr statisticResetAllHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-reset-all command.
bool del(const std::string &name)
Removes specified statistic.
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.
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:27
ObservationPtr getObservationInternal(const std::string &name) const
Returns an observation in a thread safe context.
uint32_t getMaxSampleCountDefault() const
Get default count limit.
static std::string generateName(const std::string &context, Type index, const std::string &stat_name)
Generates statistic name in a given context.
static isc::data::ConstElementPtr statisticRemoveHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-remove command.
void addValue(const std::string &name, const int64_t value)
Records incremental integer observation.
void resetAll()
Resets all collected statistics back to zero.
Defines the logger used by the top-level component of kea-lfc.
isc::data::ConstElementPtr getAll() const
Returns all statistics as a JSON structure.
bool setMaxSampleCount(const std::string &name, uint32_t max_samples)
Determines how many samples of a given statistic should be kept.
void setValueInternal(const std::string &name, DataType value)
Sets a given statistic to specified value (internal version).
bool reset(const std::string &name)
Resets specified statistic.
Represents a single observable characteristic (a &#39;statistic&#39;)
Definition: observation.h:84
isc::data::ConstElementPtr statisticSetMaxSampleAgeAllHandler(const isc::data::ConstElementPtr &params)
Handles statistic-sample-age-set-all command.
void setMaxSampleAgeDefault(const StatsDuration &duration)
Set default duration limit.
const StatsDuration & getMaxSampleAgeDefault() const
Get default duration limit.
boost::shared_ptr< Observation > ObservationPtr
Observation pointer.
Definition: observation.h:440
void setValue(const std::string &name, const int64_t value)
Records absolute integer observation.