1 | #pragma once |
2 | |
3 | #include <Interpreters/SystemLog.h> |
4 | #include <Common/ProfileEvents.h> |
5 | #include <Common/CurrentMetrics.h> |
6 | |
7 | #include <vector> |
8 | #include <atomic> |
9 | #include <ctime> |
10 | |
11 | |
12 | namespace DB |
13 | { |
14 | |
15 | /** MetricLog is a log of metric values measured at regular time interval. |
16 | */ |
17 | |
18 | struct MetricLogElement |
19 | { |
20 | time_t event_time{}; |
21 | UInt64 milliseconds{}; |
22 | |
23 | std::vector<ProfileEvents::Count> profile_events; |
24 | std::vector<CurrentMetrics::Metric> current_metrics; |
25 | |
26 | static std::string name() { return "MetricLog" ; } |
27 | static Block createBlock(); |
28 | void appendToBlock(Block & block) const; |
29 | }; |
30 | |
31 | |
32 | class MetricLog : public SystemLog<MetricLogElement> |
33 | { |
34 | using SystemLog<MetricLogElement>::SystemLog; |
35 | |
36 | public: |
37 | /// Launches a background thread to collect metrics with interval |
38 | void startCollectMetric(size_t collect_interval_milliseconds_); |
39 | |
40 | /// Stop background thread. Call before shutdown. |
41 | void stopCollectMetric(); |
42 | |
43 | private: |
44 | void metricThreadFunction(); |
45 | |
46 | ThreadFromGlobalPool metric_flush_thread; |
47 | size_t collect_interval_milliseconds; |
48 | std::atomic<bool> is_shutdown_metric_thread{false}; |
49 | }; |
50 | |
51 | } |
52 | |