2019-11-24 15:43:53 +00:00
|
|
|
#include "PrometheusMetricsWriter.h"
|
|
|
|
|
|
|
|
#include <IO/WriteHelpers.h>
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2019-11-24 20:03:09 +00:00
|
|
|
template <typename T>
|
2019-11-24 15:43:53 +00:00
|
|
|
void writeOutLine(DB::WriteBuffer & wb, T && val)
|
|
|
|
{
|
|
|
|
DB::writeText(std::forward<T>(val), wb);
|
|
|
|
DB::writeChar('\n', wb);
|
|
|
|
}
|
|
|
|
|
2019-11-24 20:03:09 +00:00
|
|
|
template <typename T, typename... TArgs>
|
2019-11-24 15:43:53 +00:00
|
|
|
void writeOutLine(DB::WriteBuffer & wb, T && val, TArgs &&... args)
|
|
|
|
{
|
|
|
|
DB::writeText(std::forward<T>(val), wb);
|
|
|
|
DB::writeChar(' ', wb);
|
|
|
|
writeOutLine(wb, std::forward<TArgs>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
PrometheusMetricsWriter::PrometheusMetricsWriter(
|
2019-11-26 20:27:24 +00:00
|
|
|
const Poco::Util::AbstractConfiguration & config, const std::string & config_name,
|
|
|
|
const AsynchronousMetrics & async_metrics_)
|
|
|
|
: async_metrics(async_metrics_)
|
|
|
|
, send_events(config.getBool(config_name + ".events", true))
|
2019-11-24 15:43:53 +00:00
|
|
|
, send_metrics(config.getBool(config_name + ".metrics", true))
|
2019-11-26 20:27:24 +00:00
|
|
|
, send_asynchronous_metrics(config.getBool(config_name + ".asynchronous_metrics", true))
|
2019-11-24 15:43:53 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-11-24 20:03:09 +00:00
|
|
|
void PrometheusMetricsWriter::write(WriteBuffer & wb) const
|
2019-11-24 15:43:53 +00:00
|
|
|
{
|
|
|
|
if (send_events)
|
|
|
|
{
|
|
|
|
for (size_t i = 0, end = ProfileEvents::end(); i < end; ++i)
|
|
|
|
{
|
|
|
|
const auto counter = ProfileEvents::global_counters[i].load(std::memory_order_relaxed);
|
|
|
|
|
2019-12-04 07:54:09 +00:00
|
|
|
std::string metric_name{ProfileEvents::getName(static_cast<ProfileEvents::Event>(i))};
|
2019-11-24 15:43:53 +00:00
|
|
|
std::string metric_doc{ProfileEvents::getDocumentation(static_cast<ProfileEvents::Event>(i))};
|
|
|
|
|
|
|
|
std::string key{profile_events_prefix + metric_name};
|
|
|
|
|
|
|
|
writeOutLine(wb, "# HELP", key, metric_doc);
|
|
|
|
writeOutLine(wb, "# TYPE", key, "counter");
|
|
|
|
writeOutLine(wb, key, counter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (send_metrics)
|
|
|
|
{
|
|
|
|
for (size_t i = 0, end = CurrentMetrics::end(); i < end; ++i)
|
|
|
|
{
|
|
|
|
const auto value = CurrentMetrics::values[i].load(std::memory_order_relaxed);
|
|
|
|
|
2019-12-04 07:54:09 +00:00
|
|
|
std::string metric_name{CurrentMetrics::getName(static_cast<CurrentMetrics::Metric>(i))};
|
2019-11-24 15:43:53 +00:00
|
|
|
std::string metric_doc{CurrentMetrics::getDocumentation(static_cast<CurrentMetrics::Metric>(i))};
|
|
|
|
|
|
|
|
std::string key{current_metrics_prefix + metric_name};
|
|
|
|
|
|
|
|
writeOutLine(wb, "# HELP", key, metric_doc);
|
|
|
|
writeOutLine(wb, "# TYPE", key, "gauge");
|
|
|
|
writeOutLine(wb, key, value);
|
|
|
|
}
|
|
|
|
}
|
2019-11-26 20:27:24 +00:00
|
|
|
|
|
|
|
if (send_asynchronous_metrics)
|
|
|
|
{
|
|
|
|
auto async_metrics_values = async_metrics.getValues();
|
|
|
|
for (const auto & name_value : async_metrics_values)
|
|
|
|
{
|
|
|
|
std::string key{asynchronous_metrics_prefix + name_value.first};
|
|
|
|
auto value = name_value.second;
|
|
|
|
|
|
|
|
// TODO: add HELP section? asynchronous_metrics contains only key and value
|
|
|
|
writeOutLine(wb, "# TYPE", key, "gauge");
|
|
|
|
writeOutLine(wb, key, value);
|
|
|
|
}
|
|
|
|
}
|
2019-11-24 15:43:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|