statistics.h 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // Copyright (C) 2010 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 __STATISTICS_H
  15. #define __STATISTICS_H 1
  16. #include <dns/opcode.h>
  17. #include <dns/rcode.h>
  18. #include <cc/session.h>
  19. #include <stdint.h>
  20. #include <boost/scoped_ptr.hpp>
  21. class AuthCountersImpl;
  22. /// \brief Set of query counters.
  23. ///
  24. /// \c AuthCounters is set of query counters class. It holds query counters
  25. /// and provides an interface to increment the counter of specified type
  26. /// (e.g. UDP query, TCP query).
  27. ///
  28. /// This class also provides a function to send statistics information to
  29. /// statistics module.
  30. ///
  31. /// This class is designed to be a part of \c AuthSrv.
  32. /// Call \c setStatisticsSession() to set a session to communicate with
  33. /// statistics module like Xfrin session.
  34. /// Call \c inc() to increment a counter for specific type of query in
  35. /// the query processing function. use \c enum \c CounterType to specify
  36. /// the type of query.
  37. /// Call \c submitStatistics() to submit statistics information to statistics
  38. /// module with statistics_session, periodically or at a time the command
  39. /// \c sendstats is received.
  40. ///
  41. /// We may eventually want to change the structure to hold values that are
  42. /// not counters (such as concurrent TCP connections), or seperate generic
  43. /// part to src/lib to share with the other modules.
  44. ///
  45. /// This class uses pimpl idiom and hides detailed implementation.
  46. /// This class is constructed on startup of the server, so
  47. /// construction overhead of this approach should be acceptable.
  48. ///
  49. /// \todo Hold counters for each query types (Notify, Axfr, Ixfr, Normal)
  50. /// \todo Consider overhead of \c AuthCounters::inc()
  51. class AuthCounters {
  52. private:
  53. boost::scoped_ptr<AuthCountersImpl> impl_;
  54. public:
  55. // Enum for the type of counter
  56. enum ServerCounterType {
  57. SERVER_UDP_QUERY, ///< SERVER_UDP_QUERY: counter for UDP queries
  58. SERVER_TCP_QUERY, ///< SERVER_TCP_QUERY: counter for TCP queries
  59. SERVER_COUNTER_TYPES ///< The number of defined counters
  60. };
  61. enum PerZoneCounterType {
  62. ZONE_UDP_QUERY, ///< ZONE_UDP_QUERY: counter for UDP queries
  63. ZONE_TCP_QUERY, ///< ZONE_TCP_QUERY: counter for TCP queries
  64. PER_ZONE_COUNTER_TYPES ///< The number of defined counters
  65. };
  66. /// The constructor.
  67. ///
  68. /// This constructor is mostly exception free. But it may still throw
  69. /// a standard exception if memory allocation fails inside the method.
  70. ///
  71. AuthCounters();
  72. /// The destructor.
  73. ///
  74. /// This method never throws an exception.
  75. ///
  76. ~AuthCounters();
  77. /// \brief Increment the counter specified by the parameter.
  78. ///
  79. /// \param type Type of a counter to increment.
  80. ///
  81. /// \throw std::out_of_range \a type is unknown.
  82. ///
  83. /// usage: counter.inc(AuthCounters::SERVER_UDP_QUERY);
  84. ///
  85. void inc(const ServerCounterType type);
  86. /// \brief Increment the counter of a per opcode counter.
  87. ///
  88. /// \note This is a tentative interface. See \c getCounter().
  89. ///
  90. /// \param opcode The opcode of the counter to increment.
  91. ///
  92. /// \throw None
  93. void inc(const isc::dns::Opcode opcode);
  94. /// \brief Increment the counter of a per rcode counter.
  95. ///
  96. /// \note This is a tentative interface. See \c getCounter().
  97. ///
  98. /// \param rcode The rcode of the counter to increment.
  99. ///
  100. /// \throw None
  101. void inc(const isc::dns::Rcode rcode);
  102. /// \brief Submit statistics counters to statistics module.
  103. ///
  104. /// This method is desinged to be called periodically
  105. /// with \c asio_link::StatisticsSendTimer, or arbitrary
  106. /// by the command 'sendstats'.
  107. ///
  108. /// Note: Set the session to communicate with statistics module
  109. /// by \c setStatisticsSession() before calling \c submitStatistics().
  110. ///
  111. /// This method is mostly exception free (error conditions are
  112. /// represented via the return value). But it may still throw
  113. /// a standard exception if memory allocation fails inside the method.
  114. ///
  115. /// \return true on success, false on error.
  116. ///
  117. /// \todo Do not block message handling in auth_srv while submitting
  118. /// statistics data.
  119. ///
  120. bool submitStatistics() const;
  121. /// \brief Set the session to communicate with Statistics
  122. /// module.
  123. ///
  124. /// This method never throws an exception.
  125. ///
  126. /// Note: this interface is tentative. We'll revisit the ASIO and session
  127. /// frameworks, at which point the session will probably be passed on
  128. /// construction of the server.
  129. ///
  130. /// Ownership isn't transferred: the caller is responsible for keeping
  131. /// this object to be valid while the server object is working and for
  132. /// disconnecting the session and destroying the object when the server
  133. /// is shutdown.
  134. ///
  135. /// \param statistics_session A pointer to the session
  136. ///
  137. void setStatisticsSession(isc::cc::AbstractSession* statistics_session);
  138. /// \brief Get the value of a counter in the AuthCounters.
  139. ///
  140. /// This function returns a value of the counter specified by \a type.
  141. /// This method never throws an exception.
  142. ///
  143. /// Note: Currently this function is for testing purpose only.
  144. ///
  145. /// \param type Type of a counter to get the value of
  146. ///
  147. /// \return the value of the counter specified by \a type.
  148. uint64_t getCounter(const AuthCounters::ServerCounterType type) const;
  149. /// \brief Get the value of a per opcode counter.
  150. ///
  151. /// This method returns the value of the per opcode counter for the
  152. /// specified \c opcode.
  153. ///
  154. /// \note This is a tentative interface as an attempt of experimentally
  155. /// supporting more statistics counters. This should eventually be more
  156. /// generalized. In any case, this method is mainly for testing.
  157. ///
  158. /// \throw None
  159. /// \param opcode The opcode of the counter to get the value of
  160. /// \return the value of the counter.
  161. uint64_t getCounter(const isc::dns::Opcode opcode) const;
  162. /// \brief Get the value of a per rcode counter.
  163. ///
  164. /// This method returns the value of the per rcode counter for the
  165. /// specified \c rcode.
  166. ///
  167. /// \note As mentioned in getCounter(const isc::dns::Opcode opcode),
  168. /// This is a tentative interface as an attempt of experimentally
  169. /// supporting more statistics counters. This should eventually be more
  170. /// generalized. In any case, this method is mainly for testing.
  171. ///
  172. /// \throw None
  173. /// \param rcode The rcode of the counter to get the value of
  174. /// \return the value of the counter.
  175. uint64_t getCounter(const isc::dns::Rcode rcode) const;
  176. /// \brief A type of validation function for the specification in
  177. /// isc::config::ModuleSpec.
  178. ///
  179. /// This type might be useful for not only statistics
  180. /// specificatoin but also for config_data specification and for
  181. /// commnad.
  182. ///
  183. typedef boost::function<bool(const isc::data::ConstElementPtr&)>
  184. validator_type;
  185. /// \brief Register a function type of the statistics validation
  186. /// function for AuthCounters.
  187. ///
  188. /// This method never throws an exception.
  189. ///
  190. /// \param validator A function type of the validation of
  191. /// statistics specification.
  192. ///
  193. void registerStatisticsValidator(AuthCounters::validator_type validator) const;
  194. };
  195. #endif // __STATISTICS_H
  196. // Local Variables:
  197. // mode: c++
  198. // End: