Apache Mesos
push_gauge.hpp
Go to the documentation of this file.
1 // Licensed under the Apache License, Version 2.0 (the "License");
2 // you may not use this file except in compliance with the License.
3 // You may obtain a copy of the License at
4 //
5 // http://www.apache.org/licenses/LICENSE-2.0
6 //
7 // Unless required by applicable law or agreed to in writing, software
8 // distributed under the License is distributed on an "AS IS" BASIS,
9 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10 // See the License for the specific language governing permissions and
11 // limitations under the License
12 
13 #ifndef __PROCESS_METRICS_PUSH_GAUGE_HPP__
14 #define __PROCESS_METRICS_PUSH_GAUGE_HPP__
15 
16 #include <memory>
17 #include <string>
18 
20 
21 namespace process {
22 namespace metrics {
23 
24 // A Metric that represents an instantaneous measurement of a value
25 // (e.g. number of items in a queue).
26 //
27 // A push-based gauge differs from a pull-based gauge in that the
28 // client is responsible for pushing the latest value into the gauge
29 // whenever it changes. This can be challenging in some cases as it
30 // requires the client to have a good handle on when the gauge value
31 // changes (rather than just computing the current value when asked).
32 //
33 // NOTE: It is highly recommended to use push-based gauges if
34 // possible as they provide significant performance benefits over
35 // pull-based gauges. Pull-based gauge suffer from delays getting
36 // processed on the event queue of a `Process`, as well as incur
37 // computation cost on the `Process` each time the metrics are
38 // collected. Push-based gauges, on the other hand, incur no cost
39 // to the owning `Process` when metrics are collected, and instead
40 // incur a trivial cost when the `Process` pushes new values in.
41 class PushGauge : public Metric
42 {
43 public:
44  // 'name' is the unique name for the instance of Gauge being constructed.
45  // It will be the key exposed in the JSON endpoint.
46  //
47  // 'f' is the function that is called when the Metric value is requested.
48  // The user of `Gauge` must ensure that `f` is safe to execute up until
49  // the removal of the `Gauge` (via `process::metrics::remove(...)`) is
50  // complete.
51  explicit PushGauge(const std::string& name)
52  : Metric(name, None()), data(new Data()) {}
53 
54  ~PushGauge() override {}
55 
56  Future<double> value() const override
57  {
58  return static_cast<double>(data->value.load());
59  }
60 
61  PushGauge& operator=(double v)
62  {
63  data->value.store(v);
64  push(v);
65  return *this;
66  }
67 
68  PushGauge& operator++() { return *this += 1; }
69 
70  PushGauge& operator+=(double v)
71  {
72  double prev;
73 
74  while (true) {
75  prev = data->value.load();
76 
77  if (data->value.compare_exchange_weak(prev, prev + v)) {
78  break;
79  }
80  }
81 
82  push(static_cast<double>(prev + v));
83  return *this;
84  }
85 
86  PushGauge& operator--() { return *this -= 1; }
87 
88  PushGauge& operator-=(double v)
89  {
90  double prev;
91 
92  while (true) {
93  prev = data->value.load();
94 
95  if (data->value.compare_exchange_weak(prev, prev - v)) {
96  break;
97  }
98  }
99 
100  push(static_cast<double>(prev - v));
101  return *this;
102  }
103 
104 private:
105  struct Data
106  {
107  explicit Data() : value(0) {}
108 
109  std::atomic<double> value;
110  };
111 
112  std::shared_ptr<Data> data;
113 };
114 
115 } // namespace metrics {
116 } // namespace process {
117 
118 #endif // __PROCESS_METRICS_PUSH_GAUGE_HPP__
PushGauge & operator--()
Definition: push_gauge.hpp:86
PushGauge & operator++()
Definition: push_gauge.hpp:68
PushGauge & operator=(double v)
Definition: push_gauge.hpp:61
Definition: metric.hpp:33
Future< double > value() const override
Definition: push_gauge.hpp:56
PushGauge & operator-=(double v)
Definition: push_gauge.hpp:88
~PushGauge() override
Definition: push_gauge.hpp:54
Definition: none.hpp:27
const std::string & name() const
Definition: metric.hpp:39
Definition: executor.hpp:48
PushGauge(const std::string &name)
Definition: push_gauge.hpp:51
PushGauge & operator+=(double v)
Definition: push_gauge.hpp:70
void push(double value)
Definition: metric.hpp:63
Definition: push_gauge.hpp:41
PID< MetricsProcess > metrics
Definition: future.hpp:58