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/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  void initialize() override;
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  // TODO(bmahler): Make this static once we can move
70  // capture with C++14.
72  const Option<Duration>& timeout,
73  std::vector<std::string>&& keys,
74  std::vector<Future<double>>&& metrics,
75  std::vector<Option<Statistics<double>>>&& statistics);
76 
77  // The Owned<Metric> is an explicit copy of the Metric passed to 'add'.
78  std::map<std::string, Owned<Metric>> metrics;
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="")
void initialize() override
Invoked when a process gets spawned.
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:520
void dispatch(const PID< T > &pid, void(T::*method)())
Definition: dispatch.hpp:174
Future< std::map< std::string, double > > snapshot(const Option< Duration > &timeout)
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:32