userver: userver/server/handlers/server_monitor.hpp Source File
Loading...
Searching...
No Matches
server_monitor.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/server/handlers/server_monitor.hpp
4/// @brief @copybrief server::handlers::ServerMonitor
5
6#include <userver/server/handlers/http_handler_base.hpp>
7#include <userver/utils/statistics/fwd.hpp>
8
9USERVER_NAMESPACE_BEGIN
10
11namespace server::handlers {
12
13// clang-format off
14
15/// @ingroup userver_components userver_http_handlers
16///
17/// @brief Handler that returns statistics data.
18///
19/// Additionally to the
20/// @ref userver_http_handlers "common handler options" the component has
21/// 'common-labels' option that should be a map of label name to label value.
22/// Items of the map are added to each metric.
23///
24/// ## Static configuration example:
25///
26/// @snippet components/common_server_component_list_test.cpp Sample handler server monitor component config
27///
28/// ## Scheme
29///
30/// Accepts a path arguments 'format', 'labels', 'path' and 'prefix':
31/// * format - "prometheus", "prometheus-untyped", "graphite",
32/// "json", "solomon", "pretty" and internal (default) format is
33/// supported. For more info see the
34/// documentation for utils::statistics::ToPrometheusFormat,
35/// utils::statistics::ToPrometheusFormatUntyped,
36/// utils::statistics::ToGraphiteFormat, utils::statistics::ToJsonFormat,
37/// utils::statistics::ToSolomonFormat, utils::statistics::ToPrettyFormat.
38/// * labels - filter out metrics without the provided labels. Parameter should
39/// be a JSON dictionary in the form '{"label1":"value1", "label2":"value2"}'.
40/// * path - return metrics on for the following path
41/// * prefix - return metrics whose path starts from the specified prefix.
42
43// clang-format on
44class ServerMonitor final : public HttpHandlerBase {
45 public:
46 ServerMonitor(const components::ComponentConfig& config,
47 const components::ComponentContext& component_context);
48
49 /// @ingroup userver_component_names
50 /// @brief The default name of server::handlers::ServerMonitor
51 static constexpr std::string_view kName = "handler-server-monitor";
52
53 std::string HandleRequestThrow(const http::HttpRequest& request,
54 request::RequestContext&) const override;
55
56 static yaml_config::Schema GetStaticConfigSchema();
57
58 private:
59 std::string GetResponseDataForLogging(
60 const http::HttpRequest& request, request::RequestContext& context,
61 const std::string& response_data) const override;
62
63 utils::statistics::Storage& statistics_storage_;
64
65 using CommonLabels = std::unordered_map<std::string, std::string>;
66 const CommonLabels common_labels_;
67};
68
69} // namespace server::handlers
70
71template <>
72inline constexpr bool
73 components::kHasValidate<server::handlers::ServerMonitor> = true;
74
75USERVER_NAMESPACE_END