Apache Mesos
profiler.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_PROFILER_HPP__
14 #define __PROCESS_PROFILER_HPP__
15 
16 #include <string>
17 
18 #include <process/future.hpp>
19 #include <process/http.hpp>
20 #include <process/process.hpp>
21 
22 namespace process {
23 
24 class Profiler : public Process<Profiler>
25 {
26 public:
27  Profiler(const Option<std::string>& _authenticationRealm)
28  : ProcessBase("profiler"),
29  authenticationRealm(_authenticationRealm) {}
30 
31  virtual ~Profiler() {}
32 
33 protected:
34  virtual void initialize()
35  {
36  if (authenticationRealm.isSome()) {
37  route("/start",
38  authenticationRealm.get(),
39  START_HELP(),
40  &Profiler::start);
41 
42  route("/stop",
43  authenticationRealm.get(),
44  STOP_HELP(),
45  &Profiler::stop);
46  } else {
47  route("/start",
48  START_HELP(),
49  [this](const http::Request& request) {
50  return Profiler::start(request, None());
51  });
52 
53  route("/stop",
54  STOP_HELP(),
55  [this](const http::Request& request) {
56  return Profiler::stop(request, None());
57  });
58  }
59  }
60 
61 private:
62  static const std::string START_HELP();
63  static const std::string STOP_HELP();
64 
65  // HTTP endpoints.
66 
67  // Starts the profiler. There are no request parameters.
69  const http::Request& request,
71 
72  // Stops the profiler. There are no request parameters.
73  // This returns the profile output, it will also remain present
74  // in the working directory.
76  const http::Request& request,
78 
79  // The authentication realm that the profiler's HTTP endpoints will be
80  // installed into.
81  Option<std::string> authenticationRealm;
82 };
83 
84 } // namespace process {
85 
86 #endif // __PROCESS_PROCESS_HPP__
virtual void initialize()
Invoked when a process gets spawned.
Definition: profiler.hpp:34
Profiler(const Option< std::string > &_authenticationRealm)
Definition: profiler.hpp:27
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...
Definition: profiler.hpp:24
Definition: process.hpp:72
void route(const std::string &name, const Option< std::string > &help, const HttpRequestHandler &handler, const RouteOptions &options=RouteOptions())
Sets up a handler for HTTP requests with the specified name.
bool isSome() const
Definition: option.hpp:115
Definition: http.hpp:518
const T & get() const &
Definition: option.hpp:118
Result< Process > process(pid_t pid)
Definition: freebsd.hpp:30
Definition: none.hpp:27
virtual ~Profiler()
Definition: profiler.hpp:31
Definition: process.hpp:493