artdaq_utilities  1.08.06
report_metric.cc
1 // report_metric.cc: Periodic Report Metric Plugin
2 // Author: Eric Flumerfelt
3 // Last Modified: 11/06/2014
4 //
5 // An implementation of the MetricPlugin for creating "Periodic Report" messages
6 
7 #include "TRACE/tracemf.h" // order matters -- trace.h (no "mf") is nested from MetricMacros.hh
8 #define TRACE_NAME (app_name_ + "_report_metric").c_str()
9 
10 #include "artdaq-utilities/Plugins/MetricMacros.hh"
11 
12 #include <sys/types.h>
13 #include <unistd.h>
14 #include <ctime>
15 #include <fstream>
16 #include <mutex>
17 #include <sstream>
18 #include <string>
19 #include "TRACE/tracemf.h"
20 
21 namespace artdaq {
25 class PeriodicReportMetric final : public MetricPlugin
26 {
27 private:
28  std::chrono::steady_clock::time_point last_report_time_;
29 
30  std::map<std::string, std::string> metrics_;
31 
32  std::mutex report_mutex_;
33 
34 public:
45  explicit PeriodicReportMetric(fhicl::ParameterSet const& config, std::string const& app_name, std::string const& metric_name)
46  : MetricPlugin(config, app_name, metric_name)
47  , last_report_time_(std::chrono::steady_clock::now())
48 
49  {
50  startMetrics();
51  }
52 
57  {
58  stopMetrics();
59  }
60 
65  std::string getLibName() const override { return "report"; }
66 
75  void sendMetric_(const std::string& name, const std::string& value, const std::string& unit, const std::chrono::system_clock::time_point&) override
76  {
77  if (!inhibit_)
78  {
79  metrics_[name] = value + " " + unit;
80  writeReportMessage_(false);
81  }
82  }
83 
91  void sendMetric_(const std::string& name, const int& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
92  {
93  sendMetric_(name, std::to_string(value), unit, time);
94  }
95 
103  void sendMetric_(const std::string& name, const double& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
104  {
105  sendMetric_(name, std::to_string(value), unit, time);
106  }
107 
115  void sendMetric_(const std::string& name, const float& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
116  {
117  sendMetric_(name, std::to_string(value), unit, time);
118  }
119 
127  void sendMetric_(const std::string& name, const uint64_t& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
128  {
129  sendMetric_(name, std::to_string(value), unit, time);
130  }
131 
135  void startMetrics_() override
136  {
137  }
138 
142  void stopMetrics_() override
143  {
144  writeReportMessage_(true);
145  metrics_.clear();
146  }
147 
148 private:
151  PeriodicReportMetric& operator=(const PeriodicReportMetric&) = delete;
152  PeriodicReportMetric& operator=(PeriodicReportMetric&&) = delete;
153 
154  void writeReportMessage_(bool force)
155  {
156  std::unique_lock<std::mutex> lk(report_mutex_);
157  if (force || std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1>>>(std::chrono::steady_clock::now() - last_report_time_).count() >= accumulationTime_)
158  {
159  if (metrics_.empty())
160  {
161  return;
162  }
163  last_report_time_ = std::chrono::steady_clock::now();
164  std::ostringstream str;
165 
166  int count = 0;
167  int live_metrics = 0;
168  for (auto& metric : metrics_)
169  {
170  if (count != 0)
171  {
172  str << "," << std::endl;
173  }
174  str << "\t" << metric.first << ": " << metric.second;
175  if (metric.second != "NOT REPORTED")
176  {
177  live_metrics++;
178  }
179  metric.second = "NOT REPORTED";
180  count++;
181  }
182  if (live_metrics > 0)
183  {
184  METLOG(TLVL_INFO) << "Periodic report: " << live_metrics << " active metrics:" << std::endl
185  << str.str();
186  }
187  else
188  {
189  TLOG_INFO(app_name_) << "Periodic report: No active metrics in last reporting interval!";
190  METLOG(TLVL_INFO) << "Periodic report: No active metrics in last reporting interval!";
191  }
192  }
193  }
194 };
195 } // End namespace artdaq
196 
197 DEFINE_ARTDAQ_METRIC(artdaq::PeriodicReportMetric)
The MetricPlugin class defines the interface that MetricManager uses to send metric data to the vario...
Definition: MetricPlugin.hh:41
PeriodicReportMetric(fhicl::ParameterSet const &config, std::string const &app_name, std::string const &metric_name)
PeriodicReportMetric Constructor.
void startMetrics()
Perform startup actions. Simply calls the virtual startMetrics_ function.
void sendMetric_(const std::string &name, const int &value, const std::string &unit, const std::chrono::system_clock::time_point &time) override
Write metric data to a file.
void sendMetric_(const std::string &name, const std::string &value, const std::string &unit, const std::chrono::system_clock::time_point &) override
Write metric data to a file.
void sendMetric_(const std::string &name, const uint64_t &value, const std::string &unit, const std::chrono::system_clock::time_point &time) override
Write metric data to a file.
void sendMetric_(const std::string &name, const double &value, const std::string &unit, const std::chrono::system_clock::time_point &time) override
Write metric data to a file.
void stopMetrics()
Perform shutdown actions. Zeroes out all accumulators, and sends zeros for each metric. Calls stopMetrics_() for any plugin-defined shutdown actions.
std::string app_name_
Name of the application which is sending metrics to this plugin.
void sendMetric_(const std::string &name, const float &value, const std::string &unit, const std::chrono::system_clock::time_point &time) override
Write metric data to a file.
std::string getLibName() const override
Get the library name for the PeriodicReport metric.
PeriodicReportMetric writes metric data to a file on disk.
void startMetrics_() override
Perform startup actions.
void stopMetrics_() override
Perform shutdown actions.
double accumulationTime_
The amount of time to average metric values; except for accumulate=false metrics, will be the interva...
~PeriodicReportMetric() override
PeriodicReportMetric Destructor. Calls stopMetrics and then closes the file.
bool inhibit_
Flag to indicate that the MetricPlugin is being stopped, and any metric back-ends which do not have a...