artdaq_utilities  v1_05_08
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 
70  void sendMetric_(const std::string& name, const std::string& value, const std::string& unit) override
71  {
72  if (!inhibit_)
73  {
74  metrics_[name] = value + " " + unit;
75  writeReportMessage_(false);
76  }
77  }
78 
85  void sendMetric_(const std::string& name, const int& value, const std::string& unit) override
86  {
87  sendMetric_(name, std::to_string(value), unit);
88  }
89 
96  void sendMetric_(const std::string& name, const double& value, const std::string& unit) override
97  {
98  sendMetric_(name, std::to_string(value), unit);
99  }
100 
107  void sendMetric_(const std::string& name, const float& value, const std::string& unit) override
108  {
109  sendMetric_(name, std::to_string(value), unit);
110  }
111 
118  void sendMetric_(const std::string& name, const uint64_t& value, const std::string& unit) override
119  {
120  sendMetric_(name, std::to_string(value), unit);
121  }
122 
126  void startMetrics_() override
127  {
128  }
129 
133  void stopMetrics_() override
134  {
135  writeReportMessage_(true);
136  metrics_.clear();
137  }
138 
139 private:
142  PeriodicReportMetric& operator=(const PeriodicReportMetric&) = delete;
143  PeriodicReportMetric& operator=(PeriodicReportMetric&&) = delete;
144 
145  void writeReportMessage_(bool force)
146  {
147  std::unique_lock<std::mutex> lk(report_mutex_);
148  if (force || std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1>>>(std::chrono::steady_clock::now() - last_report_time_).count() >= accumulationTime_)
149  {
150  if (metrics_.empty())
151  {
152  return;
153  }
154  last_report_time_ = std::chrono::steady_clock::now();
155  std::ostringstream str;
156 
157  int count = 0;
158  int live_metrics = 0;
159  for (auto& metric : metrics_)
160  {
161  if (count != 0)
162  {
163  str << "," << std::endl;
164  }
165  str << "\t" << metric.first << ": " << metric.second;
166  if (metric.second != "NOT REPORTED")
167  {
168  live_metrics++;
169  }
170  metric.second = "NOT REPORTED";
171  count++;
172  }
173  if (live_metrics > 0)
174  {
175  TLOG_INFO(app_name_) << "Periodic report: " << live_metrics << " active metrics:" << std::endl
176  << str.str();
177  }
178  else
179  {
180  TLOG_INFO(app_name_) << "Periodic report: No active metrics in last reporting interval!";
181  }
182  }
183  }
184 };
185 } //End namespace artdaq
186 
187 DEFINE_ARTDAQ_METRIC(artdaq::PeriodicReportMetric)
void sendMetric_(const std::string &name, const double &value, const std::string &unit) override
Write metric data to a file.
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.
PeriodicReportMetric(fhicl::ParameterSet const &config, std::string const &app_name)
PeriodicReportMetric Constructor.
void sendMetric_(const std::string &name, const uint64_t &value, const std::string &unit) 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.
std::string getLibName() const override
Get the library name for the PeriodicReport metric.
void sendMetric_(const std::string &name, const int &value, const std::string &unit) override
Write metric data to a file.
void sendMetric_(const std::string &name, const float &value, const std::string &unit) override
Write metric data to a file.
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...
void sendMetric_(const std::string &name, const std::string &value, const std::string &unit) override
Write metric data to a file.
~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...