7 #include "artdaq-utilities/Plugins/MetricMacros.hh"
8 #include "fhiclcpp/ParameterSet.h"
9 #include "messagefacility/MessageLogger/MessageLogger.h"
15 #include <boost/asio.hpp>
18 using boost::asio::ip::tcp;
37 std::string namespace_;
38 boost::asio::io_service io_service_;
42 std::chrono::steady_clock::time_point waitStart_;
57 , host_(
pset.get<std::string>(
"host",
"localhost"))
58 , port_(
pset.get<int>(
"port", 2003))
59 , namespace_(
pset.get<std::string>(
"namespace",
"artdaq."))
61 , socket_(io_service_)
77 std::string
getLibName()
const override {
return "graphite"; }
84 void sendMetric_(
const std::string& name,
const std::string& value,
const std::string&)
override
88 const auto result = std::time(0);
89 boost::asio::streambuf data;
91 std::replace(nameTemp.begin(), nameTemp.end(),
' ',
'_');
92 std::ostream out(&data);
93 out << namespace_ << nameTemp <<
" "
95 << result << std::endl;
97 boost::system::error_code error;
98 boost::asio::write(socket_, data, error);
113 void sendMetric_(
const std::string& name,
const int& value,
const std::string& unit)
override
124 void sendMetric_(
const std::string& name,
const double& value,
const std::string& unit)
override
135 void sendMetric_(
const std::string& name,
const float& value,
const std::string& unit)
override
146 void sendMetric_(
const std::string& name,
const unsigned long int& value,
const std::string& unit)
override
170 socket_.shutdown(boost::asio::socket_base::shutdown_send);
184 boost::system::error_code error;
185 tcp::resolver resolver(io_service_);
186 tcp::resolver::query query(host_, std::to_string(port_));
187 boost::asio::connect(socket_, resolver.resolve(query), error);
188 if (!error) { errorCount_ = 0; }
189 else { mf::LogWarning(
"GraphiteMetric") <<
"Error reconnecting socket, attempt #" << errorCount_; }
190 waitStart_ = std::chrono::steady_clock::now();
192 else if (std::chrono::duration_cast<std::chrono::seconds>(std::chrono::steady_clock::now() - waitStart_).count() >= 5)
void sendMetric_(const std::string &name, const double &value, const std::string &unit) override
Send a metric to Graphite.
fhicl::ParameterSet pset
The ParameterSet used to configure the MetricPlugin.
std::string getLibName() const override
Get the library name for the Graphite metric.
void sendMetric_(const std::string &name, const int &value, const std::string &unit) override
Send a metric to Graphite.
void sendMetric_(const std::string &name, const std::string &value, const std::string &) override
Send a metric to Graphite.
void startMetrics_() override
Perform startup actions. For Graphite, this means reconnecting the socket.
virtual ~GraphiteMetric()
GraphiteMetric Destructor. Calls stopMetrics()
void sendMetric_(const std::string &name, const float &value, const std::string &unit) override
Send a metric to Graphite.
GraphiteMetric(fhicl::ParameterSet const &config, std::string const &app_name)
GraphiteMetric Constructor.
void stopMetrics_() override
Perform shutdown actions. This shuts down the socket and closes it.
Send a metric to Graphite.
void sendMetric_(const std::string &name, const unsigned long int &value, const std::string &unit) override
Send a metric to Graphite.