Skip to content

Commit dad71d9

Browse files
authored
Move derived methods to lambdas (#108)
* Move derived methods to lambdas * Throw when mode unknown
1 parent 268c367 commit dad71d9

File tree

5 files changed

+75
-78
lines changed

5 files changed

+75
-78
lines changed

include/Monitoring/DerivedMetrics.h

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,12 @@ class DerivedMetrics
3535
/// Default destructor
3636
~DerivedMetrics() = default;
3737

38-
/// Calculates rate value based on metrics stored in mCache map
39-
/// \param metric metric object
40-
/// \return metric object holding calculated rate value
41-
Metric rate(Metric& metric);
42-
43-
/// Increments the previous metric value by value stored in the metric
44-
/// \param metric metric object
45-
/// \return metric object holding incremented value
46-
Metric increment(Metric& metric);
47-
4838
/// Metrics store necessary for derived metrics
4939
std::unordered_map <std::string, Metric> mStorage;
40+
41+
/// Entry method to DerivedMetrics
42+
/// Switches over processing modes: rate and increment
43+
Metric process(Metric& metric, DerivedMetricMode mode);
5044
};
5145

5246
} // namespace monitoring

include/Monitoring/Monitoring.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class Monitoring
107107
std::vector <std::unique_ptr<Backend>> mBackends;
108108

109109
/// Pushes metric to all backends or to the buffer
110-
void pushToBackends(Metric&& metric);
110+
void transmit(Metric&& metric);
111111

112112
/// States whether Process Monitor thread is running
113113
std::atomic<bool> mMonitorRunning;

src/DerivedMetrics.cxx

Lines changed: 56 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -22,57 +22,67 @@ namespace o2
2222
namespace 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

src/Monitoring.cxx

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ void Monitoring::debug(Metric&& metric)
154154
}
155155
}
156156

