8 #ifndef __METRIC_INTERFACE__
9 #define __METRIC_INTERFACE__
13 #include <unordered_map>
14 #include "fhiclcpp/ParameterSet.h"
15 #include "artdaq-utilities/Plugins/MetricData.hh"
60 virtual std::string
getLibName()
const {
return "ERROR"; }
70 virtual void sendMetric_(
const std::string& name,
const std::string& value,
const std::string& unit) = 0;
80 virtual void sendMetric_(
const std::string& name,
const int& value,
const std::string& unit) = 0;
90 virtual void sendMetric_(
const std::string& name,
const double& value,
const std::string& unit) = 0;
100 virtual void sendMetric_(
const std::string& name,
const float& value,
const std::string& unit) = 0;
110 virtual void sendMetric_(
const std::string& name,
const long unsigned int& value,
const std::string& unit) = 0;
144 if (!metricRegistry_.count(data.
Name))
146 metricRegistry_[data.
Name] = data;
148 metricData_[data.
Name].push_back(data);
159 for (
auto metric : metricData_)
161 auto metricName = metric.first;
162 if (readyToSend_(metricName) || forceSend)
164 if (metricData_[metricName].size() == 0 && metricRegistry_.count(metricName))
166 sendZero_(metricRegistry_[metricName]);
168 else if (metricData_[metricName].size() > 0)
170 auto metricMode = metricData_[metricName].back().Mode;
171 auto metricUnits = metricData_[metricName].back().Unit;
172 auto metricType = metricData_[metricName].back().Type;
176 if (metricData_[metricName].size() > 1)
178 metricData_[metricName].erase(metricData_[metricName].begin(), std::prev(metricData_[metricName].end()));
189 for (
auto& mv : metricData_[metricName]) { ds += mv.DoubleValue; }
197 for (
auto& mv : metricData_[metricName]) { fs += mv.FloatValue; }
205 for (
auto& mv : metricData_[metricName]) { is += mv.IntValue; }
213 for (
auto& mv : metricData_[metricName]) { us += mv.UnsignedValue; }
221 metricData_[metricName].clear();
241 for (
auto metric : metricRegistry_)
243 sendZero_(metric.second);
267 std::unordered_map<std::string, std::list<MetricData>> metricData_;
268 std::unordered_map<std::string, MetricData> metricRegistry_;
269 std::unordered_map<std::string, std::chrono::steady_clock::time_point> lastSendTime_;
271 bool readyToSend_(std::string name)
273 auto now = std::chrono::steady_clock::now();
274 if (std::chrono::duration_cast<std::chrono::duration<
double, std::ratio<1>>>(now - lastSendTime_[name]).count() >=
accumulationTime_)
276 lastSendTime_[name] = now;
309 sendMetric_(data.Name, data.DoubleValue, data.Unit);
312 sendMetric_(data.Name, data.FloatValue, data.Unit);
318 sendMetric_(data.Name, data.UnsignedValue, data.Unit);
327 #endif //End ifndef __METRIC_INTERFACE__
virtual void startMetrics_()=0
Perform any start-up actions necessary for the metric plugin.
std::string StringValue
Value of the metric, if it is a MetricType::StringMetric
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.
MetricPlugin(fhicl::ParameterSet const &ps)
MetricPlugin Constructor.
Report the average of all values. Use for rates to report accurate results.
fhicl::ParameterSet pset
The ParameterSet used to configure the MetricPlugin.
virtual void sendMetric_(const std::string &name, const std::string &value, const std::string &unit)=0
Send a metric to the underlying metric storage (file, Graphite, Ganglia, etc.)
std::string Unit
Units of the metric
Metric is a std::string (not in union)
std::string Name
Name of the metric
void addMetricData(MetricData data)
Send a metric value to the MetricPlugin.
void sendMetrics(bool forceSend=false)
For each known metric, determine whether the reporting interval has elapsed, and if so...
void stopMetrics()
Perform shutdown actions. Zeroes out all accumulators, and sends zeros for each metric. Calls stopMetrics_() for any plugin-defined shutdown actions.
Metric is a long unsigned int.
int getRunLevel() const
Get the threshold for sending metrics to the underlying storage.
virtual std::string getLibName() const
Return the name of the current MetricPlugin instance.
virtual void stopMetrics_()=0
Perform any shutdown actions necessary for the metric plugin.
MetricType Type
Type of the metric
double accumulationTime_
The amount of time to average metric values; except for accumulate=false metrics, will be the interva...
Small structure used to hold a metric data point before sending to the metric plugins ...
Report only the last value recorded. Useful for event counters, run numbers, etc. ...
virtual ~MetricPlugin()=default
Default virtual Desctructor.
void setRunLevel(int level)
Set the threshold for sending metrics to the underlying storage.
bool inhibit_
Whether to inhibit all metric sending.
int runLevel_
The threshold for sending metrics to the underlying storage. Metrics with level <= to runLevel_ will ...