artdaq_utilities  v1_05_00
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 {
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  , metrics_()
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 unsigned long int& 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:
140  void writeReportMessage_(bool force)
141  {
142  std::unique_lock<std::mutex> lk(report_mutex_);
143  if (force || std::chrono::duration_cast<std::chrono::duration<double, std::ratio<1>>>(std::chrono::steady_clock::now() - last_report_time_).count() >= accumulationTime_)
144  {
145  if (metrics_.size() == 0) return;
146  last_report_time_ = std::chrono::steady_clock::now();
147  std::ostringstream str;
148 
149  int count = 0;
150  int live_metrics = 0;
151  for (auto& metric : metrics_)
152  {
153  if (count != 0) str << "," << std::endl;
154  str << "\t" << metric.first << ": " << metric.second;
155  if (metric.second != "NOT REPORTED") live_metrics++;
156  metric.second = "NOT REPORTED";
157  count++;
158  }
159  if (live_metrics > 0)
160  TLOG_INFO(app_name_) << "Periodic report: " << live_metrics << " active metrics:" << std::endl
161  << str.str();
162  else
163  TLOG_INFO(app_name_) << "Periodic report: No active metrics in last reporting interval!";
164  }
165  }
166 };
167 } //End namespace artdaq
168 
169 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:29
void startMetrics()
Perform startup actions. Simply calls the virtual startMetrics_ function.
virtual ~PeriodicReportMetric()
PeriodicReportMetric Destructor. Calls stopMetrics and then closes the file.
PeriodicReportMetric(fhicl::ParameterSet const &config, std::string const &app_name)
PeriodicReportMetric Constructor.
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 unsigned long int &value, const std::string &unit) override
Write metric data to a file.
void sendMetric_(const std::string &name, const std::string &value, const std::string &unit) override
Write metric data to a file.
bool inhibit_
Flag to indicate that the MetricPlugin is being stopped, and any metric back-ends which do not have a...