2023-10-21 01:07:49 +00:00
|
|
|
#include <base/getFQDNOrHostName.h>
|
2019-08-13 14:31:46 +00:00
|
|
|
#include <Interpreters/MetricLog.h>
|
2023-04-06 17:00:51 +00:00
|
|
|
#include <Common/ThreadPool.h>
|
2023-10-21 01:07:49 +00:00
|
|
|
#include <DataTypes/DataTypeLowCardinality.h>
|
2019-08-13 14:31:46 +00:00
|
|
|
#include <DataTypes/DataTypesNumber.h>
|
|
|
|
#include <DataTypes/DataTypeDate.h>
|
|
|
|
#include <DataTypes/DataTypeDateTime.h>
|
2020-09-05 23:19:38 +00:00
|
|
|
#include <DataTypes/DataTypeDateTime64.h>
|
2023-10-21 01:07:49 +00:00
|
|
|
#include <DataTypes/DataTypeString.h>
|
2019-08-13 14:31:46 +00:00
|
|
|
|
2019-08-18 00:04:58 +00:00
|
|
|
|
2019-08-13 14:31:46 +00:00
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
2024-01-12 15:39:22 +00:00
|
|
|
ColumnsDescription MetricLogElement::getColumnsDescription()
|
2019-08-13 14:31:46 +00:00
|
|
|
{
|
2024-01-12 15:39:22 +00:00
|
|
|
ColumnsDescription result;
|
2019-08-13 14:31:46 +00:00
|
|
|
|
2024-01-12 15:39:22 +00:00
|
|
|
result.add({"hostname", std::make_shared<DataTypeLowCardinality>(std::make_shared<DataTypeString>()), "Hostname of the server executing the query."});
|
|
|
|
result.add({"event_date", std::make_shared<DataTypeDate>(), "Event date."});
|
|
|
|
result.add({"event_time", std::make_shared<DataTypeDateTime>(), "Event time."});
|
|
|
|
result.add({"event_time_microseconds", std::make_shared<DataTypeDateTime64>(6), "Event time with microseconds resolution."});
|
2019-08-13 14:31:46 +00:00
|
|
|
|
|
|
|
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
|
|
|
|
{
|
2024-01-12 15:39:22 +00:00
|
|
|
auto name = fmt::format("ProfileEvent_{}", ProfileEvents::getName(ProfileEvents::Event(i)));
|
|
|
|
const auto * comment = ProfileEvents::getDocumentation(ProfileEvents::Event(i));
|
|
|
|
result.add({std::move(name), std::make_shared<DataTypeUInt64>(), comment});
|
2019-08-13 14:31:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t i = 0, end = CurrentMetrics::end(); i < end; ++i)
|
|
|
|
{
|
2024-01-12 15:39:22 +00:00
|
|
|
auto name = fmt::format("CurrentMetric_{}", CurrentMetrics::getName(CurrentMetrics::Metric(i)));
|
|
|
|
const auto * comment = CurrentMetrics::getDocumentation(CurrentMetrics::Metric(i));
|
|
|
|
result.add({std::move(name), std::make_shared<DataTypeInt64>(), comment});
|
2019-08-13 14:31:46 +00:00
|
|
|
}
|
|
|
|
|
2024-01-12 15:39:22 +00:00
|
|
|
return result;
|
2019-08-13 14:31:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 00:04:58 +00:00
|
|
|
|
2020-05-21 20:15:18 +00:00
|
|
|
void MetricLogElement::appendToBlock(MutableColumns & columns) const
|
2019-08-13 14:31:46 +00:00
|
|
|
{
|
2019-08-18 00:04:58 +00:00
|
|
|
size_t column_idx = 0;
|
2019-08-13 14:31:46 +00:00
|
|
|
|
2023-10-21 01:07:49 +00:00
|
|
|
columns[column_idx++]->insert(getFQDNOrHostName());
|
2020-04-17 13:26:44 +00:00
|
|
|
columns[column_idx++]->insert(DateLUT::instance().toDayNum(event_time).toUnderType());
|
2019-08-18 00:04:58 +00:00
|
|
|
columns[column_idx++]->insert(event_time);
|
2020-09-05 23:19:38 +00:00
|
|
|
columns[column_idx++]->insert(event_time_microseconds);
|
2019-08-13 14:31:46 +00:00
|
|
|
|
|
|
|
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
|
2019-08-18 00:04:58 +00:00
|
|
|
columns[column_idx++]->insert(profile_events[i]);
|
2019-08-13 14:31:46 +00:00
|
|
|
|
|
|
|
for (size_t i = 0, end = CurrentMetrics::end(); i < end; ++i)
|
2023-04-01 09:17:23 +00:00
|
|
|
columns[column_idx++]->insert(current_metrics[i].toUnderType());
|
2019-08-13 14:31:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 00:04:58 +00:00
|
|
|
|
2019-08-13 16:47:12 +00:00
|
|
|
void MetricLog::startCollectMetric(size_t collect_interval_milliseconds_)
|
|
|
|
{
|
|
|
|
collect_interval_milliseconds = collect_interval_milliseconds_;
|
|
|
|
is_shutdown_metric_thread = false;
|
2023-04-06 17:00:51 +00:00
|
|
|
metric_flush_thread = std::make_unique<ThreadFromGlobalPool>([this] { metricThreadFunction(); });
|
2019-08-13 16:47:12 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 00:04:58 +00:00
|
|
|
|
2019-08-13 16:47:12 +00:00
|
|
|
void MetricLog::stopCollectMetric()
|
|
|
|
{
|
|
|
|
bool old_val = false;
|
|
|
|
if (!is_shutdown_metric_thread.compare_exchange_strong(old_val, true))
|
|
|
|
return;
|
2023-04-06 17:00:51 +00:00
|
|
|
if (metric_flush_thread)
|
|
|
|
metric_flush_thread->join();
|
2019-08-13 16:47:12 +00:00
|
|
|
}
|
|
|
|
|
2019-08-18 00:04:58 +00:00
|
|
|
|
2020-04-13 01:33:05 +00:00
|
|
|
void MetricLog::shutdown()
|
|
|
|
{
|
|
|
|
stopCollectMetric();
|
|
|
|
stopFlushThread();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-13 16:47:12 +00:00
|
|
|
void MetricLog::metricThreadFunction()
|
|
|
|
{
|
2019-08-14 12:54:41 +00:00
|
|
|
auto desired_timepoint = std::chrono::system_clock::now();
|
2019-08-18 00:34:23 +00:00
|
|
|
|
|
|
|
/// For differentiation of ProfileEvents counters.
|
|
|
|
std::vector<ProfileEvents::Count> prev_profile_events(ProfileEvents::end());
|
2019-08-18 00:04:58 +00:00
|
|
|
|
2019-08-15 23:35:54 +00:00
|
|
|
while (!is_shutdown_metric_thread)
|
2019-08-13 16:47:12 +00:00
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2019-08-15 23:35:54 +00:00
|
|
|
const auto current_time = std::chrono::system_clock::now();
|
2019-08-18 00:04:58 +00:00
|
|
|
|
|
|
|
MetricLogElement elem;
|
2019-08-15 23:35:54 +00:00
|
|
|
elem.event_time = std::chrono::system_clock::to_time_t(current_time);
|
2022-10-03 18:52:14 +00:00
|
|
|
elem.event_time_microseconds = timeInMicroseconds(current_time);
|
2019-08-15 16:39:18 +00:00
|
|
|
|
2019-08-18 00:25:58 +00:00
|
|
|
elem.profile_events.resize(ProfileEvents::end());
|
2023-04-01 09:52:23 +00:00
|
|
|
for (ProfileEvents::Event i = ProfileEvents::Event(0), end = ProfileEvents::end(); i < end; ++i)
|
2019-08-18 00:04:58 +00:00
|
|
|
{
|
|
|
|
const ProfileEvents::Count new_value = ProfileEvents::global_counters[i].load(std::memory_order_relaxed);
|
2019-09-21 18:30:01 +00:00
|
|
|
auto & old_value = prev_profile_events[i];
|
2019-08-18 00:04:58 +00:00
|
|
|
elem.profile_events[i] = new_value - old_value;
|
|
|
|
old_value = new_value;
|
|
|
|
}
|
|
|
|
|
2019-08-18 00:25:58 +00:00
|
|
|
elem.current_metrics.resize(CurrentMetrics::end());
|
2019-08-18 00:04:58 +00:00
|
|
|
for (size_t i = 0, end = CurrentMetrics::end(); i < end; ++i)
|
|
|
|
{
|
|
|
|
elem.current_metrics[i] = CurrentMetrics::values[i];
|
|
|
|
}
|
|
|
|
|
2023-07-28 07:23:34 +00:00
|
|
|
this->add(std::move(elem));
|
2019-08-13 16:47:12 +00:00
|
|
|
|
2019-08-15 23:35:54 +00:00
|
|
|
/// We will record current time into table but align it to regular time intervals to avoid time drift.
|
|
|
|
/// We may drop some time points if the server is overloaded and recording took too much time.
|
|
|
|
while (desired_timepoint <= current_time)
|
2019-08-15 16:09:43 +00:00
|
|
|
desired_timepoint += std::chrono::milliseconds(collect_interval_milliseconds);
|
2019-08-15 23:35:54 +00:00
|
|
|
|
2019-08-14 12:54:41 +00:00
|
|
|
std::this_thread::sleep_until(desired_timepoint);
|
2019-08-13 16:47:12 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-13 14:31:46 +00:00
|
|
|
}
|