gauge.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #include <ctime>
  8. namespace prometheus {
  9. /// \brief A gauge metric to represent a value that can arbitrarily go up and
  10. /// down.
  11. ///
  12. /// The class represents the metric type gauge:
  13. /// https://prometheus.io/docs/concepts/metric_types/#gauge
  14. ///
  15. /// Gauges are typically used for measured values like temperatures or current
  16. /// memory usage, but also "counts" that can go up and down, like the number of
  17. /// running processes.
  18. ///
  19. /// The class is thread-safe. No concurrent call to any API of this type causes
  20. /// a data race.
  21. template <typename Value_ = uint64_t>
  22. class Gauge : public Metric {
  23. std::atomic<Value_> value { 0 };
  24. public:
  25. using Value = Value_;
  26. using Family = CustomFamily<Gauge<Value>>;
  27. static const Metric::Type static_type = Metric::Type::Gauge;
  28. Gauge() : Metric (static_type) {} ///< \brief Create a gauge that starts at 0.
  29. Gauge(const Value value_) : Metric(static_type), value{ value_ } {} ///< \brief Create a gauge that starts at the given amount.
  30. Gauge(const Gauge<Value_> &rhs) : Metric(rhs.static_type), value{rhs.value.load()} {}
  31. // original API
  32. void Increment() { ++value; } ///< \brief Increment the gauge by 1.
  33. void Increment(const Value& val) { value += val; } ///< \brief Increment the gauge by the given amount.
  34. void Decrement() { --value; } ///< \brief Decrement the gauge by 1.
  35. void Decrement(const Value& val) { value -= val; } ///< \brief Decrement the gauge by the given amount.
  36. void SetToCurrentTime() { ///< \brief Set the gauge to the current unixtime in seconds.
  37. const time_t time = std::time(nullptr);
  38. value = static_cast<Value>(time);
  39. }
  40. void Set(const Value& val) { value = val; } ///< \brief Set the gauge to the given value.
  41. const Value Get() const { return value; } ///< \brief Get the current value of the gauge.
  42. virtual ClientMetric Collect() const { ///< \brief Get the current value of the gauge. Collect is called by the Registry when collecting metrics.
  43. ClientMetric metric;
  44. metric.gauge.value = static_cast<double>(value);
  45. return metric;
  46. }
  47. // new API
  48. Gauge& operator ++() {
  49. ++value;
  50. return *this;
  51. }
  52. Gauge& operator++ (int) {
  53. ++value;
  54. return *this;
  55. }
  56. Gauge& operator --() {
  57. --value;
  58. return *this;
  59. }
  60. Gauge& operator-- (int) {
  61. --value;
  62. return *this;
  63. }
  64. Gauge& operator+=(const Value& val) {
  65. value += val;
  66. return *this;
  67. }
  68. Gauge& operator-=(const Value& val) {
  69. value -= val;
  70. return *this;
  71. }
  72. };
  73. /// \brief Return a builder to configure and register a Gauge metric.
  74. ///
  75. /// @copydetails Family<>::Family()
  76. ///
  77. /// Example usage:
  78. ///
  79. /// \code
  80. /// auto registry = std::make_shared<Registry>();
  81. /// auto& gauge_family = prometheus::BuildGauge()
  82. /// .Name("some_name")
  83. /// .Help("Additional description.")
  84. /// .Labels({{"key", "value"}})
  85. /// .Register(*registry);
  86. ///
  87. /// ...
  88. /// \endcode
  89. ///
  90. /// \return An object of unspecified type T, i.e., an implementation detail
  91. /// except that it has the following members:
  92. ///
  93. /// - Name(const std::string&) to set the metric name,
  94. /// - Help(const std::string&) to set an additional description.
  95. /// - Label(const std::map<std::string, std::string>&) to assign a set of
  96. /// key-value pairs (= labels) to the metric.
  97. ///
  98. /// To finish the configuration of the Gauge metric register it with
  99. /// Register(Registry&).
  100. using BuildGauge = Builder<Gauge<double>>;
  101. } // namespace prometheus