2016-01-17 13:34:36 +00:00
|
|
|
#include "MetricsTransmitter.h"
|
|
|
|
|
2022-11-09 12:37:42 +00:00
|
|
|
#include <Common/AsynchronousMetrics.h>
|
2017-08-24 14:51:13 +00:00
|
|
|
|
2017-04-01 09:19:00 +00:00
|
|
|
#include <Common/CurrentMetrics.h>
|
|
|
|
#include <Common/Exception.h>
|
|
|
|
#include <Common/setThreadName.h>
|
2017-08-24 14:51:13 +00:00
|
|
|
|
2022-04-27 15:05:45 +00:00
|
|
|
#include <Daemon/BaseDaemon.h>
|
2017-08-24 14:51:13 +00:00
|
|
|
|
|
|
|
#include <Poco/Util/Application.h>
|
|
|
|
#include <Poco/Util/LayeredConfiguration.h>
|
|
|
|
|
2016-01-17 13:34:36 +00:00
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
2017-08-24 14:51:13 +00:00
|
|
|
|
2019-07-04 19:08:37 +00:00
|
|
|
MetricsTransmitter::MetricsTransmitter(
|
2019-08-03 11:02:40 +00:00
|
|
|
const Poco::Util::AbstractConfiguration & config, const std::string & config_name_, const AsynchronousMetrics & async_metrics_)
|
|
|
|
: async_metrics(async_metrics_), config_name(config_name_)
|
2019-07-04 19:08:37 +00:00
|
|
|
{
|
|
|
|
interval_seconds = config.getInt(config_name + ".interval", 60);
|
|
|
|
send_events = config.getBool(config_name + ".events", true);
|
2019-09-22 22:57:06 +00:00
|
|
|
send_events_cumulative = config.getBool(config_name + ".events_cumulative", false);
|
2019-07-04 19:08:37 +00:00
|
|
|
send_metrics = config.getBool(config_name + ".metrics", true);
|
|
|
|
send_asynchronous_metrics = config.getBool(config_name + ".asynchronous_metrics", true);
|
2019-09-30 08:10:12 +00:00
|
|
|
|
|
|
|
thread = ThreadFromGlobalPool{&MetricsTransmitter::run, this};
|
2019-07-04 19:08:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-17 13:34:36 +00:00
|
|
|
MetricsTransmitter::~MetricsTransmitter()
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
2019-01-02 06:44:36 +00:00
|
|
|
std::lock_guard lock{mutex};
|
2017-04-01 07:20:54 +00:00
|
|
|
quit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cond.notify_one();
|
|
|
|
|
2019-09-30 08:10:12 +00:00
|
|
|
thread->join();
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
|
|
|
DB::tryLogCurrentException(__PRETTY_FUNCTION__);
|
|
|
|
}
|
2016-01-17 13:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MetricsTransmitter::run()
|
|
|
|
{
|
2019-07-04 19:08:37 +00:00
|
|
|
const std::string thread_name = "MetrTx" + std::to_string(interval_seconds);
|
2017-04-01 07:20:54 +00:00
|
|
|
setThreadName(thread_name.c_str());
|
2017-03-21 19:08:09 +00:00
|
|
|
|
2018-01-10 00:04:08 +00:00
|
|
|
const auto get_next_time = [](size_t seconds)
|
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
/// To avoid time drift and transmit values exactly each interval:
|
|
|
|
/// next time aligned to system seconds
|
|
|
|
/// (60s -> every minute at 00 seconds, 5s -> every minute:[00, 05, 15 ... 55]s, 3600 -> every hour:00:00
|
|
|
|
return std::chrono::system_clock::time_point(
|
|
|
|
(std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()) / seconds) * seconds
|
|
|
|
+ std::chrono::seconds(seconds));
|
|
|
|
};
|
2016-01-17 13:34:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
std::vector<ProfileEvents::Count> prev_counters(ProfileEvents::end());
|
2016-10-24 04:06:27 +00:00
|
|
|
|
2019-01-02 06:44:36 +00:00
|
|
|
std::unique_lock lock{mutex};
|
2016-01-17 13:34:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
while (true)
|
|
|
|
{
|
2019-07-04 19:08:37 +00:00
|
|
|
if (cond.wait_until(lock, get_next_time(interval_seconds), [this] { return quit; }))
|
2017-04-01 07:20:54 +00:00
|
|
|
break;
|
2016-01-17 13:34:36 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
transmit(prev_counters);
|
|
|
|
}
|
2016-01-17 13:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-11-01 17:56:25 +00:00
|
|
|
void MetricsTransmitter::transmit(std::vector<ProfileEvents::Count> & prev_counters)
|
2016-01-17 13:34:36 +00:00
|
|
|
{
|
2017-04-01 07:20:54 +00:00
|
|
|
auto async_metrics_values = async_metrics.getValues();
|
|
|
|
|
|
|
|
GraphiteWriter::KeyValueVector<ssize_t> key_vals{};
|
|
|
|
key_vals.reserve(ProfileEvents::end() + CurrentMetrics::end() + async_metrics_values.size());
|
|
|
|
|
2019-07-04 19:08:37 +00:00
|
|
|
if (send_events)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
|
|
|
|
{
|
2018-02-01 17:55:08 +00:00
|
|
|
const auto counter = ProfileEvents::global_counters[i].load(std::memory_order_relaxed);
|
2017-04-01 07:20:54 +00:00
|
|
|
const auto counter_increment = counter - prev_counters[i];
|
|
|
|
prev_counters[i] = counter;
|
|
|
|
|
2018-09-01 19:51:38 +00:00
|
|
|
std::string key{ProfileEvents::getName(static_cast<ProfileEvents::Event>(i))};
|
2017-04-01 07:20:54 +00:00
|
|
|
key_vals.emplace_back(profile_events_path_prefix + key, counter_increment);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-22 22:57:06 +00:00
|
|
|
if (send_events_cumulative)
|
2019-09-17 23:16:49 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
|
|
|
|
{
|
|
|
|
const auto counter = ProfileEvents::global_counters[i].load(std::memory_order_relaxed);
|
|
|
|
std::string key{ProfileEvents::getName(static_cast<ProfileEvents::Event>(i))};
|
2019-09-22 22:57:06 +00:00
|
|
|
key_vals.emplace_back(profile_events_cumulative_path_prefix + key, counter);
|
2019-09-17 23:16:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 19:08:37 +00:00
|
|
|
if (send_metrics)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
for (size_t i = 0, end = CurrentMetrics::end(); i < end; ++i)
|
|
|
|
{
|
|
|
|
const auto value = CurrentMetrics::values[i].load(std::memory_order_relaxed);
|
|
|
|
|
2018-09-01 19:51:38 +00:00
|
|
|
std::string key{CurrentMetrics::getName(static_cast<CurrentMetrics::Metric>(i))};
|
2017-04-01 07:20:54 +00:00
|
|
|
key_vals.emplace_back(current_metrics_path_prefix + key, value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-04 19:08:37 +00:00
|
|
|
if (send_asynchronous_metrics)
|
2017-04-01 07:20:54 +00:00
|
|
|
{
|
|
|
|
for (const auto & name_value : async_metrics_values)
|
|
|
|
{
|
2022-11-13 02:36:20 +00:00
|
|
|
key_vals.emplace_back(asynchronous_metrics_path_prefix + name_value.first, name_value.second.value);
|
2017-04-01 07:20:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-09 03:38:43 +00:00
|
|
|
if (!key_vals.empty())
|
2017-04-01 07:20:54 +00:00
|
|
|
BaseDaemon::instance().writeToGraphite(key_vals, config_name);
|
2016-01-17 13:34:36 +00:00
|
|
|
}
|
2019-07-04 19:08:37 +00:00
|
|
|
|
2016-01-17 13:34:36 +00:00
|
|
|
}
|