1
1
#include " TelemetryCollector.h"
2
2
#include " config_version.h"
3
3
4
- #include < Poco/Util/AbstractConfiguration.h>
4
+ #include < filesystem>
5
+ #include < Core/ServerUUID.h>
6
+ #include < IO/WriteBufferFromString.h>
7
+ #include < IO/WriteHelpers.h>
8
+ #include < Interpreters/Context.h>
9
+ #include < base/ClockUtils.h>
10
+ #include < base/getMemoryAmount.h>
5
11
#include < Poco/Net/HTTPRequest.h>
6
12
#include < Poco/Net/HTTPResponse.h>
7
13
#include < Poco/Net/HTTPSClientSession.h>
8
- #include < base/ClockUtils.h>
9
- #include < base/getMemoryAmount.h>
14
+ #include < Poco/Util/AbstractConfiguration.h>
10
15
#include < Common/DateLUT.h>
11
16
#include < Common/getNumberOfPhysicalCPUCores.h>
12
- #include < Core/ServerUUID.h>
13
- #include < IO/WriteBufferFromString.h>
14
- #include < IO/WriteHelpers.h>
15
- #include < Interpreters/Context.h>
16
- #include < filesystem>
17
17
18
18
namespace fs = std::filesystem;
19
19
20
20
namespace ProfileEvents
21
21
{
22
- extern const Event SelectQuery;
23
- extern const Event StreamingSelectQuery;
24
- extern const Event HistoricalSelectQuery;
22
+ extern const Event SelectQuery;
23
+ extern const Event StreamingSelectQuery;
24
+ extern const Event HistoricalSelectQuery;
25
25
}
26
26
27
27
namespace DB
28
28
{
29
+ namespace
30
+ {
31
+ constexpr auto DEFAULT_INTERVAL_MS = 5 * 60 * 1000 ;
32
+ }
29
33
30
34
TelemetryCollector::TelemetryCollector (ContextPtr context_)
31
- : log(&Poco::Logger::get (" TelemetryCollector" )),
32
- pool(context_->getSchedulePool ()),
33
- started_on_in_minutes(UTCMinutes::now())
35
+ : log(&Poco::Logger::get (" TelemetryCollector" )), pool(context_->getSchedulePool ()), started_on_in_minutes(UTCMinutes::now())
34
36
{
35
37
const auto & config = context_->getConfigRef ();
36
38
37
- if (!config.getBool (" telemetry_enabled" , true ))
38
- {
39
- LOG_WARNING (log, " Please note that telemetry is disabled." );
40
- is_shutdown.test_and_set ();
41
- return ;
42
- }
39
+ is_enable = config.getBool (" telemetry_enabled" , true );
40
+
41
+ collect_interval_ms = config.getUInt (" telemetry_interval_ms" , DEFAULT_INTERVAL_MS);
43
42
44
43
WriteBufferFromOwnString wb;
45
44
writeDateTimeTextISO (UTCMilliseconds::now (), 3 , wb, DateLUT::instance (" UTC" ));
46
45
started_on = wb.str ();
47
-
48
- LOG_WARNING (log, " Please note that telemetry is enabled. "
49
- " This is used to collect the version and runtime environment information to Timeplus, Inc. "
50
- " You can disable it by setting telemetry_enabled to false in config.yaml" );
51
-
52
- collector_task = pool.createTask (" TelemetryCollector" , [this ]() { this ->collect (); });
53
- collector_task->activate ();
54
- collector_task->schedule ();
55
46
}
56
47
57
48
TelemetryCollector::~TelemetryCollector ()
58
49
{
59
50
shutdown ();
51
+ LOG_INFO (log, " stopped" );
52
+ }
53
+
54
+ void TelemetryCollector::startup ()
55
+ {
56
+ collector_task = pool.createTask (" TelemetryCollector" , [this ]() { this ->collect (); });
57
+ collector_task->activate ();
58
+ collector_task->schedule ();
60
59
}
61
60
62
61
void TelemetryCollector::shutdown ()
63
62
{
64
- if (!is_shutdown.test_and_set ())
63
+ if (is_shutdown.test_and_set ())
64
+ return ;
65
+
66
+ if (collector_task)
65
67
{
66
68
LOG_INFO (log, " Stopped" );
67
69
collector_task->deactivate ();
68
70
}
69
71
}
70
72
73
+ void TelemetryCollector::enable ()
74
+ {
75
+ LOG_WARNING (
76
+ log,
77
+ " Please note that telemetry is enabled. "
78
+ " This is used to collect the version and runtime environment information to Timeplus, Inc. "
79
+ " You can disable it by setting telemetry_enabled to false in config.yaml" );
80
+ is_enable = true ;
81
+ }
82
+
83
+ void TelemetryCollector::disable ()
84
+ {
85
+ LOG_WARNING (log, " Please note that telemetry is disabled." );
86
+ is_enable = false ;
87
+ }
88
+
71
89
void TelemetryCollector::collect ()
72
90
{
73
- SCOPE_EXIT ({
74
- collector_task->scheduleAfter (INTERVAL_MS);
75
- });
91
+ SCOPE_EXIT ({ collector_task->scheduleAfter (getCollectIntervalMilliseconds ()); });
92
+
93
+ if (!isEnabled ())
94
+ return ;
76
95
77
96
constexpr auto jitsu_url = " https://data.timeplus.com/api/s/s2s/track" ;
78
97
constexpr auto jitsu_token = " U7qmIGzuZvvkp16iPaYLeBR4IHfKBY6P:Cc6EUDRmEHG9TCO7DX8x23xWrdFg8pBU" ;
@@ -94,16 +113,26 @@ void TelemetryCollector::collect()
94
113
// / https://stackoverflow.com/questions/20010199/how-to-determine-if-a-process-runs-inside-lxc-docker
95
114
bool in_docker = fs::exists (" /.dockerenv" );
96
115
97
- auto load_counter = [](const auto & event){
98
- assert (event < ProfileEvents::end ());
99
- return ProfileEvents::global_counters[event].load (std::memory_order_relaxed);
116
+ auto load_counter = [](const auto & event) {
117
+ assert (event < ProfileEvents::end ());
118
+ return static_cast <Int64>( ProfileEvents::global_counters[event].load (std::memory_order_relaxed) );
100
119
};
101
120
102
121
const auto total_select_query = load_counter (ProfileEvents::SelectQuery);
103
122
const auto streaming_select_query = load_counter (ProfileEvents::StreamingSelectQuery);
104
123
const auto historical_select_query = load_counter (ProfileEvents::HistoricalSelectQuery);
105
124
106
- std::string data = fmt::format (" {{"
125
+ const auto delta_total_select_query = total_select_query - prev_total_select_query;
126
+ prev_total_select_query = total_select_query;
127
+
128
+ const auto delta_streaming_select_query = streaming_select_query - prev_streaming_select_query;
129
+ prev_streaming_select_query = streaming_select_query;
130
+
131
+ const auto delta_historical_select_query = historical_select_query - prev_historical_select_query;
132
+ prev_historical_select_query = historical_select_query;
133
+
134
+ std::string data = fmt::format (
135
+ " {{"
107
136
" \" type\" : \" track\" ,"
108
137
" \" event\" : \" proton_ping\" ,"
109
138
" \" properties\" : {{"
@@ -118,14 +147,30 @@ void TelemetryCollector::collect()
118
147
" \" docker\" : \" {}\" ,"
119
148
" \" total_select_query\" : \" {}\" ,"
120
149
" \" historical_select_query\" : \" {}\" ,"
121
- " \" streaming_select_query\" : \" {}\" "
150
+ " \" streaming_select_query\" : \" {}\" ,"
151
+ " \" delta_total_select_query\" : \" {}\" ,"
152
+ " \" delta_historical_select_query\" : \" {}\" ,"
153
+ " \" delta_streaming_select_query\" : \" {}\" "
122
154
" }}"
123
- " }}" , cpu, memory_in_gb, EDITION, VERSION_STRING, new_session, started_on, duration_in_minute, server_uuid_str, in_docker, total_select_query, historical_select_query, streaming_select_query);
155
+ " }}" ,
156
+ cpu,
157
+ memory_in_gb,
158
+ EDITION,
159
+ VERSION_STRING,
160
+ new_session,
161
+ started_on,
162
+ duration_in_minute,
163
+ server_uuid_str,
164
+ in_docker,
165
+ total_select_query,
166
+ historical_select_query,
167
+ streaming_select_query,
168
+ delta_total_select_query,
169
+ delta_historical_select_query,
170
+ delta_streaming_select_query);
124
171
125
172
LOG_TRACE (log, " Sending telemetry: {}." , data);
126
173
127
- new_session = false ;
128
-
129
174
request.setContentLength (data.length ());
130
175
request.setContentType (" application/json" );
131
176
request.add (" X-Write-Key" , jitsu_token);
@@ -145,6 +190,7 @@ void TelemetryCollector::collect()
145
190
return ;
146
191
}
147
192
193
+ new_session = false ;
148
194
LOG_INFO (log, " Telemetry sent successfully." );
149
195
}
150
196
catch (Poco::Exception & ex)
0 commit comments