stats_mgr.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #ifndef STATSMGR_H
  15. #define STATSMGR_H
  16. #include <stats/observation.h>
  17. #include <stats/context.h>
  18. #include <boost/noncopyable.hpp>
  19. #include <map>
  20. #include <string>
  21. #include <vector>
  22. namespace isc {
  23. namespace stats {
  24. /// @brief Statistics Manager class
  25. ///
  26. /// StatsMgr is a singleton class that represents a subsystem that manages
  27. /// collection, storage and reporting of various types of statistics.
  28. /// It is also the intended API for both core code and hooks.
  29. ///
  30. /// As of May 2015, Tomek ran performance benchmarks (see unit-tests in
  31. /// stats_mgr_unittest.cc with performance in their names) and it seems
  32. /// the code is able to register ~2.5-3 million observations per second, even
  33. /// with 1000 different statistics recored. That seems sufficient for now,
  34. /// so there is no immediate need to develop any multi-threading solutions
  35. /// for now. However, should this decision be revised in the future, the
  36. /// best place for it would to be modify @ref addObservation method here.
  37. /// It's the common code point that all new observations must pass through.
  38. /// One possible way to enable multi-threading would be to run a separate
  39. /// thread handling collection. The main thread would call @ref addValue and
  40. /// @ref setValue methods that would end up calling @ref addObservation.
  41. /// That method would pass the data to separate thread to be collected and
  42. /// would immediately return. Further processing would be mostly as it
  43. /// is today, except happening in a separate thread. One unsolved issue in
  44. /// this approach is how to extract data, but that will remain unsolvable
  45. /// until we get the control socket implementation.
  46. class StatsMgr : public boost::noncopyable {
  47. public:
  48. /// @brief Statistics Manager accessor method.
  49. static StatsMgr& instance();
  50. /// @defgroup producer_methods Methods are used by data producers.
  51. ///
  52. /// @brief The following methods are used by data producers:
  53. ///
  54. /// @{
  55. /// @brief Records absolute integer observation.
  56. ///
  57. /// @param name name of the observation
  58. /// @param value integer value observed
  59. /// @throw InvalidStatType if statistic is not integer
  60. void setValue(const std::string& name, const uint64_t value);
  61. /// @brief Records absolute floating point observation.
  62. ///
  63. /// @param name name of the observation
  64. /// @param value floating point value observed
  65. /// @throw InvalidStatType if statistic is not fp
  66. void setValue(const std::string& name, const double value);
  67. /// @brief Records absolute duration observation.
  68. ///
  69. /// @param name name of the observation
  70. /// @param value duration value observed
  71. /// @throw InvalidStatType if statistic is not time duration
  72. void setValue(const std::string& name, const StatsDuration& value);
  73. /// @brief Records absolute string observation.
  74. ///
  75. /// @param name name of the observation
  76. /// @param value string value observed
  77. /// @throw InvalidStatType if statistic is not a string
  78. void setValue(const std::string& name, const std::string& value);
  79. /// @brief Records incremental integer observation.
  80. ///
  81. /// @param name name of the observation
  82. /// @param value integer value observed
  83. /// @throw InvalidStatType if statistic is not integer
  84. void addValue(const std::string& name, const uint64_t value);
  85. /// @brief Records incremental floating point observation.
  86. ///
  87. /// @param name name of the observation
  88. /// @param value floating point value observed
  89. /// @throw InvalidStatType if statistic is not fp
  90. void addValue(const std::string& name, const double value);
  91. /// @brief Records incremental duration observation.
  92. ///
  93. /// @param name name of the observation
  94. /// @param value duration value observed
  95. /// @throw InvalidStatType if statistic is not time duration
  96. void addValue(const std::string& name, const StatsDuration& time);
  97. /// @brief Records incremental string observation.
  98. ///
  99. /// @param name name of the observation
  100. /// @param value string value observed
  101. /// @throw InvalidStatType if statistic is not a string
  102. void addValue(const std::string& name, const std::string& value);
  103. /// @brief Determines maximum age of samples.
  104. ///
  105. /// Specifies that statistic name should be stored not as a single value,
  106. /// but rather as a set of values. duration determines the timespan.
  107. /// Samples older than duration will be discarded. This is time-constrained
  108. /// approach. For sample count constrained approach, see @ref
  109. /// setMaxSampleCount() below.
  110. ///
  111. /// @todo: Not implemented.
  112. ///
  113. /// Example: to set a statistic to keep observations for the last 5 minutes,
  114. /// call setMaxSampleAge("incoming-packets", time_duration(0,5,0,0));
  115. /// to revert statistic to a single value, call:
  116. /// setMaxSampleAge("incoming-packets" time_duration(0,0,0,0))
  117. void setMaxSampleAge(const std::string& name, const StatsDuration& duration);
  118. /// @brief Determines how many samples of a given statistic should be kept.
  119. ///
  120. /// Specifies that statistic name should be stored not as single value, but
  121. /// rather as a set of values. In this form, at most max_samples will be kept.
  122. /// When adding max_samples+1 sample, the oldest sample will be discarded.
  123. ///
  124. /// @todo: Not implemented.
  125. ///
  126. /// Example:
  127. /// To set a statistic to keep the last 100 observations, call:
  128. /// setMaxSampleCount("incoming-packets", 100);
  129. void setMaxSampleCount(const std::string& name, uint32_t max_samples);
  130. /// @}
  131. /// @defgroup consumer_methods Methods are used by data consumers.
  132. ///
  133. /// @brief The following methods are used by data consumers:
  134. ///
  135. /// @{
  136. /// @brief Resets specified statistic.
  137. ///
  138. /// This is a convenience function and is equivalent to setValue(name,
  139. /// neutral_value), where neutral_value is 0, 0.0 or "".
  140. /// @param name name of the statistic to be reset.
  141. /// @return true if successful, false if there's no such statistic
  142. bool reset(const std::string& name);
  143. /// @brief Removes specified statistic.
  144. ///
  145. /// @param name name of the statistic to be removed.
  146. /// @return true if successful, false if there's no such statistic
  147. bool del(const std::string& name);
  148. /// @brief Resets all collected statistics back to zero.
  149. void resetAll();
  150. /// @brief Removes all collected statistics.
  151. void removeAll();
  152. /// @brief Returns number of available statistics.
  153. ///
  154. /// @return number of recorded statistics.
  155. size_t count() const;
  156. /// @brief Returns a single statistic as a JSON structure.
  157. ///
  158. /// @return JSON structures representing a single statistic
  159. isc::data::ConstElementPtr get(const std::string& name) const;
  160. /// @brief Returns all statistics as a JSON structure.
  161. ///
  162. /// @return JSON structures representing all statistics
  163. isc::data::ConstElementPtr getAll() const;
  164. /// @}
  165. /// @brief Returns an observation.
  166. ///
  167. /// Used in testing only. Production code should use @ref get() method.
  168. /// @param name name of the statistic
  169. /// @return Pointer to the Observation object
  170. ObservationPtr getObservation(const std::string& name) const;
  171. /// @defgroup command_methods Methods are used to handle commands.
  172. ///
  173. /// @brief The following methods are used to handle commands:
  174. ///
  175. /// @{
  176. /// @brief Handles statistic-get command
  177. ///
  178. /// This method handles statistic-get command, which returns value
  179. /// of a given statistic). It expects one parameter stored in params map:
  180. /// name: name-of-the-statistic
  181. ///
  182. /// Example params structure:
  183. /// {
  184. /// "name": "packets-received"
  185. /// }
  186. ///
  187. /// @param name name of the command (ignored, should be "statistic-get")
  188. /// @param params structure containing a map that contains "name"
  189. /// @param return answer containing details of specified statistic
  190. isc::data::ConstElementPtr
  191. statisticGetHandler(const std::string& name,
  192. const isc::data::ConstElementPtr& params);
  193. /// @param Handles statistic-reset command
  194. ///
  195. /// This method handles statistic-reset command, which resets value
  196. /// of a given statistic. It expects one parameter stored in params map:
  197. /// name: name-of-the-statistic
  198. ///
  199. /// Example params structure:
  200. /// {
  201. /// "name": "packets-received"
  202. /// }
  203. ///
  204. /// @param name name of the command (ignored, should be "statistic-reset")
  205. /// @param params structure containing a map that contains "name"
  206. /// @param return answer containing confirmation
  207. isc::data::ConstElementPtr
  208. statisticResetHandler(const std::string& name,
  209. const isc::data::ConstElementPtr& params);
  210. /// @param Handles statistic-remove command
  211. ///
  212. /// This method handles statistic-reset command, which removes a given
  213. /// statistic completely. It expects one parameter stored in params map:
  214. /// name: name-of-the-statistic
  215. ///
  216. /// Example params structure:
  217. /// {
  218. /// "name": "packets-received"
  219. /// }
  220. ///
  221. /// @param name name of the command (ignored, should be "statistic-remove")
  222. /// @param params structure containing a map that contains "name" element
  223. /// @param return answer containing confirmation
  224. isc::data::ConstElementPtr
  225. statisticRemoveHandler(const std::string& name,
  226. const isc::data::ConstElementPtr& params);
  227. /// @brief Handles statistic-get-all command
  228. ///
  229. /// This method handles statistic-get-all command, which returns values
  230. /// of all statistics. Params parameter is ignored.
  231. ///
  232. /// @param name name of the command (ignored, should be "statistic-get-all")
  233. /// @param params ignored
  234. /// @param return answer containing values of all statistic
  235. isc::data::ConstElementPtr
  236. statisticGetAllHandler(const std::string& name,
  237. const isc::data::ConstElementPtr& params);
  238. /// @brief Handles statistic-reset-all command
  239. ///
  240. /// This method handles statistic-reset-all command, which sets values of
  241. /// all statistics back to zero. Params parameter is ignored.
  242. ///
  243. /// @param name name of the command (ignored, should be "statistic-reset-all")
  244. /// @param params ignored
  245. /// @param return answer confirming success of this operation
  246. isc::data::ConstElementPtr
  247. statisticResetAllHandler(const std::string& name,
  248. const isc::data::ConstElementPtr& params);
  249. /// @brief Handles statistic-remove-all command
  250. ///
  251. /// This method handles statistic-remove-all command, which removes all
  252. /// statistics. Params parameter is ignored.
  253. ///
  254. /// @param name name of the command (ignored, should be "statistic-remove-all")
  255. /// @param params ignored
  256. /// @param return answer confirming success of this operation
  257. isc::data::ConstElementPtr
  258. statisticRemoveAllHandler(const std::string& name,
  259. const isc::data::ConstElementPtr& params);
  260. /// @}
  261. private:
  262. /// @brief Sets a given statistic to specified value (internal version).
  263. ///
  264. /// This template method sets statistic identified by name to a value
  265. /// specified by value. This internal method is used by public @ref setValue
  266. /// methods.
  267. ///
  268. /// @tparam DataType one of uint64_t, double, StatsDuration or string
  269. /// @param name name of the statistic
  270. /// @param value specified statistic will be set to this value
  271. /// @throw InvalidStatType is statistic exists and has a different type.
  272. template<typename DataType>
  273. void setValueInternal(const std::string& name, DataType value) {
  274. ObservationPtr stat = getObservation(name);
  275. if (stat) {
  276. stat->setValue(value);
  277. } else {
  278. stat.reset(new Observation(name, value));
  279. addObservation(stat);
  280. }
  281. }
  282. /// @brief Adds specified value to a given statistic (internal version).
  283. ///
  284. /// This template method adds specified value to a given statistic (identified
  285. /// by name to a value). This internal method is used by public @ref setValue
  286. /// methods.
  287. ///
  288. /// @tparam DataType one of uint64_t, double, StatsDuration or string
  289. /// @param name name of the statistic
  290. /// @param value specified statistic will be set to this value
  291. /// @throw InvalidStatType is statistic exists and has a different type.
  292. template<typename DataType>
  293. void addValueInternal(const std::string& name, DataType value) {
  294. ObservationPtr existing = getObservation(name);
  295. if (!existing) {
  296. // We tried to add to a non-existing statistic. We can recover from
  297. // that. Simply add the new incremental value as a new statistic and
  298. // we're done.
  299. setValue(name, value);
  300. return;
  301. } else {
  302. // Let's hope it is of correct type. If not, the underlying
  303. // addValue() method will throw.
  304. existing->addValue(value);
  305. }
  306. }
  307. /// @brief Private constructor.
  308. /// StatsMgr is a singleton. It should be accessed using @ref instance
  309. /// method.
  310. StatsMgr();
  311. /// @brief Adds a new observation.
  312. ///
  313. /// That's an utility method used by public @ref setValue() and
  314. /// @ref addValue() methods.
  315. /// @param obs observation
  316. void addObservation(const ObservationPtr& o);
  317. /// @brief Tries to delete an observation.
  318. ///
  319. /// @param name of the statistic to be deleted
  320. /// @return true if deleted, false if not found
  321. bool deleteObservation(const std::string& name);
  322. // This is a global context. All statistics will initially be stored here.
  323. StatContextPtr global_;
  324. };
  325. };
  326. };
  327. #endif // STATS_MGR