157-
void Monitoring::pushToBackends(Metric&& metric)
157+
void Monitoring::transmit(Metric&& metric)
158158
{
159159
if (mBuffering) {
160160
mStorage.push_back(std::move(metric));
@@ -170,18 +170,11 @@ void Monitoring::pushToBackends(Metric&& metric)
170170

171171
void Monitoring::send(Metric&& metric, DerivedMetricMode mode)
172172
{
173-
try {
174-
if (mode == DerivedMetricMode::RATE) {
175-
pushToBackends(mDerivedHandler->rate(metric));
176-
}
177-
178-
if (mode == DerivedMetricMode::INCREMENT) {
179-
pushToBackends(mDerivedHandler->increment(metric));
180-
}
181-
} catch(MonitoringException& e) {
182-
MonLogger::Get() << "[WARN] " << e.what() << MonLogger::End();
173+
if (mode != DerivedMetricMode::NONE) {
174+
try { transmit(mDerivedHandler->process(metric, mode)); }
175+
catch (MonitoringException& e) { MonLogger::Get() << e.what() << MonLogger::End(); }
183176
}
184-
pushToBackends(std::move(metric));
177+
transmit(std::move(metric));
185178
}
186179

187180
} // namespace monitoring

test/testDerived.cxx

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ BOOST_AUTO_TEST_CASE(derivedRateInt)
2929
try {
3030
std::this_thread::sleep_for(std::chrono::milliseconds(100));
3131
o2::monitoring::Metric metric(result.value, name);
32-
o2::monitoring::Metric derived = derivedHandler.rate(metric);
32+
o2::monitoring::Metric derived = derivedHandler.process(metric, DerivedMetricMode::RATE);
3333
BOOST_CHECK_EQUAL(derived.getName(), "metricIntRate");
3434
BOOST_WARN_CLOSE(boost::get<double>(derived.getValue()), result.rate, 1.0);
3535
} catch(MonitoringException &e) {
@@ -52,8 +52,8 @@ BOOST_AUTO_TEST_CASE(derivedRateDouble) {
5252
std::this_thread::sleep_for(std::chrono::milliseconds(100));
5353
o2::monitoring::Metric metric(results[i].value, name);
5454
o2::monitoring::Metric metricTagged = Metric{resultsTagged[i].value, name}.addTags({o2::monitoring::tags::Subsystem::Readout});
55-
o2::monitoring::Metric derived = derivedHandler.rate(metric);
56-
o2::monitoring::Metric derivedTagged = derivedHandler.rate(metricTagged);
55+
o2::monitoring::Metric derived = derivedHandler.process(metric, DerivedMetricMode::RATE);
56+
o2::monitoring::Metric derivedTagged = derivedHandler.process(metricTagged, DerivedMetricMode::RATE);
5757
BOOST_CHECK_EQUAL(derived.getName(), "metricDoubleRate");
5858
BOOST_CHECK_EQUAL(derivedTagged.getName(), "metricDoubleRate");
5959
BOOST_WARN_CLOSE(boost::get<double>(derived.getValue()), results[i].rate, 5.0);
@@ -77,7 +77,7 @@ BOOST_AUTO_TEST_CASE(derivedRateUint64_t) {
7777
try {
7878
std::this_thread::sleep_for(std::chrono::milliseconds(100));
7979
o2::monitoring::Metric metric(result.value, name);
80-
o2::monitoring::Metric derived = derivedHandler.rate(metric);
80+
o2::monitoring::Metric derived = derivedHandler.process(metric, DerivedMetricMode::RATE);
8181
BOOST_CHECK_EQUAL(derived.getName(), "metricUint64_tRate");
8282
BOOST_WARN_CLOSE(boost::get<double>(derived.getValue()), result.rate, 1.0);
8383
} catch(MonitoringException &e) {
@@ -99,8 +99,8 @@ BOOST_AUTO_TEST_CASE(divisionByZero)
9999
o2::monitoring::DerivedMetrics derivedHandler;
100100
o2::monitoring::Metric metric(10, name);
101101

102-
derivedHandler.rate(metric);
103-
BOOST_CHECK_EXCEPTION(derivedHandler.rate(metric), MonitoringException, exceptionCheck);
102+
derivedHandler.process(metric, DerivedMetricMode::RATE);
103+
BOOST_CHECK_EXCEPTION(derivedHandler.process(metric, DerivedMetricMode::RATE), MonitoringException, exceptionCheck);
104104
}
105105

106106
BOOST_AUTO_TEST_CASE(derivedIncrementInt) {
@@ -113,7 +113,7 @@ BOOST_AUTO_TEST_CASE(derivedIncrementInt) {
113113
std::string name("metricInt");
114114
for (auto const result : results) {
115115
o2::monitoring::Metric metric(result.value, name);
116-
o2::monitoring::Metric derived = derivedHandler.increment(metric);
116+
o2::monitoring::Metric derived = derivedHandler.process(metric, DerivedMetricMode::INCREMENT);
117117
BOOST_CHECK_EQUAL(boost::get<int>(derived.getValue()), result.rate);
118118
}
119119
}
@@ -128,7 +128,7 @@ BOOST_AUTO_TEST_CASE(derivedIncrementUint64_t) {
128128
std::string name("metricUint64_t");
129129
for (auto const result : results) {
130130
o2::monitoring::Metric metric(result.value, name);
131-
o2::monitoring::Metric derived = derivedHandler.increment(metric);
131+
o2::monitoring::Metric derived = derivedHandler.process(metric, DerivedMetricMode::INCREMENT);
132132
BOOST_CHECK_EQUAL(boost::get<uint64_t>(derived.getValue()), result.rate);
133133
}
134134
}
@@ -143,7 +143,7 @@ BOOST_AUTO_TEST_CASE(derivedIncrementDouble) {
143143
std::string name("metricDouble");
144144
for (auto const result : results) {
145145
o2::monitoring::Metric metric(result.value, name);
146-
o2::monitoring::Metric derived = derivedHandler.increment(metric);
146+
o2::monitoring::Metric derived = derivedHandler.process(metric, DerivedMetricMode::INCREMENT);
147147
BOOST_CHECK_CLOSE(boost::get<double>(derived.getValue()), result.rate, 0.01);
148148
}
149149
}

0 commit comments

Comments
 (0)