8 #ifndef __METRIC_INTERFACE__
9 #define __METRIC_INTERFACE__
13 #include <unordered_map>
14 #include "fhiclcpp/ParameterSet.h"
59 virtual std::string
getLibName()
const {
return "ERROR"; }
69 virtual void sendMetric_(
const std::string& name,
const std::string& value,
const std::string& unit) = 0;
79 virtual void sendMetric_(
const std::string& name,
const int& value,
const std::string& unit) = 0;
89 virtual void sendMetric_(
const std::string& name,
const double& value,
const std::string& unit) = 0;
99 virtual void sendMetric_(
const std::string& name,
const float& value,
const std::string& unit) = 0;
109 virtual void sendMetric_(
const std::string& name,
const long unsigned int& value,
const std::string& unit) = 0;
138 void sendMetric(
const std::string& name,
const std::string& value,
const std::string& unit,
bool accumulate =
true)
158 void sendMetric(
const std::string& name,
const int& value,
const std::string& unit,
bool accumulate =
true)
163 intAccumulator_[name].push_back(value);
168 intAccumulator_[name].clear();
169 lastSendTime_[name] = std::chrono::steady_clock::now();
173 if (readyToSend_(name))
175 double sendValue = 0;
176 for (
auto val : intAccumulator_[name])
178 sendValue += val /
static_cast<double>(intAccumulator_[name].size());
183 intAccumulator_[name].clear();
194 void sendMetric(
const std::string& name,
const double& value,
const std::string& unit,
bool accumulate =
true)
199 doubleAccumulator_[name].push_back(value);
204 doubleAccumulator_[name].clear();
205 lastSendTime_[name] = std::chrono::steady_clock::now();
209 if (readyToSend_(name))
211 double sendValue = 0;
212 for (
auto val : doubleAccumulator_[name])
214 sendValue += val / doubleAccumulator_[name].size();
219 doubleAccumulator_[name].clear();
230 void sendMetric(
const std::string& name,
const float& value,
const std::string& unit,
bool accumulate =
true)
235 floatAccumulator_[name].push_back(value);
240 floatAccumulator_[name].clear();
241 lastSendTime_[name] = std::chrono::steady_clock::now();
245 if (readyToSend_(name))
248 for (
auto val : floatAccumulator_[name])
250 sendValue += val / floatAccumulator_[name].size();
255 floatAccumulator_[name].clear();
266 void sendMetric(
const std::string& name,
const long unsigned int& value,
const std::string& unit,
bool accumulate =
true)
271 auto uvalue =
static_cast<uint32_t
>(value);
272 uintAccumulator_[name].push_back(uvalue);
277 uintAccumulator_[name].clear();
278 lastSendTime_[name] = std::chrono::steady_clock::now();
282 if (readyToSend_(name))
284 double sendValue = 0;
285 for (
auto val : uintAccumulator_[name])
287 sendValue += val /
static_cast<double>(uintAccumulator_[name].size());
292 uintAccumulator_[name].clear();
308 for (
auto dv : doubleAccumulator_)
310 static_cast<std::vector<double>
>(dv.second).clear();
312 sendMetric(dv.first, static_cast<double>(0.0),
"",
false);
314 for (
auto iv : intAccumulator_)
316 static_cast<std::vector<int>
>(iv.second).clear();
318 sendMetric(iv.first, static_cast<int>(0),
"",
false);
320 for (
auto fv : floatAccumulator_)
322 static_cast<std::vector<float>
>(fv.second).clear();
324 sendMetric(fv.first, static_cast<float>(0.0),
"",
false);
326 for (
auto uv : uintAccumulator_)
328 static_cast<std::vector<uint32_t>
>(uv.second).clear();
330 sendMetric(uv.first, static_cast<long unsigned int>(0),
"",
false);
354 std::unordered_map<std::string, std::vector<double>> doubleAccumulator_;
355 std::unordered_map<std::string, std::vector<int>> intAccumulator_;
356 std::unordered_map<std::string, std::vector<float>> floatAccumulator_;
357 std::unordered_map<std::string, std::vector<uint32_t>> uintAccumulator_;
358 std::unordered_map<std::string, std::chrono::steady_clock::time_point> lastSendTime_;
360 bool readyToSend_(std::string name)
362 auto now = std::chrono::steady_clock::now();
363 if (std::chrono::duration_cast<std::chrono::duration<
double, std::ratio<1>>>(now - lastSendTime_[name]).count() >=
accumulationTime_)
365 lastSendTime_[name] = now;
374 #endif //End ifndef __METRIC_INTERFACE__
virtual void startMetrics_()=0
Perform any start-up actions necessary for the metric plugin.
void sendMetric(const std::string &name, const long unsigned int &value, const std::string &unit, bool accumulate=true)
Send a metric value to the MetricPlugin.
void sendMetric(const std::string &name, const double &value, const std::string &unit, bool accumulate=true)
Send a metric value to the MetricPlugin.
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.
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.)
void sendMetric(const std::string &name, const float &value, const std::string &unit, bool accumulate=true)
Send a metric value to the MetricPlugin.
void stopMetrics()
Perform shutdown actions. Zeroes out all accumulators, and sends zeros for each metric. Calls stopMetrics_() for any plugin-defined shutdown actions.
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.
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 int &value, const std::string &unit, bool accumulate=true)
Send a metric value to the MetricPlugin.
void sendMetric(const std::string &name, const std::string &value, const std::string &unit, bool accumulate=true)
Send a metric value to the MetricPlugin.
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 ...