@@ -22,57 +22,67 @@ namespace o2
2222namespace monitoring
2323{
2424
25- Metric DerivedMetrics::rate (Metric& metric)
26- {
27- // disallow string
28- auto tags = metric.getTags ();
29- std::string key = metric.getName () + std::string (tags.begin (), tags.end ());
30- if (metric.getType () == MetricType::STRING) {
31- throw MonitoringException (" DerivedMetrics/ProcessMetric" , " Not able to process string values" );
32- }
33-
34- // search for previous value
35- auto search = mStorage .find (key);
36- if (search == mStorage .end ()) {
37- mStorage .insert (std::make_pair (key, metric));
38- return Metric{(double ) 0.0 , metric.getName () + " Rate" }.addTags (std::move (tags));
39- }
25+ Metric DerivedMetrics::process (Metric& metric, DerivedMetricMode mode) {
26+ const std::map<DerivedMetricMode, std::function<Metric (Metric&)>> map = {
27+ {
28+ DerivedMetricMode::INCREMENT, [this ](Metric& metric) {
29+ auto tags = metric.getTags ();
30+ std::string key = metric.getName () + std::string (tags.begin (), tags.end ());
31+ auto search = mStorage .find (key);
32+ if (search != mStorage .end ()) {
33+ auto currentValue = metric.getValue ();
34+ auto storedValue = search->second .getValue ();
35+ auto value = boost::apply_visitor (VariantVisitorAdd (), currentValue, storedValue);
36+ mStorage .erase (search);
37+ Metric result = Metric{value, metric.getName ()}.addTags (std::move (tags));
38+ mStorage .insert (std::make_pair (key, result));
39+ return result;
40+ }
41+ mStorage .insert (std::make_pair (key, metric));
42+ return metric;
43+ }
44+ }, {
45+ DerivedMetricMode::RATE, [this ](Metric& metric) {
46+ // disallow string
47+ auto tags = metric.getTags ();
48+ std::string key = metric.getName () + std::string (tags.begin (), tags.end ());
49+ if (metric.getType () == MetricType::STRING) {
50+ throw MonitoringException (" DerivedMetrics" , " Not able to process string values" );
51+ }
4052
41- auto timestampDifference = std::chrono::duration_cast<std::chrono::milliseconds>(
42- metric.getTimestamp ()
43- - search->second .getTimestamp ()
44- );
45- int timestampCount = timestampDifference.count ();
46- // disallow dividing by 0
47- if (timestampCount == 0 ) {
48- throw MonitoringException (" DerivedMetrics/Calculate rate" , " Division by 0" );
49- }
53+ // search for previous value
54+ auto search = mStorage .find (key);
55+ if (search == mStorage .end ()) {
56+ mStorage .insert (std::make_pair (key, metric));
57+ return Metric{(double ) 0.0 , metric.getName () + " Rate" }.addTags (std::move (tags));
58+ }
5059
51- auto current = metric.getValue ();
52- auto previous = search->second .getValue ();
53- auto rate = boost::apply_visitor (VariantVisitorRate (timestampCount), current, previous);
60+ auto timestampDifference = std::chrono::duration_cast<std::chrono::milliseconds>(
61+ metric.getTimestamp ()
62+ - search->second .getTimestamp ()
63+ );
64+ int timestampCount = timestampDifference.count ();
65+ // disallow dividing by 0
66+ if (timestampCount == 0 ) {
67+ throw MonitoringException (" DerivedMetrics" , " Division by 0" );
68+ }
5469
55- // swap metrics
56- mStorage .erase (key);
57- mStorage .insert (std::make_pair (key, metric));
58- return Metric{rate, metric.getName () + " Rate" }.addTags (std::move (tags));
59- }
70+ auto current = metric.getValue ();
71+ auto previous = search->second .getValue ();
72+ auto rate = boost::apply_visitor (VariantVisitorRate (timestampCount), current, previous);
6073
61- Metric DerivedMetrics::increment (Metric& metric) {
62- auto tags = metric.getTags ();
63- std::string key = metric.getName () + std::string (tags.begin (), tags.end ());
64- auto search = mStorage .find (key);
65- if (search != mStorage .end ()) {
66- auto currentValue = metric.getValue ();
67- auto storedValue = search->second .getValue ();
68- auto value = boost::apply_visitor (VariantVisitorAdd (), currentValue, storedValue);
69- mStorage .erase (search);
70- Metric result = Metric{value, metric.getName ()}.addTags (std::move (tags));
71- mStorage .insert (std::make_pair (key, result));
72- return result;
74+ // swap metrics
75+ mStorage .erase (key);
76+ mStorage .insert (std::make_pair (key, metric));
77+ return Metric{rate, metric.getName () + " Rate" }.addTags (std::move (tags));
78+ }
79+ }
80+ };
81+ auto iterator = map.find (mode);
82+ if (iterator == map.end ()) {
83+ throw MonitoringException (" DerivedMetrics" , " Unknown mode" );
7384 }
74- mStorage .insert (std::make_pair (key, metric));
75- return metric;
85+ return iterator->second (metric);
7686}
7787
7888} // namespace monitoring
0 commit comments