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 #include <vector>
19 
20 #include <process/dispatch.hpp>
21 #include <process/future.hpp>
22 #include <process/limiter.hpp>
23 #include <process/owned.hpp>
24 #include <process/process.hpp>
25 
27 
28 #include <stout/hashmap.hpp>
29 #include <stout/nothing.hpp>
30 #include <stout/option.hpp>
31 
32 namespace process {
33 namespace metrics {
34 namespace internal {
35 
36 class MetricsProcess : public Process<MetricsProcess>
37 {
38 public:
39  static MetricsProcess* create(const Option<std::string>& authenticationRealm);
40 
42 
43  Future<Nothing> remove(const std::string& name);
44 
46  const Option<Duration>& timeout);
47 
48 protected:
49  virtual void initialize();
50 
51 private:
52  static std::string help();
53 
55  const Option<Owned<RateLimiter>>& _limiter,
56  const Option<std::string>& _authenticationRealm)
57  : ProcessBase("metrics"),
58  limiter(_limiter),
59  authenticationRealm(_authenticationRealm)
60  {}
61 
62  // Non-copyable, non-assignable.
64  MetricsProcess& operator=(const MetricsProcess&);
65 
66  Future<http::Response> _snapshot(
67  const http::Request& request,
69 
70  // TODO(bmahler): Make this static once we can move
71  // capture with C++14.
73  const Option<Duration>& timeout,
74  hashmap<std::string, Future<double>>&& metrics,
75  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.
89 extern PID<MetricsProcess> metrics;
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="")
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)
Definition: http.hpp:517
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)
A "process identifier" used to uniquely identify a process when dispatching messages.
Definition: pid.hpp:279
Definition: attributes.hpp:24
Definition: executor.hpp:48
Definition: owned.hpp:36
Future< Nothing > add(Owned< Metric > metric)
Definition: process.hpp:501
PID< MetricsProcess > metrics
constexpr const char * name
Definition: shell.hpp:43
Definition: statistics.hpp:30