Apache Mesos
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_GAUGE_HPP__
14 #define __PROCESS_METRICS_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 value evaluated when
26 // 'value' is called.
27 class Gauge : public Metric
28 {
29 public:
30  // 'name' is the unique name for the instance of Gauge being constructed.
31  // It will be the key exposed in the JSON endpoint.
32  //
33  // 'f' is the function that is called when the Metric value is requested.
34  // The user of `Gauge` must ensure that `f` is safe to execute up until
35  // the removal of the `Gauge` (via `process::metrics::remove(...)`) is
36  // complete.
37  Gauge(const std::string& name, const std::function<Future<double>()>& f)
38  : Metric(name, None()), data(new Data(f)) {}
39 
40  virtual ~Gauge() {}
41 
42  virtual Future<double> value() const { return data->f(); }
43 
44 private:
45  struct Data
46  {
47  explicit Data(const std::function<Future<double>()>& _f) : f(_f) {}
48 
49  const std::function<Future<double>()> f;
50  };
51 
52  std::shared_ptr<Data> data;
53 };
54 
55 } // namespace metrics {
56 } // namespace process {
57 
58 #endif // __PROCESS_METRICS_GAUGE_HPP__
F && f
Definition: defer.hpp:270
Definition: metric.hpp:33
virtual Future< double > value() const
Definition: gauge.hpp:42
Definition: gauge.hpp:27
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
Definition: none.hpp:27
const std::string & name() const
Definition: metric.hpp:39
virtual ~Gauge()
Definition: gauge.hpp:40
PID< MetricsProcess > metrics
Definition: future.hpp:57