2016-01-17 13:34:36 +00:00
|
|
|
#include "MetricsTransmitter.h"
|
|
|
|
|
2016-02-09 17:06:50 +00:00
|
|
|
#include <daemon/BaseDaemon.h>
|
2016-01-17 13:34:36 +00:00
|
|
|
#include <DB/Common/setThreadName.h>
|
2016-01-20 21:32:01 +00:00
|
|
|
#include <DB/Common/CurrentMetrics.h>
|
2016-10-24 04:06:27 +00:00
|
|
|
#include <DB/Interpreters/AsynchronousMetrics.h>
|
2016-01-17 13:34:36 +00:00
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
MetricsTransmitter::~MetricsTransmitter()
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock{mutex};
|
|
|
|
quit = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
cond.notify_one();
|
|
|
|
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
catch (...)
|
|
|
|
{
|
2016-10-23 10:52:32 +00:00
|
|
|
DB::tryLogCurrentException(__PRETTY_FUNCTION__);
|
2016-01-17 13:34:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void MetricsTransmitter::run()
|
|
|
|
{
|
2016-01-20 21:32:01 +00:00
|
|
|
setThreadName("MetricsTransmit");
|
2016-01-17 13:34:36 +00:00
|
|
|
|
2016-10-23 10:52:32 +00:00
|
|
|
/// Next minute at 00 seconds. To avoid time drift and transmit values exactly each minute.
|
2016-10-23 06:12:50 +00:00
|
|
|
const auto get_next_minute = []
|
|
|
|
{
|
2016-01-17 13:34:36 +00:00
|
|
|
return std::chrono::time_point_cast<std::chrono::minutes, std::chrono::system_clock>(
|
2016-10-23 06:12:50 +00:00
|
|
|
std::chrono::system_clock::now() + std::chrono::minutes(1));
|
2016-01-17 13:34:36 +00:00
|
|
|
};
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
ProfileEvents::Count prev_counters[ProfileEvents::end()] {};
|
|
|
|
|
2016-01-17 13:34:36 +00:00
|
|
|
std::unique_lock<std::mutex> lock{mutex};
|
|
|
|
|
|
|
|
while (true)
|
|
|
|
{
|
|
|
|
if (cond.wait_until(lock, get_next_minute(), [this] { return quit; }))
|
|
|
|
break;
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
transmit(prev_counters);
|
2016-01-17 13:34:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
void MetricsTransmitter::transmit(ProfileEvents::Count * prev_counters)
|
2016-01-17 13:34:36 +00:00
|
|
|
{
|
2016-10-24 04:06:27 +00:00
|
|
|
auto async_metrics_values = async_metrics.getValues();
|
|
|
|
|
2016-01-21 01:47:28 +00:00
|
|
|
GraphiteWriter::KeyValueVector<ssize_t> key_vals{};
|
2016-10-24 04:06:27 +00:00
|
|
|
key_vals.reserve(ProfileEvents::end() + CurrentMetrics::end() + async_metrics_values.size());
|
2016-01-17 13:34:36 +00:00
|
|
|
|
2016-10-24 02:02:37 +00:00
|
|
|
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
|
2016-01-17 13:34:36 +00:00
|
|
|
{
|
2016-07-31 03:53:16 +00:00
|
|
|
const auto counter = ProfileEvents::counters[i].load(std::memory_order_relaxed);
|
2016-10-24 04:06:27 +00:00
|
|
|
const auto counter_increment = counter - prev_counters[i];
|
|
|
|
prev_counters[i] = counter;
|
2016-01-17 13:34:36 +00:00
|
|
|
|
2016-01-20 21:32:01 +00:00
|
|
|
std::string key {ProfileEvents::getDescription(static_cast<ProfileEvents::Event>(i))};
|
2016-10-24 02:02:37 +00:00
|
|
|
key_vals.emplace_back(profile_events_path_prefix + key, counter_increment);
|
2016-01-17 13:34:36 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
for (size_t i = 0, end = CurrentMetrics::end(); i < end; ++i)
|
2016-01-20 21:32:01 +00:00
|
|
|
{
|
2016-07-31 03:53:16 +00:00
|
|
|
const auto value = CurrentMetrics::values[i].load(std::memory_order_relaxed);
|
2016-01-20 21:32:01 +00:00
|
|
|
|
|
|
|
std::string key {CurrentMetrics::getDescription(static_cast<CurrentMetrics::Metric>(i))};
|
2016-10-24 02:02:37 +00:00
|
|
|
key_vals.emplace_back(current_metrics_path_prefix + key, value);
|
2016-01-20 21:32:01 +00:00
|
|
|
}
|
|
|
|
|
2016-10-24 04:06:27 +00:00
|
|
|
for (const auto & name_value : async_metrics_values)
|
|
|
|
{
|
|
|
|
key_vals.emplace_back(asynchronous_metrics_path_prefix + name_value.first, name_value.second);
|
|
|
|
}
|
|
|
|
|
2016-02-09 17:06:50 +00:00
|
|
|
BaseDaemon::instance().writeToGraphite(key_vals);
|
2016-01-17 13:34:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|