]>
Commit | Line | Data |
---|---|---|
2a845540 TL |
1 | #pragma once |
2 | ||
3 | #include "common/admin_socket_client.h" | |
4 | #include <map> | |
5 | #include <string> | |
6 | #include <vector> | |
7 | ||
8 | #include <boost/asio.hpp> | |
9 | #include <boost/json/object.hpp> | |
10 | #include <filesystem> | |
11 | #include <map> | |
12 | #include <string> | |
13 | #include <vector> | |
14 | ||
15 | struct pstat { | |
16 | unsigned long utime; | |
17 | unsigned long stime; | |
18 | unsigned long minflt; | |
19 | unsigned long majflt; | |
20 | unsigned long start_time; | |
21 | int num_threads; | |
22 | unsigned long vm_size; | |
23 | int resident_size; | |
24 | }; | |
25 | ||
26 | class MetricsBuilder; | |
27 | class OrderedMetricsBuilder; | |
28 | class UnorderedMetricsBuilder; | |
29 | class Metric; | |
30 | ||
31 | typedef std::map<std::string, std::string> labels_t; | |
32 | ||
33 | class DaemonMetricCollector { | |
34 | public: | |
35 | void main(); | |
36 | std::string get_metrics(); | |
aee94f69 | 37 | labels_t get_extra_labels(std::string daemon_name); |
2a845540 TL |
38 | |
39 | private: | |
40 | std::map<std::string, AdminSocketClient> clients; | |
41 | std::string metrics; | |
42 | std::mutex metrics_mutex; | |
43 | std::unique_ptr<MetricsBuilder> builder; | |
44 | void update_sockets(); | |
45 | void request_loop(boost::asio::steady_timer &timer); | |
46 | ||
47 | void dump_asok_metrics(); | |
48 | void dump_asok_metric(boost::json::object perf_info, | |
49 | boost::json::value perf_values, std::string name, | |
50 | labels_t labels); | |
1e59de90 | 51 | std::pair<labels_t, std::string> add_fixed_name_metrics(std::string metric_name); |
2a845540 TL |
52 | void get_process_metrics(std::vector<std::pair<std::string, int>> daemon_pids); |
53 | std::string asok_request(AdminSocketClient &asok, std::string command, std::string daemon_name); | |
54 | }; | |
55 | ||
56 | class Metric { | |
57 | private: | |
58 | struct metric_entry { | |
59 | labels_t labels; | |
60 | std::string value; | |
61 | }; | |
62 | std::string name; | |
63 | std::string mtype; | |
64 | std::string description; | |
65 | std::vector<metric_entry> entries; | |
66 | ||
67 | public: | |
68 | Metric(std::string name, std::string mtype, std::string description) | |
69 | : name(name), mtype(mtype), description(description) {} | |
70 | Metric(const Metric &) = default; | |
71 | Metric() = default; | |
72 | void add(labels_t labels, std::string value); | |
73 | std::string dump(); | |
74 | }; | |
75 | ||
76 | class MetricsBuilder { | |
77 | public: | |
78 | virtual ~MetricsBuilder() = default; | |
79 | virtual std::string dump() = 0; | |
80 | virtual void add(std::string value, std::string name, std::string description, | |
81 | std::string mtype, labels_t labels) = 0; | |
82 | ||
83 | protected: | |
84 | std::string out; | |
85 | }; | |
86 | ||
87 | class OrderedMetricsBuilder : public MetricsBuilder { | |
88 | private: | |
89 | std::map<std::string, Metric> metrics; | |
90 | ||
91 | public: | |
92 | std::string dump(); | |
93 | void add(std::string value, std::string name, std::string description, | |
94 | std::string mtype, labels_t labels); | |
95 | }; | |
96 | ||
97 | class UnorderedMetricsBuilder : public MetricsBuilder { | |
98 | public: | |
99 | std::string dump(); | |
100 | void add(std::string value, std::string name, std::string description, | |
101 | std::string mtype, labels_t labels); | |
102 | }; | |
103 | ||
104 | DaemonMetricCollector &collector_instance(); |