artdaq_utilities  v1_06_02
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 "artdaq-utilities/Plugins/MetricMacros.hh"
8 #include "fhiclcpp/ParameterSet.h"
9 
10 #include <sys/types.h>
11 #include <unistd.h>
12 #include <ctime>
13 #include <fstream>
14 #include <mutex>
15 #include <sstream>
16 #include <string>
17 #include "tracemf.h"
18 
19 namespace artdaq {
23 class PeriodicReportMetric final : public MetricPlugin
24 {
25 private:
26  std::chrono::steady_clock::time_point last_report_time_;
27 
28  std::map<std::string, std::string> metrics_;
29 
30  std::mutex report_mutex_;
31 
32 public:
42  explicit PeriodicReportMetric(fhicl::ParameterSet const& config, std::string const& app_name)
43  : MetricPlugin(config, app_name)
44  , last_report_time_(std::chrono::steady_clock::now())
45 
46  {
47  startMetrics();
48  }
49 
54  {
55  stopMetrics();
56  }
57 
62  std::string getLibName() const override { return "report"; }
63 
72  void sendMetric_(const std::string& name, const std::string& value, const std::string& unit, const std::chrono::system_clock::time_point&) override
73  {
74  if (!inhibit_)
75  {
76  metrics_[name] = value + " " + unit;
77  writeReportMessage_(false);
78  }
79  }
80 
88  void sendMetric_(const std::string& name, const int& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
89  {
90  sendMetric_(name, std::to_string(value), unit, time);
91  }
92 
100  void sendMetric_(const std::string& name, const double& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
101  {
102  sendMetric_(name, std::to_string(value), unit, time);
103  }
104 
112  void sendMetric_(const std::string& name, const float& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
113  {
114  sendMetric_(name, std::to_string(value), unit, time);
115  }
116 
124  void sendMetric_(const std::string& name, const uint64_t& value, const std::string& unit, const std::chrono::system_clock::time_point& time) override
125  {
126  sendMetric_(name, std::to_string(value), unit, time);
127  }
128 
132  void startMetrics_() override
133  {
134  }
135 
139  void stopMetrics_() override
140  {
141  writeReportMessage_(true);
142  metrics_.clear();
143  }
144 
145 private:
148  PeriodicReportMetric& operator=(const PeriodicReportMetric&) = delete;
149  PeriodicReportMetric& operator=(PeriodicReportMetric&&) = delete;
150 
151  void writeReportMessage_(bool force)
152  {
153  std::unique_lock<std::mutex> lk(report_mutex_);
154  if (force || std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1>>>(std::chrono::steady_clock::now() - last_report_time_).count() >= accumulationTime_)
155  {
156  if (metrics_.empty())
157  {
158  return;
159  }
160  last_report_time_ = std::chrono::steady_clock::now();
161  std::ostringstream str;
162 
163  int count = 0;
164  int live_metrics = 0;
165  for (auto& metric : metrics_)
166  {
167  if (count != 0)
168  {
169  str << "," << std::endl;
170  }
171  str << "\t" << metric.first << ": " << metric.second;
172  if (metric.second != "NOT REPORTED")
173  {
174  live_metrics++;
175  }
176  metric.second = "NOT REPORTED";
177  count++;
178  }
179  if (live_metrics > 0)
180  {
181  TLOG_INFO(app_name_) << "Periodic report: " << live_metrics << " active metrics:" << std::endl
182  << str.str();
183  }
184  else
185  {
186  TLOG_INFO(app_name_) << "Periodic report: No active metrics in last reporting interval!";
187  }
188  }
189  }
190 };
191 } //End namespace artdaq
192 
193 DEFINE_ARTDAQ_METRIC(artdaq::PeriodicReportMetric)
The MetricPlugin class defines the interface that MetricManager uses to send metric data to the vario...
Definition: MetricPlugin.hh:37
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.
PeriodicReportMetric(fhicl::ParameterSet const &config, std::string const &app_name)
PeriodicReportMetric Constructor.
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...