counter.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #pragma once
  2. #include "prometheus/atomic_floating.h"
  3. #include "prometheus/metric.h"
  4. #include "prometheus/family.h"
  5. #include "prometheus/builder.h"
  6. #include <atomic>
  7. namespace prometheus {
  8. /// \brief A counter metric to represent a monotonically increasing value.
  9. ///
  10. /// This class represents the metric type counter:
  11. /// https://prometheus.io/docs/concepts/metric_types/#counter
  12. ///
  13. /// The value of the counter can only increase. Example of counters are:
  14. /// - the number of requests served
  15. /// - tasks completed
  16. /// - errors
  17. ///
  18. /// Do not use a counter to expose a value that can decrease - instead use a
  19. /// Gauge.
  20. ///
  21. /// The class is thread-safe. No concurrent call to any API of this type causes
  22. /// a data race.
  23. template <typename Value_ = uint64_t>
  24. class Counter : public Metric {
  25. std::atomic<Value_> value{ 0 };
  26. public:
  27. using Value = Value_;
  28. using Family = CustomFamily<Counter<Value>>;
  29. static const Metric::Type static_type = Metric::Type::Counter;
  30. Counter() : Metric (Metric::Type::Counter) {} ///< \brief Create a counter that starts at 0.
  31. Counter(const Counter<Value_> &rhs) : Metric(static_type), value{rhs.value.load()} {}
  32. // original API
  33. void Increment() { ///< \brief Increment the counter by 1.
  34. ++value;
  35. }
  36. void Increment(const Value& val) { ///< \brief Increment the counter by a given amount. The counter will not change if the given amount is negative.
  37. if (val > 0)
  38. value += val;
  39. }
  40. const Value Get() const { ///< \brief Get the current value of the counter.
  41. return value;
  42. }
  43. virtual ClientMetric Collect() const { ///< /// \brief Get the current value of the counter. Collect is called by the Registry when collecting metrics.
  44. ClientMetric metric;
  45. metric.counter.value = static_cast<double>(value);
  46. return metric;
  47. }
  48. // new API
  49. Counter& operator ++() {
  50. ++value;
  51. return *this;
  52. }
  53. Counter& operator++ (int) {
  54. ++value;
  55. return *this;
  56. }
  57. Counter& operator += (const Value& val) {
  58. value += val;
  59. return *this;
  60. }
  61. };
  62. /// \brief Return a builder to configure and register a Counter metric.
  63. ///
  64. /// @copydetails Family<>::Family()
  65. ///
  66. /// Example usage:
  67. ///
  68. /// \code
  69. /// auto registry = std::make_shared<Registry>();
  70. /// auto& counter_family = prometheus::BuildCounter()
  71. /// .Name("some_name")
  72. /// .Help("Additional description.")
  73. /// .Labels({{"key", "value"}})
  74. /// .Register(*registry);
  75. ///
  76. /// ...
  77. /// \endcode
  78. ///
  79. /// \return An object of unspecified type T, i.e., an implementation detail
  80. /// except that it has the following members:
  81. ///
  82. /// - Name(const std::string&) to set the metric name,
  83. /// - Help(const std::string&) to set an additional description.
  84. /// - Label(const std::map<std::string, std::string>&) to assign a set of
  85. /// key-value pairs (= labels) to the metric.
  86. ///
  87. /// To finish the configuration of the Counter metric, register it with
  88. /// Register(Registry&).
  89. using BuildCounter = Builder<Counter<double>>;
  90. } // namespace prometheus