family.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. #pragma once
  2. #include <cstddef>
  3. #include <map>
  4. #include <memory>
  5. #include <mutex>
  6. #include <string>
  7. #include <unordered_map>
  8. #include <vector>
  9. #include <cassert>
  10. #include "prometheus/collectable.h"
  11. #include "prometheus/metric.h"
  12. #include "prometheus/hash.h"
  13. namespace prometheus {
  14. /// \brief A metric of type T with a set of labeled dimensions.
  15. ///
  16. /// One of Prometheus main feature is a multi-dimensional data model with time
  17. /// series data identified by metric name and key/value pairs, also known as
  18. /// labels. A time series is a series of data points indexed (or listed or
  19. /// graphed) in time order (https://en.wikipedia.org/wiki/Time_series).
  20. ///
  21. /// An instance of this class is exposed as multiple time series during
  22. /// scrape, i.e., one time series for each set of labels provided to Add().
  23. ///
  24. /// For example it is possible to collect data for a metric
  25. /// `http_requests_total`, with two time series:
  26. ///
  27. /// - all HTTP requests that used the method POST
  28. /// - all HTTP requests that used the method GET
  29. ///
  30. /// The metric name specifies the general feature of a system that is
  31. /// measured, e.g., `http_requests_total`. Labels enable Prometheus's
  32. /// dimensional data model: any given combination of labels for the same
  33. /// metric name identifies a particular dimensional instantiation of that
  34. /// metric. For example a label for 'all HTTP requests that used the method
  35. /// POST' can be assigned with `method= "POST"`.
  36. ///
  37. /// Given a metric name and a set of labels, time series are frequently
  38. /// identified using this notation:
  39. ///
  40. /// <metric name> { < label name >= <label value>, ... }
  41. ///
  42. /// It is required to follow the syntax of metric names and labels given by:
  43. /// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
  44. ///
  45. /// The following metric and label conventions are not required for using
  46. /// Prometheus, but can serve as both a style-guide and a collection of best
  47. /// practices: https://prometheus.io/docs/practices/naming/
  48. ///
  49. /// tparam T One of the metric types Counter, Gauge, Histogram or Summary.
  50. class Family : public Collectable {
  51. public:
  52. using Hash = std::size_t;
  53. using Label = std::pair<const std::string, const std::string>;
  54. using Labels = std::map <const std::string, const std::string>;
  55. using MetricPtr = std::unique_ptr<Metric>;
  56. const Metric::Type type;
  57. const std::string name;
  58. const std::string help;
  59. const Labels constant_labels;
  60. mutable std::mutex mutex;
  61. std::unordered_map<Hash, MetricPtr> metrics;
  62. std::unordered_map<Hash, Labels> labels;
  63. std::unordered_map<Metric*, Hash> labels_reverse_lookup;
  64. /// \brief Compute the hash value of a map of labels.
  65. ///
  66. /// \param labels The map that will be computed the hash value.
  67. ///
  68. /// \returns The hash value of the given labels.
  69. static Hash hash_labels (const Labels& labels) {
  70. size_t seed = 0;
  71. for (const Label& label : labels)
  72. detail::hash_combine (&seed, label.first, label.second);
  73. return seed;
  74. }
  75. static bool isLocaleIndependentDigit (char c) { return '0' <= c && c <= '9'; }
  76. static bool isLocaleIndependentAlphaNumeric (char c) { return isLocaleIndependentDigit(c) || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z'); }
  77. bool nameStartsValid (const std::string& name) {
  78. if (name.empty()) return false; // must not be empty
  79. if (isLocaleIndependentDigit(name.front())) return false; // must not start with a digit
  80. if (name.compare(0, 2, "__") == 0) return false; // must not start with "__"
  81. return true;
  82. }
  83. /// \brief Check if the metric name is valid
  84. ///
  85. /// The metric name regex is "[a-zA-Z_:][a-zA-Z0-9_:]*"
  86. ///
  87. /// \see https://prometheus.io/docs/concepts/data_model/
  88. ///
  89. /// \param name metric name
  90. /// \return true is valid, false otherwise
  91. bool CheckMetricName (const std::string& name) {
  92. if (!nameStartsValid(name))
  93. return false;
  94. for (const char& c : name)
  95. if ( !isLocaleIndependentAlphaNumeric(c) && c != '_' && c != ':' )
  96. return false;
  97. return true;
  98. }
  99. /// \brief Check if the label name is valid
  100. ///
  101. /// The label name regex is "[a-zA-Z_][a-zA-Z0-9_]*"
  102. ///
  103. /// \see https://prometheus.io/docs/concepts/data_model/
  104. ///
  105. /// \param name label name
  106. /// \return true is valid, false otherwise
  107. bool CheckLabelName (const std::string& name) {
  108. if (!nameStartsValid(name))
  109. return false;
  110. for (const char& c : name)
  111. if (!isLocaleIndependentAlphaNumeric(c) && c != '_')
  112. return false;
  113. return true;
  114. }
  115. /// \brief Create a new metric.
  116. ///
  117. /// Every metric is uniquely identified by its name and a set of key-value
  118. /// pairs, also known as labels. Prometheus's query language allows filtering
  119. /// and aggregation based on metric name and these labels.
  120. ///
  121. /// This example selects all time series that have the `http_requests_total`
  122. /// metric name:
  123. ///
  124. /// http_requests_total
  125. ///
  126. /// It is possible to assign labels to the metric name. These labels are
  127. /// propagated to each dimensional data added with Add(). For example if a
  128. /// label `job= "prometheus"` is provided to this constructor, it is possible
  129. /// to filter this time series with Prometheus's query language by appending
  130. /// a set of labels to match in curly braces ({})
  131. ///
  132. /// http_requests_total{job= "prometheus"}
  133. ///
  134. /// For further information see: [Quering Basics]
  135. /// (https://prometheus.io/docs/prometheus/latest/querying/basics/)
  136. ///
  137. /// \param name Set the metric name.
  138. /// \param help Set an additional description.
  139. /// \param constant_labels Assign a set of key-value pairs (= labels) to the
  140. /// metric. All these labels are propagated to each time series within the
  141. /// metric.
  142. /// \throw std::runtime_exception on invalid metric or label names.
  143. Family (Metric::Type type_, const std::string& name_, const std::string& help_, const Labels& constant_labels_)
  144. : type(type_), name(name_), help(help_), constant_labels(constant_labels_) {
  145. if (!CheckMetricName(name_))
  146. throw std::invalid_argument("Invalid metric name");
  147. for (const Label& label_pair : constant_labels) {
  148. const std::string& label_name = label_pair.first;
  149. if (!CheckLabelName(label_name))
  150. throw std::invalid_argument("Invalid label name");
  151. }
  152. }
  153. /// \brief Remove the given dimensional data.
  154. ///
  155. /// \param metric Dimensional data to be removed. The function does nothing,
  156. /// if the given metric was not returned by Add().
  157. void Remove (Metric* metric) {
  158. std::lock_guard<std::mutex> lock{ mutex };
  159. if (labels_reverse_lookup.count(metric) == 0)
  160. return;
  161. const Hash hash = labels_reverse_lookup.at(metric);
  162. metrics.erase(hash);
  163. labels.erase(hash);
  164. labels_reverse_lookup.erase(metric);
  165. }
  166. /// \brief Returns true if the dimensional data with the given labels exist
  167. ///
  168. /// \param labels A set of key-value pairs (= labels) of the dimensional data.
  169. bool Has (const Labels& labels) const {
  170. const Hash hash = hash_labels (labels);
  171. std::lock_guard<std::mutex> lock{ mutex };
  172. return metrics.find(hash) != metrics.end();
  173. }
  174. /// \brief Returns the name for this family.
  175. ///
  176. /// \return The family name.
  177. const std::string& GetName() const {
  178. return name;
  179. }
  180. /// \brief Returns the constant labels for this family.
  181. ///
  182. /// \return All constant labels as key-value pairs.
  183. const Labels& GetConstantLabels() const {
  184. return constant_labels;
  185. }
  186. /// \brief Returns the current value of each dimensional data.
  187. ///
  188. /// Collect is called by the Registry when collecting metrics.
  189. ///
  190. /// \return Zero or more samples for each dimensional data.
  191. MetricFamilies Collect() const override {
  192. std::lock_guard<std::mutex> lock{ mutex };
  193. if (metrics.empty())
  194. return {};
  195. MetricFamily family = MetricFamily{};
  196. family.type = type;
  197. family.name = name;
  198. family.help = help;
  199. family.metric.reserve(metrics.size());
  200. for (const std::pair<const Hash, MetricPtr>& metric_pair : metrics) {
  201. ClientMetric collected = metric_pair.second->Collect();
  202. for (const Label& constant_label : constant_labels)
  203. collected.label.emplace_back(ClientMetric::Label(constant_label.first, constant_label.second));
  204. const Labels& metric_labels = labels.at(metric_pair.first);
  205. for (const Label& metric_label : metric_labels)
  206. collected.label.emplace_back(ClientMetric::Label(metric_label.first, metric_label.second));
  207. family.metric.push_back(std::move(collected));
  208. }
  209. return { family };
  210. }
  211. };
  212. template <typename CustomMetric>
  213. class CustomFamily : public Family {
  214. public:
  215. static const Metric::Type static_type = CustomMetric::static_type;
  216. CustomFamily(const std::string& name, const std::string& help, const Family::Labels& constant_labels)
  217. : Family(static_type, name, help, constant_labels) {}
  218. /// \brief Add a new dimensional data.
  219. ///
  220. /// Each new set of labels adds a new dimensional data and is exposed in
  221. /// Prometheus as a time series. It is possible to filter the time series
  222. /// with Prometheus's query language by appending a set of labels to match in
  223. /// curly braces ({})
  224. ///
  225. /// http_requests_total{job= "prometheus",method= "POST"}
  226. ///
  227. /// \param labels Assign a set of key-value pairs (= labels) to the
  228. /// dimensional data. The function does nothing, if the same set of labels
  229. /// already exists.
  230. /// \param args Arguments are passed to the constructor of metric type T. See
  231. /// Counter, Gauge, Histogram or Summary for required constructor arguments.
  232. /// \return Return the newly created dimensional data or - if a same set of
  233. /// labels already exists - the already existing dimensional data.
  234. /// \throw std::runtime_exception on invalid label names.
  235. template <typename... Args>
  236. CustomMetric& Add (const Labels& new_labels, Args&&... args) {
  237. const Hash hash = hash_labels (new_labels);
  238. std::lock_guard<std::mutex> lock{ mutex };
  239. // try to find existing one
  240. auto metrics_iter = metrics.find(hash);
  241. if (metrics_iter != metrics.end()) {
  242. #ifndef NDEBUG
  243. // check that we have stored labels for this existing metric
  244. auto labels_iter = labels.find(hash);
  245. assert(labels_iter != labels.end());
  246. const Labels& stored_labels = labels_iter->second;
  247. assert(new_labels == stored_labels);
  248. #endif
  249. return dynamic_cast<CustomMetric&>(*metrics_iter->second);
  250. }
  251. // check labels before create the new one
  252. for (const Label& label_pair : new_labels) {
  253. const std::string& label_name = label_pair.first;
  254. if (!CheckLabelName(label_name))
  255. throw std::invalid_argument("Invalid label name");
  256. if (constant_labels.count(label_name))
  257. throw std::invalid_argument("Label name already present in constant labels");
  258. }
  259. // create new one
  260. std::unique_ptr<CustomMetric> metric_ptr (new CustomMetric(std::forward<Args>(args)...));
  261. CustomMetric& metric = *metric_ptr;
  262. const auto stored_metric = metrics.insert(std::make_pair(hash, std::move(metric_ptr)));
  263. assert(stored_metric.second);
  264. labels.insert({ hash, new_labels });
  265. labels_reverse_lookup.insert({ stored_metric.first->second.get(), hash });
  266. return metric;
  267. }
  268. /// \brief Return a builder to configure and register a Counter metric.
  269. ///
  270. /// @copydetails family_base_t<>::family_base_t()
  271. ///
  272. /// Example usage:
  273. ///
  274. /// \code
  275. /// auto registry = std::make_shared<Registry>();
  276. /// auto& counter_family = prometheus::Counter_family::build("some_name", "Additional description.", {{"key", "value"}}, *registry);
  277. ///
  278. /// ...
  279. /// \endcode
  280. ///
  281. /// \return An object of unspecified type T, i.e., an implementation detail
  282. /// except that it has the following members:
  283. ///
  284. /// - Name(const std::string&) to set the metric name,
  285. /// - Help(const std::string&) to set an additional description.
  286. /// - Label(const std::map<std::string, std::string>&) to assign a set of
  287. /// key-value pairs (= labels) to the metric.
  288. ///
  289. /// To finish the configuration of the Counter metric, register it with
  290. /// Register(Registry&).
  291. template <typename Registry>
  292. static CustomFamily& Build(Registry& registry, const std::string& name, const std::string& help, const Family::Labels& labels = Family::Labels()) {
  293. return registry.template Add<CustomFamily>(name, help, labels);
  294. }
  295. };
  296. } // namespace prometheus