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
13namespace impl {
14enum class StatsFormat;
15}
16
17// clang-format off
18
19/// @ingroup userver_components userver_http_handlers
20///
21/// @brief Handler that returns statistics data.
22///
23/// Additionally to the
24/// @ref userver_http_handlers "common handler options" the component has
25/// 'common-labels' option that should be a map of label name to label value.
26/// Items of the map are added to each metric.
27///
28/// Default format can be set via 'format' option. Supported formats are: "prometheus", "prometheus-untyped", "graphite",
29/// "json", "solomon", "pretty" and "internal". For more info see the documentation for utils::statistics::ToPrometheusFormat,
30/// utils::statistics::ToPrometheusFormatUntyped, utils::statistics::ToGraphiteFormat, utils::statistics::ToJsonFormat,
31/// utils::statistics::ToSolomonFormat, utils::statistics::ToPrettyFormat.
32///
33/// ## Static configuration example:
34///
35/// @snippet components/common_server_component_list_test.cpp Sample handler server monitor component config
36///
37/// ## Scheme
38///
39/// Accepts a path arguments 'format', 'labels', 'path' and 'prefix':
40/// * format - overrides default format option if it's present. Make sure that you provide
41/// at least one of two formats.
42/// * labels - filter out metrics without the provided labels. Parameter should
43/// be a JSON dictionary in the form '{"label1":"value1", "label2":"value2"}'.
44/// * path - return metrics on for the following path
45/// * prefix - return metrics whose path starts from the specified prefix.
46
47// clang-format on
48class ServerMonitor final : public HttpHandlerBase {
49public:
50 ServerMonitor(const components::ComponentConfig& config, const components::ComponentContext& component_context);
51
52 /// @ingroup userver_component_names
53 /// @brief The default name of server::handlers::ServerMonitor
54 static constexpr std::string_view kName = "handler-server-monitor";
55
56 std::string HandleRequestThrow(const http::HttpRequest& request, request::RequestContext&) const override;
57
58 static yaml_config::Schema GetStaticConfigSchema();
59
60private:
61 std::string GetResponseDataForLogging(
62 const http::HttpRequest& request,
63 request::RequestContext& context,
64 const std::string& response_data
65 ) const override;
66
67 utils::statistics::Storage& statistics_storage_;
68
69 using CommonLabels = std::unordered_map<std::string, std::string>;
70 const CommonLabels common_labels_;
71 const std::optional<impl::StatsFormat> default_format_;
72};
73
74} // namespace server::handlers
75
76template <>
77inline constexpr bool components::kHasValidate<server::handlers::ServerMonitor> = true;
78
79USERVER_NAMESPACE_END