Apache Mesos
pull_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_PULL_GAUGE_HPP__
14 #define __PROCESS_METRICS_PULL_GAUGE_HPP__
15 
16 #include <functional>
17 #include <memory>
18 #include <string>
19 
21 
22 namespace process {
23 namespace metrics {
24 
25 // A Metric that represents an instantaneous measurement of a value
26 // (e.g. number of items in a queue).
27 //
28 // A pull-based gauge differs from a push-based gauge in that the
29 // client does not need to worry about when to compute a new gauge
30 // value and instead provides the value function to invoke during
31 // metrics collection. This makes the client logic simpler but comes
32 // with significant performance disadvantages. If this function
33 // dispatches to a `Process`, metrics collection will be exposed to
34 // delays while the dispatches work their way through the event
35 // queues. Also, gauges can be expensive to compute (e.g. loop over
36 // all items while counting how many match a criteria) and therefore
37 // can lead to a performance degradation on critial `Process`es
38 // during metrics collection.
39 //
40 // NOTE: Due to the performance disadvantages of pull-based gauges,
41 // it is recommended to use push-based gauges where possible.
42 // Pull-based gauges should ideally (1) be used on `Process`es that
43 // experience very light load, and (2) use functions that do not
44 // perform heavyweight computation to compute the value (e.g. looping
45 // over a large collection).
46 class PullGauge : public Metric
47 {
48 public:
49  // 'name' is the unique name for the instance of Gauge being constructed.
50  // It will be the key exposed in the JSON endpoint.
51  //
52  // 'f' is the function that is called when the Metric value is requested.
53  // The user of `Gauge` must ensure that `f` is safe to execute up until
54  // the removal of the `Gauge` (via `process::metrics::remove(...)`) is
55  // complete.
56  PullGauge(const std::string& name, const std::function<Future<double>()>& f)
57  : Metric(name, None()), data(new Data(f)) {}
58 
59  ~PullGauge() override {}
60 
61  Future<double> value() const override { return data->f(); }
62 
63 private:
64  struct Data
65  {
66  explicit Data(const std::function<Future<double>()>& _f) : f(_f) {}
67 
68  const std::function<Future<double>()> f;
69  };
70 
71  std::shared_ptr<Data> data;
72 };
73 
74 } // namespace metrics {
75 } // namespace process {
76 
77 #endif // __PROCESS_METRICS_PULL_GAUGE_HPP__
F && f
Definition: defer.hpp:270
Definition: metric.hpp:33
PullGauge(const std::string &name, const std::function< Future< double >()> &f)
Definition: pull_gauge.hpp:56
Definition: pull_gauge.hpp:46
~PullGauge() override
Definition: pull_gauge.hpp:59
Future< double > value() const override
Definition: pull_gauge.hpp:61
Definition: none.hpp:27
const std::string & name() const
Definition: metric.hpp:39
Definition: executor.hpp:48
PID< MetricsProcess > metrics
Definition: future.hpp:58