counter.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include <stdint.h>
  24. #if UINTPTR_MAX == 0xffFFffFF
  25. // 32-bit platform
  26. template <typename Value_ = uint32_t>
  27. #elif UINTPTR_MAX == 0xffFFffFFffFFffFF
  28. // 64-bit platform
  29. template <typename Value_ = uint64_t>
  30. #else
  31. #error Unknown platform - does not look either like 32-bit or 64-bit
  32. #endif
  33. class Counter : public Metric {
  34. std::atomic<Value_> value{ 0 };
  35. public:
  36. using Value = Value_;
  37. using Family = CustomFamily<Counter<Value>>;
  38. static const Metric::Type static_type = Metric::Type::Counter;
  39. Counter() : Metric (Metric::Type::Counter) {} ///< \brief Create a counter that starts at 0.
  40. Counter(const Counter<Value_> &rhs) : Metric(static_type), value{rhs.value.load()} {}
  41. // original API
  42. void Increment() { ///< \brief Increment the counter by 1.
  43. ++value;
  44. }
  45. void Increment(const Value& val) { ///< \brief Increment the counter by a given amount. The counter will not change if the given amount is negative.
  46. if (val > 0)
  47. value += val;
  48. }
  49. const Value Get() const { ///< \brief Get the current value of the counter.
  50. return value;
  51. }
  52. virtual ClientMetric Collect() const { ///< /// \brief Get the current value of the counter. Collect is called by the Registry when collecting metrics.
  53. ClientMetric metric;
  54. metric.counter.value = static_cast<double>(value);
  55. return metric;
  56. }
  57. // new API
  58. Counter& operator ++() {
  59. ++value;
  60. return *this;
  61. }
  62. Counter& operator++ (int) {
  63. ++value;
  64. return *this;
  65. }
  66. Counter& operator += (const Value& val) {
  67. value += val;
  68. return *this;
  69. }
  70. };
  71. /// \brief Return a builder to configure and register a Counter metric.
  72. ///
  73. /// @copydetails Family<>::Family()
  74. ///
  75. /// Example usage:
  76. ///
  77. /// \code
  78. /// auto registry = std::make_shared<Registry>();
  79. /// auto& counter_family = prometheus::BuildCounter()
  80. /// .Name("some_name")
  81. /// .Help("Additional description.")
  82. /// .Labels({{"key", "value"}})
  83. /// .Register(*registry);
  84. ///
  85. /// ...
  86. /// \endcode
  87. ///
  88. /// \return An object of unspecified type T, i.e., an implementation detail
  89. /// except that it has the following members:
  90. ///
  91. /// - Name(const std::string&) to set the metric name,
  92. /// - Help(const std::string&) to set an additional description.
  93. /// - Label(const std::map<std::string, std::string>&) to assign a set of
  94. /// key-value pairs (= labels) to the metric.
  95. ///
  96. /// To finish the configuration of the Counter metric, register it with
  97. /// Register(Registry&).
  98. using BuildCounter = Builder<Counter<double>>;
  99. } // namespace prometheus