Skip to content

Commit 067567e

Browse files
authored
Derived metrics should be aware of tags (#105)
* Make derived metrics aware of tags * Adapt example * Extend derived test in order to cover usecase with different tags
1 parent 27d62b7 commit 067567e

File tree

5 files changed

+28
-24
lines changed

5 files changed

+28
-24
lines changed

examples/4-RateDerivedMetric.cxx

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@
55

66
#include "Monitoring/MonitoringFactory.h"
77

8-
using Monitoring = o2::monitoring::MonitoringFactory;
9-
using DerivedMetricMode = o2::monitoring::DerivedMetricMode;
8+
using namespace o2::monitoring;
109

1110
int main() {
1211
// Configure monitoring
1312
// Pass string with list of URLs as parameter
14-
auto monitoring = Monitoring::Get("stdout://");
13+
auto monitoring = MonitoringFactory::Get("stdout://");
1514

1615
// now send at least two metrics to see the result
1716
for (int i = 0; i < 101; i += 10) {
18-
monitoring->send({i, "myMetric"}, DerivedMetricMode::RATE);
19-
std::this_thread::sleep_for(std::chrono::milliseconds(250));
17+
monitoring->send(Metric{i, "myMetric"}.addTags({tags::Subsystem::Readout}), DerivedMetricMode::RATE);
18+
std::this_thread::sleep_for(std::chrono::milliseconds(100));
2019
}
2120
}

include/Monitoring/Metric.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ class Metric
7575
/// Add user defined tags
7676
/// \param tags r-value to vector of tags
7777
/// \return r-value to "this" - to be able to chain methods
78-
Metric&& addTags(std::initializer_list<unsigned int>&& tags);
78+
Metric&& addTags(std::vector<unsigned int>&& tags);
7979

8080
/// Generetes current timestamp
8181
/// return timestamp as std::chrono::system_clock

src/DerivedMetrics.cxx

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,17 @@ namespace monitoring
2525
Metric DerivedMetrics::rate(Metric& metric)
2626
{
2727
// disallow string
28-
std::string name = metric.getName();
28+
auto tags = metric.getTags();
29+
std::string key = metric.getName() + std::string(tags.begin(), tags.end());
2930
if (metric.getType() == MetricType::STRING) {
3031
throw MonitoringException("DerivedMetrics/ProcessMetric", "Not able to process string values");
3132
}
3233

3334
// search for previous value
34-
auto search = mStorage.find(name);
35+
auto search = mStorage.find(key);
3536
if (search == mStorage.end()) {
36-
mStorage.insert(std::make_pair(name, metric));
37-
return Metric{(double) 0.0, name + "Rate"};
37+
mStorage.insert(std::make_pair(key, metric));
38+
return Metric{(double) 0.0, metric.getName() + "Rate"}.addTags(std::move(tags));
3839
}
3940

4041
auto timestampDifference = std::chrono::duration_cast<std::chrono::milliseconds>(
@@ -52,25 +53,25 @@ Metric DerivedMetrics::rate(Metric& metric)
5253
auto rate = boost::apply_visitor(VariantVisitorRate(timestampCount), current, previous);
5354

5455
// swap metrics
55-
mStorage.erase(name);
56-
mStorage.insert(std::make_pair(name, metric));
57-
58-
return Metric{rate, name + "Rate"};
56+
mStorage.erase(key);
57+
mStorage.insert(std::make_pair(key, metric));
58+
return Metric{rate, metric.getName() + "Rate"}.addTags(std::move(tags));
5959
}
6060

6161
Metric DerivedMetrics::increment(Metric& metric) {
62-
std::string name = metric.getName();
63-
auto search = mStorage.find(name);
62+
auto tags = metric.getTags();
63+
std::string key = metric.getName() + std::string(tags.begin(), tags.end());
64+
auto search = mStorage.find(key);
6465
if (search != mStorage.end()) {
6566
auto currentValue = metric.getValue();
6667
auto storedValue = search->second.getValue();
6768
auto value = boost::apply_visitor(VariantVisitorAdd(), currentValue, storedValue);
6869
mStorage.erase(search);
69-
Metric result = Metric{value, name};
70-
mStorage.insert(std::make_pair(name, result));
70+
Metric result = Metric{value, metric.getName()}.addTags(std::move(tags));
71+
mStorage.insert(std::make_pair(key, result));
7172
return result;
7273
}
73-
mStorage.insert(std::make_pair(name, metric));
74+
mStorage.insert(std::make_pair(key, metric));
7475
return metric;
7576
}
7677

src/Metric.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ boost::variant< int, std::string, double, uint64_t > Metric::getValue() const
5656
return mValue;
5757
}
5858

59-
Metric&& Metric::addTags(std::initializer_list<unsigned int>&& tags)
59+
Metric&& Metric::addTags(std::vector<unsigned int>&& tags)
6060
{
6161
mTags = std::move(tags);
6262
return std::move(*this);

test/testDerived.cxx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,16 +44,20 @@ BOOST_AUTO_TEST_CASE(derivedRateDouble) {
4444
double rate;
4545
};
4646
std::vector<RateResults> results = {{1.2, 0 }, {11.2, 100}, {21.2, 100}, {41.2, 200}, {51.2, 100}, {61, 98}};
47+
std::vector<RateResults> resultsTagged = {{0.5, 0 }, {5.5, 50}, {10.5, 50}, {20.5, 100}, {40.5, 200}, {45.5, 50}};
4748
o2::monitoring::DerivedMetrics derivedHandler;
4849
std::string name("metricDouble");
49-
50-
for (auto const result : results) {
50+
for(std::size_t i = 0; i < results.size(); i++) {
5151
try {
5252
std::this_thread::sleep_for(std::chrono::milliseconds(100));
53-
o2::monitoring::Metric metric(result.value, name);
53+
o2::monitoring::Metric metric(results[i].value, name);
54+
o2::monitoring::Metric metricTagged = Metric{resultsTagged[i].value, name}.addTags({o2::monitoring::tags::Subsystem::Readout});
5455
o2::monitoring::Metric derived = derivedHandler.rate(metric);
56+
o2::monitoring::Metric derivedTagged = derivedHandler.rate(metricTagged);
5557
BOOST_CHECK_EQUAL(derived.getName(), "metricDoubleRate");
56-
BOOST_WARN_CLOSE(boost::get<double>(derived.getValue()), result.rate, 1.0);
58+
BOOST_CHECK_EQUAL(derivedTagged.getName(), "metricDoubleRate");
59+
BOOST_WARN_CLOSE(boost::get<double>(derived.getValue()), results[i].rate, 5.0);
60+
BOOST_WARN_CLOSE(boost::get<double>(derivedTagged.getValue()), resultsTagged[i].rate, 5.0);
5761
} catch(MonitoringException &e) {
5862
BOOST_CHECK_EQUAL(e.what(), std::string("Not enough values"));
5963
}

0 commit comments

Comments
 (0)