A Metric that represents an integer value that can be incremented and decremented.
| 24 | // A Metric that represents an integer value that can be incremented and |
| 25 | // decremented. |
| 26 | class Counter : public Metric |
| 27 | { |
| 28 | public: |
| 29 | // 'name' is the unique name for the instance of Counter being constructed. |
| 30 | // This is what will be used as the key in the JSON endpoint. |
| 31 | // 'window' is the amount of history to keep for this Metric. |
| 32 | Counter(const std::string& name, const Option<Duration>& window = None()) |
| 33 | : Metric(name, window), |
| 34 | data(new Data()) |
| 35 | { |
| 36 | push(static_cast<double>(data->value.load())); |
| 37 | } |
| 38 | |
| 39 | ~Counter() override {} |
| 40 | |
| 41 | Future<double> value() const override |
| 42 | { |
| 43 | return static_cast<double>(data->value.load()); |
| 44 | } |
| 45 | |
| 46 | void reset() |
| 47 | { |
| 48 | data->value.store(0); |
| 49 | push(0); |
| 50 | } |
| 51 | |
| 52 | Counter& operator++() |
| 53 | { |
| 54 | return *this += 1; |
| 55 | } |
| 56 | |
| 57 | Counter operator++(int) |
| 58 | { |
| 59 | Counter c(*this); |
| 60 | ++(*this); |
| 61 | return c; |
| 62 | } |
| 63 | |
| 64 | Counter& operator+=(int64_t v) |
| 65 | { |
| 66 | int64_t prev = data->value.fetch_add(v); |
| 67 | push(static_cast<double>(prev + v)); |
| 68 | return *this; |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | struct Data |
| 73 | { |
| 74 | explicit Data() : value(0) {} |
| 75 | |
| 76 | std::atomic<int64_t> value; |
| 77 | }; |
| 78 | |
| 79 | std::shared_ptr<Data> data; |
| 80 | }; |
| 81 | |
| 82 | } // namespace metrics { |
| 83 | } // namespace process { |