1 // This file is part of CAF, the C++ Actor Framework. See the file LICENSE in 2 // the main distribution directory for license terms and copyright or visit 3 // https://github.com/actor-framework/actor-framework/blob/master/LICENSE. 4 5 #pragma once 6 7 #include "caf/detail/core_export.hpp" 8 9 #include <atomic> 10 #include <cstdint> 11 12 #include "caf/fwd.hpp" 13 #include "caf/span.hpp" 14 #include "caf/telemetry/label.hpp" 15 #include "caf/telemetry/metric_type.hpp" 16 17 namespace caf::telemetry { 18 19 /// A metric that represents a single integer value that can arbitrarily go up 20 /// and down. 21 class CAF_CORE_EXPORT int_gauge { 22 public: 23 // -- member types ----------------------------------------------------------- 24 25 using value_type = int64_t; 26 27 using family_setting = unit_t; 28 29 // -- constants -------------------------------------------------------------- 30 31 static constexpr metric_type runtime_type = metric_type::int_gauge; 32 33 // -- constructors, destructors, and assignment operators -------------------- 34 int_gauge()35 int_gauge() noexcept : value_(0) { 36 // nop 37 } 38 int_gauge(int64_t value)39 explicit int_gauge(int64_t value) noexcept : value_(value) { 40 // nop 41 } 42 int_gauge(span<const label>)43 explicit int_gauge(span<const label>) noexcept : value_(0) { 44 // nop 45 } 46 47 // -- modifiers -------------------------------------------------------------- 48 49 /// Increments the gauge by 1. inc()50 void inc() noexcept { 51 ++value_; 52 } 53 54 /// Increments the gauge by `amount`. inc(int64_t amount)55 void inc(int64_t amount) noexcept { 56 value_.fetch_add(amount); 57 } 58 59 /// Decrements the gauge by 1. dec()60 void dec() noexcept { 61 --value_; 62 } 63 64 /// Decrements the gauge by `amount`. dec(int64_t amount)65 void dec(int64_t amount) noexcept { 66 value_.fetch_sub(amount); 67 } 68 69 /// Sets the gauge to `x`. value(int64_t x)70 void value(int64_t x) noexcept { 71 value_.store(x); 72 } 73 74 /// Increments the gauge by 1. 75 /// @returns The new value of the gauge. operator ++()76 int64_t operator++() noexcept { 77 return ++value_; 78 } 79 80 /// Decrements the gauge by 1. 81 /// @returns The new value of the gauge. operator --()82 int64_t operator--() noexcept { 83 return --value_; 84 } 85 86 // -- observers -------------------------------------------------------------- 87 88 /// Returns the current value of the gauge. value() const89 int64_t value() const noexcept { 90 return value_.load(); 91 } 92 93 private: 94 std::atomic<int64_t> value_; 95 }; 96 97 } // namespace caf::telemetry 98