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 {
49 public:
50 ServerMonitor(const components::ComponentConfig& config,
51 const components::ComponentContext& component_context);
52
53 /// @ingroup userver_component_names
54 /// @brief The default name of server::handlers::ServerMonitor
55 static constexpr std::string_view kName = "handler-server-monitor";
56
57 std::string HandleRequestThrow(const http::HttpRequest& request,
58 request::RequestContext&) const override;
59
60 static yaml_config::Schema GetStaticConfigSchema();
61
62 private:
63 std::string GetResponseDataForLogging(
64 const http::HttpRequest& request, request::RequestContext& context,
65 const std::string& response_data) 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
78 components::kHasValidate<server::handlers::ServerMonitor> = true;
79
80USERVER_NAMESPACE_END