Kea  1.5.0
lib/stats/stats_mgr.h
Go to the documentation of this file.
1 // Copyright (C) 2015,2017 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 
14 #include <map>
15 #include <string>
16 #include <vector>
17 #include <sstream>
18 
19 namespace isc {
20 namespace stats {
21 
61 class StatsMgr : public boost::noncopyable {
62  public:
63 
65  static StatsMgr& instance();
66 
72 
78  void setValue(const std::string& name, const int64_t value);
79 
85  void setValue(const std::string& name, const double value);
86 
92  void setValue(const std::string& name, const StatsDuration& value);
93 
99  void setValue(const std::string& name, const std::string& value);
100 
106  void addValue(const std::string& name, const int64_t value);
107 
113  void addValue(const std::string& name, const double value);
114 
120  void addValue(const std::string& name, const StatsDuration& value);
121 
127  void addValue(const std::string& name, const std::string& value);
128 
143  void setMaxSampleAge(const std::string& name, const StatsDuration& duration);
144 
156  void setMaxSampleCount(const std::string& name, uint32_t max_samples);
157 
159 
165 
172  bool reset(const std::string& name);
173 
178  bool del(const std::string& name);
179 
181  void resetAll();
182 
184  void removeAll();
185 
189  size_t count() const;
190 
194  isc::data::ConstElementPtr get(const std::string& name) const;
195 
200 
202 
208  ObservationPtr getObservation(const std::string& name) const;
209 
224  template<typename Type>
225  static std::string generateName(const std::string& context, Type index,
226  const std::string& stat_name) {
227  std::stringstream name;
228  name << context << "[" << index << "]." << stat_name;
229  return (name.str());
230  }
231 
237 
253  statisticGetHandler(const std::string& name,
254  const isc::data::ConstElementPtr& params);
255 
271  statisticResetHandler(const std::string& name,
272  const isc::data::ConstElementPtr& params);
273 
289  statisticRemoveHandler(const std::string& name,
290  const isc::data::ConstElementPtr& params);
291 
301  statisticGetAllHandler(const std::string& name,
302  const isc::data::ConstElementPtr& params);
303 
313  statisticResetAllHandler(const std::string& name,
314  const isc::data::ConstElementPtr& params);
315 
325  statisticRemoveAllHandler(const std::string& name,
326  const isc::data::ConstElementPtr& params);
327 
329 
330 private:
331 
335  StatsMgr();
336 
338 
349  template<typename DataType>
350  void setValueInternal(const std::string& name, DataType value) {
351 
352  // If we want to log each observation, here would be the best place for it.
353  ObservationPtr stat = getObservation(name);
354  if (stat) {
355  stat->setValue(value);
356  } else {
357  stat.reset(new Observation(name, value));
358  addObservation(stat);
359  }
360  }
361 
363 
374  template<typename DataType>
375  void addValueInternal(const std::string& name, DataType value) {
376 
377  // If we want to log each observation, here would be the best place for it.
378  ObservationPtr existing = getObservation(name);
379  if (!existing) {
380  // We tried to add to a non-existing statistic. We can recover from
381  // that. Simply add the new incremental value as a new statistic and
382  // we're done.
383  setValue(name, value);
384  return;
385  } else {
386  // Let's hope it is of correct type. If not, the underlying
387  // addValue() method will throw.
388  existing->addValue(value);
389  }
390  }
391 
393 
399  void addObservation(const ObservationPtr& stat);
400 
402 
407  bool deleteObservation(const std::string& name);
408 
421  static bool getStatName(const isc::data::ConstElementPtr& params,
422  std::string& name,
423  std::string& reason);
424 
425  // This is a global context. All statistics will initially be stored here.
426  StatContextPtr global_;
427 };
428 
429 };
430 };
431 
432 #endif // STATS_MGR
boost::shared_ptr< StatContext > StatContextPtr
Pointer to the statistics context.
Definition: context.h:59
ObservationPtr getObservation(const std::string &name) const
Returns an observation.
Definition: stats_mgr.cc:62
static isc::data::ConstElementPtr statisticResetAllHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-reset-all command.
Definition: stats_mgr.cc:204
void addObservation(const ObservationPtr &stat)
Adds a new observation.
Definition: stats_mgr.cc:68
static isc::data::ConstElementPtr statisticRemoveAllHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-remove-all command.
Definition: stats_mgr.cc:189
void setMaxSampleCount(const std::string &name, uint32_t max_samples)
Determines how many samples of a given statistic should be kept.
Definition: stats_mgr.cc:85
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.
Definition: stats_mgr.cc:139
static StatsMgr & instance()
Statistics Manager accessor method.
Definition: stats_mgr.cc:21
Statistics Manager class.
void removeAll()
Removes all collected statistics.
Definition: stats_mgr.cc:103
void setMaxSampleAge(const std::string &name, const StatsDuration &duration)
Determines maximum age of samples.
Definition: stats_mgr.cc:80
bool del(const std::string &name)
Removes specified statistic.
Definition: stats_mgr.cc:99
isc::data::ConstElementPtr getAll() const
Returns all statistics as a JSON structure.
Definition: stats_mgr.cc:116
static isc::data::ConstElementPtr statisticGetHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-get command.
Definition: stats_mgr.cc:144
boost::shared_ptr< const Element > ConstElementPtr
Definition: data.h:23
static std::string generateName(const std::string &context, Type index, const std::string &stat_name)
Generates statistic name in a given context.
void addValue(const std::string &name, const int64_t value)
Records incremental integer observation.
Definition: stats_mgr.cc:46
void resetAll()
Resets all collected statistics back to zero.
Definition: stats_mgr.cc:129
Defines the logger used by the top-level component of kea-dhcp-ddns.
void setValueInternal(const std::string &name, DataType value)
Sets a given statistic to specified value (internal version).
boost::posix_time::time_duration StatsDuration
Defines duration resolution.
Definition: observation.h:33
bool reset(const std::string &name)
Resets specified statistic.
Definition: stats_mgr.cc:89
Represents a single observable characteristic (a 'statistic')
Definition: observation.h:71
static isc::data::ConstElementPtr statisticResetHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-reset command.
Definition: stats_mgr.cc:155
static isc::data::ConstElementPtr statisticRemoveHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-remove command.
Definition: stats_mgr.cc:172
static isc::data::ConstElementPtr statisticGetAllHandler(const std::string &name, const isc::data::ConstElementPtr &params)
Handles statistic-get-all command.
Definition: stats_mgr.cc:197
boost::shared_ptr< Observation > ObservationPtr
Observation pointer.
Definition: observation.h:261
void setValue(const std::string &name, const int64_t value)
Records absolute integer observation.
Definition: stats_mgr.cc:31
isc::data::ConstElementPtr get(const std::string &name) const
Returns a single statistic as a JSON structure.
Definition: stats_mgr.cc:107