Apache Mesos
metrics.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_METRICS_HPP__
14 #define __PROCESS_METRICS_METRICS_HPP__
15 
16 #include <map>
17 #include <string>
18 
19 #include <process/dispatch.hpp>
20 #include <process/future.hpp>
21 #include <process/limiter.hpp>
22 #include <process/owned.hpp>
23 #include <process/process.hpp>
24 
26 
27 #include <stout/hashmap.hpp>
28 #include <stout/nothing.hpp>
29 #include <stout/option.hpp>
30 
31 namespace process {
32 namespace metrics {
33 namespace internal {
34 
35 class MetricsProcess : public Process<MetricsProcess>
36 {
37 public:
38  static MetricsProcess* create(const Option<std::string>& authenticationRealm);
39 
41 
42  Future<Nothing> remove(const std::string& name);
43 
45  const Option<Duration>& timeout);
46 
47 protected:
48  virtual void initialize();
49 
50 private:
51  static std::string help();
52 
54  const Option<Owned<RateLimiter>>& _limiter,
55  const Option<std::string>& _authenticationRealm)
56  : ProcessBase("metrics"),
57  limiter(_limiter),
58  authenticationRealm(_authenticationRealm)
59  {}
60 
61  // Non-copyable, non-assignable.
63  MetricsProcess& operator=(const MetricsProcess&);
64 
65  Future<http::Response> _snapshot(
66  const http::Request& request,
68 
69  static std::list<Future<double>> _snapshotTimeout(
70  const std::list<Future<double>>& futures);
71 
72  static Future<std::map<std::string, double>> __snapshot(
73  const Option<Duration>& timeout,
74  const hashmap<std::string, Future<double>>& metrics,
75  const hashmap<std::string, Option<Statistics<double>>>& statistics);
76 
77  // The Owned<Metric> is an explicit copy of the Metric passed to 'add'.
79 
80  // Used to rate limit the snapshot endpoint.
82 
83  // The authentication realm that metrics HTTP endpoints are installed into.
84  const Option<std::string> authenticationRealm;
85 };
86 
87 
88 // Global metrics process. Defined in process.cpp.
90 
91 } // namespace internal {
92 
93 
94 template <typename T>
95 Future<Nothing> add(const T& metric)
96 {
97  // The metrics process is instantiated in `process::initialize`.
99 
100  // There is an explicit copy in this call to ensure we end up owning
101  // the last copy of a Metric when we remove it.
102  return dispatch(
105  Owned<Metric>(new T(metric)));
106 }
107 
108 
109 inline Future<Nothing> remove(const Metric& metric)
110 {
111  // The metrics process is instantiated in `process::initialize`.
113 
114  return dispatch(
117  metric.name());
118 }
119 
120 
122  const Option<Duration>& timeout)
123 {
124  // The metrics process is instantiated in `process::initialize`.
126 
127  return dispatch(
130  timeout);
131 }
132 
133 } // namespace metrics {
134 } // namespace process {
135 
136 #endif // __PROCESS_METRICS_METRICS_HPP__
Future< Response > request(const Request &request, bool streamedResponse=false)
Asynchronously sends an HTTP request to the process and returns the HTTP response once the entire res...
ProcessBase(const std::string &id="")
Future< std::map< std::string, double > > snapshot(const Option< Duration > &timeout)
Definition: metrics.hpp:121
bool initialize(const Option< std::string > &delegate=None(), const Option< std::string > &readwriteAuthenticationRealm=None(), const Option< std::string > &readonlyAuthenticationRealm=None())
Initialize the library.
Definition: metric.hpp:33
Future< Nothing > remove(const std::string &name)
Future< Nothing > add(const T &metric)
Definition: metrics.hpp:95
Definition: http.hpp:518
Definition: hashmap.hpp:38
void dispatch(const PID< T > &pid, void(T::*method)())
Definition: dispatch.hpp:174
Future< std::map< std::string, double > > snapshot(const Option< Duration > &timeout)
virtual void initialize()
Invoked when a process gets spawned.
static MetricsProcess * create(const Option< std::string > &authenticationRealm)
Try< std::vector< Entry > > list(const std::string &hierarchy, const std::string &cgroup)
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
A &quot;process identifier&quot; used to uniquely identify a process when dispatching messages.
Definition: pid.hpp:279
Definition: owned.hpp:35
Future< Nothing > add(Owned< Metric > metric)
Definition: process.hpp:493
PID< MetricsProcess > metrics
constexpr const char * name
Definition: shell.hpp:41
Definition: statistics.hpp:30