lokinet/llarp/util/metrics/metrictank_publisher.hpp

91 lines
2.1 KiB
C++
Raw Normal View History

2019-04-10 11:53:37 +00:00
#ifndef LLARP_METRICS_METRICTANK_PUBLISHER_HPP
#define LLARP_METRICS_METRICTANK_PUBLISHER_HPP
2019-09-01 12:58:27 +00:00
#include <util/metrics/core.hpp>
2019-04-10 11:53:37 +00:00
2019-09-01 13:26:16 +00:00
#include <util/thread/queue.hpp>
2019-04-10 11:53:37 +00:00
#include <absl/types/variant.h>
#include <string>
#include <thread>
#include <tuple>
2019-07-30 23:42:13 +00:00
#include <utility>
2019-04-10 11:53:37 +00:00
#include <vector>
namespace llarp
{
namespace metrics
{
class MetricTankPublisherInterface : public Publisher
{
public:
// Format for metrictank is <metric path, value, seconds since epoch>
// Metric path = metrics.namespaces.metric;key=value;key1=value2
using PublishData = std::tuple< std::string, std::string, absl::Time >;
using Tags = std::map< std::string, std::string >;
private:
const std::string m_suffix; // tags to send to metric tank
public:
MetricTankPublisherInterface(const Tags& tags)
: m_suffix(makeSuffix(tags))
{
}
2019-07-30 23:42:13 +00:00
~MetricTankPublisherInterface() override = default;
2019-04-10 11:53:37 +00:00
static std::string
makeSuffix(const Tags& tags);
void
publish(const Sample& values) override;
virtual void
publish(const std::vector< PublishData >& data) = 0;
};
class MetricTankPublisher final : public MetricTankPublisherInterface
{
private:
const std::string m_host;
const short m_port;
struct StopStruct
{
};
using Queue = thread::Queue<
absl::variant< std::vector< PublishData >, StopStruct > >;
Queue m_queue; // queue of things to publish
std::thread m_worker; // worker thread
void
work();
public:
2019-07-30 23:42:13 +00:00
MetricTankPublisher(const Tags& tags, std::string host, short port)
2019-04-10 11:53:37 +00:00
: MetricTankPublisherInterface(tags)
2019-07-30 23:42:13 +00:00
, m_host(std::move(host))
2019-04-10 11:53:37 +00:00
, m_port(port)
, m_queue(100)
, m_worker(&MetricTankPublisher::work, this)
{
}
2019-07-30 23:42:13 +00:00
~MetricTankPublisher() override
2019-04-10 11:53:37 +00:00
{
// Push back a signal value onto the queue
m_queue.pushBack(StopStruct());
}
void
publish(const std::vector< PublishData >& data) override;
};
} // namespace metrics
} // namespace llarp
#endif