Apache Mesos
metric.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_METRIC_HPP__
14 #define __PROCESS_METRICS_METRIC_HPP__
15 
16 #include <atomic>
17 #include <memory>
18 #include <string>
19 
20 #include <process/future.hpp>
21 #include <process/owned.hpp>
22 #include <process/statistics.hpp>
23 #include <process/timeseries.hpp>
24 
25 #include <stout/duration.hpp>
26 #include <stout/option.hpp>
27 #include <stout/synchronized.hpp>
28 
29 namespace process {
30 namespace metrics {
31 
32 // The base class for Metrics.
33 class Metric {
34 public:
35  virtual ~Metric() {}
36 
37  virtual Future<double> value() const = 0;
38 
39  const std::string& name() const
40  {
41  return data->name;
42  }
43 
45  {
47 
48  if (data->history.isSome()) {
49  synchronized (data->lock) {
50  statistics = Statistics<double>::from(*data->history.get());
51  }
52  }
53 
54  return statistics;
55  }
56 
57 protected:
58  // Only derived classes can construct.
59  Metric(const std::string& name, const Option<Duration>& window)
60  : data(new Data(name, window)) {}
61 
62  // Inserts 'value' into the history for this metric.
63  void push(double value) {
64  if (data->history.isSome()) {
65  Time now = Clock::now();
66 
67  synchronized (data->lock) {
68  data->history.get()->set(value, now);
69  }
70  }
71  }
72 
73 private:
74  struct Data {
75  Data(const std::string& _name, const Option<Duration>& window)
76  : name(_name),
77  history(None())
78  {
79  if (window.isSome()) {
80  history =
82  }
83  }
84 
85  const std::string name;
86 
87  std::atomic_flag lock = ATOMIC_FLAG_INIT;
88 
90  };
91 
92  std::shared_ptr<Data> data;
93 };
94 
95 } // namespace metrics {
96 } // namespace process {
97 
98 #endif // __PROCESS_METRICS_METRIC_HPP__
Definition: option.hpp:29
Metric(const std::string &name, const Option< Duration > &window)
Definition: metric.hpp:59
Definition: metric.hpp:33
Option< Statistics< double > > statistics() const
Definition: metric.hpp:44
virtual Future< double > value() const =0
bool isSome() const
Definition: option.hpp:116
virtual ~Metric()
Definition: metric.hpp:35
static Option< Statistics< T > > from(const TimeSeries< T > &timeseries)
Definition: statistics.hpp:43
const T & get() const &
Definition: option.hpp:119
Definition: time.hpp:23
Definition: none.hpp:27
const std::string & name() const
Definition: metric.hpp:39
Definition: executor.hpp:48
static Time now()
The current clock time for either the current process that makes this call or the global clock time i...
void push(double value)
Definition: metric.hpp:63
Definition: owned.hpp:36
PID< MetricsProcess > metrics
Definition: timeseries.hpp:52
Definition: future.hpp:58