userver: userver/utils/statistics/request.hpp Source File
Loading...
Searching...
No Matches
request.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/statistics/request.hpp
4/// @brief @copybrief utils::statistics::Request
5
6#include <string>
7#include <unordered_map>
8#include <vector>
9
10#include <userver/utils/statistics/labels.hpp>
11#include <userver/utils/statistics/metric_value.hpp>
12
13USERVER_NAMESPACE_BEGIN
14
15namespace utils::statistics {
16
17/// @ingroup userver_universal
18///
19/// @brief Class describing the request for metrics data.
20///
21/// Metric path and metric name are the same thing. For example for code like
22///
23/// @code
24/// writer["a"]["b"]["c"] = 42;
25/// @endcode
26///
27/// the metric name is "a.b.c" and in Prometheus format it would be escaped like
28/// "a_b_c{} 42".
29///
30/// @note `add_labels` should not match labels from Storage, otherwise the
31/// returned metrics may not be read by metrics server.
32class Request final {
33public:
34 using AddLabels = std::unordered_map<std::string, std::string>;
35
36 /// Default request without parameters. Equivalent to requesting all the
37 /// metrics without adding or requiring any labels.
38 Request() = default;
39
40 /// Makes request for metrics whose path starts with the `prefix`.
41 static Request MakeWithPrefix(
42 const std::string& prefix,
43 AddLabels add_labels = {},
44 std::vector<Label> require_labels = {}
45 );
46
47 /// Makes request for metrics whose path is `path`.
48 static Request MakeWithPath(
49 const std::string& path,
50 AddLabels add_labels = {},
51 std::vector<Label> require_labels = {}
52 );
53
54 /// Return metrics whose path matches with this `prefix`
55 const std::string prefix{};
56
57 /// Enum for different match types of the `prefix`
58 enum class PrefixMatch {
59 kNoop, ///< Do not match, the `prefix` is empty
60 kExact, ///< `prefix` equal to path
61 kStartsWith, ///< Metric path starts with `prefix`
62 };
63
64 /// Match type of the `prefix`
66
67 /// Require those labels in the metric
68 const std::vector<Label> require_labels{};
69
70 /// Add those labels to each returned metric
71 const AddLabels add_labels{};
72
73private:
74 Request(
75 std::string prefix_in,
76 PrefixMatch path_match_type_in,
77 std::vector<Label> require_labels_in,
78 AddLabels add_labels_in
79 );
80};
81
83public:
84 virtual ~BaseFormatBuilder();
85
86 virtual void HandleMetric(std::string_view path, LabelsSpan labels, const MetricValue& value) = 0;
87};
88
89} // namespace utils::statistics
90
91USERVER_NAMESPACE_END