7 #include "artdaq-utilities/Plugins/MetricMacros.hh"
8 #include "fhiclcpp/ParameterSet.h"
9 #include "messagefacility/MessageLogger/MessageLogger.h"
12 #include <boost/asio.hpp>
18 using boost::asio::ip::tcp;
36 std::string namespace_;
37 boost::asio::io_service io_service_;
41 std::chrono::steady_clock::time_point waitStart_;
56 explicit GraphiteMetric(fhicl::ParameterSet
const& config, std::string
const& app_name)
58 , host_(
pset.get<std::string>(
"host",
"localhost"))
59 , port_(
pset.get<int>(
"port", 2003))
60 , namespace_(
pset.get<std::string>(
"namespace",
"artdaq."))
62 , socket_(io_service_)
78 std::string
getLibName()
const override {
return "graphite"; }
85 void sendMetric_(
const std::string& name,
const std::string& value,
const std::string&)
override
89 const auto result = std::time(0);
90 boost::asio::streambuf data;
92 std::replace(nameTemp.begin(), nameTemp.end(),
' ',
'_');
93 std::ostream out(&data);
94 out << namespace_ << nameTemp <<
" "
96 << result << std::endl;
98 boost::system::error_code error;
99 boost::asio::write(socket_, data, error);
114 void sendMetric_(
const std::string& name,
const int& value,
const std::string& unit)
override
125 void sendMetric_(
const std::string& name,
const double& value,
const std::string& unit)
override
136 void sendMetric_(
const std::string& name,
const float& value,
const std::string& unit)
override
147 void sendMetric_(
const std::string& name,
const unsigned long int& value,
const std::string& unit)
override
171 socket_.shutdown(boost::asio::socket_base::shutdown_send);
185 boost::system::error_code error;
186 tcp::resolver resolver(io_service_);
187 tcp::resolver::query query(host_, std::to_string(port_));
188 boost::asio::connect(socket_, resolver.resolve(query), error);
189 if (!error) { errorCount_ = 0; }
192 mf::LogWarning(
"GraphiteMetric") <<
"Error reconnecting socket, attempt #" << errorCount_;
194 waitStart_ = std::chrono::steady_clock::now();
196 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.
The MetricPlugin class defines the interface that MetricManager uses to send metric data to the vario...
void startMetrics()
Perform startup actions. Simply calls the virtual startMetrics_ function.
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.
void stopMetrics()
Perform shutdown actions. Zeroes out all accumulators, and sends zeros for each metric. Calls stopMetrics_() for any plugin-defined shutdown actions.
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.