From 548fc1470fc9d773c9e6d5819a4f467bf2f936ef Mon Sep 17 00:00:00 2001 From: Adam Wegrzynek Date: Thu, 21 Feb 2019 14:21:03 +0100 Subject: [PATCH] Metric verbositiy policy with regex (#119) --- README.md | 6 ++++++ include/Monitoring/Metric.h | 10 ++++++++++ src/Metric.cxx | 26 ++++++++++++++++++++------ test/testMetric.cxx | 15 ++++++++++++++- 4 files changed, 50 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 7f8ce0c16..94fca708e 100644 --- a/README.md +++ b/README.md @@ -182,6 +182,12 @@ metric = 10; ``` See how it works in the example: [examples/11-AutoUpdate.cxx](examples/11-AutoUpdate.cxx). +### Regex verbosity policy +Overwrite metric verbosities using regex expression: +``` +Metric::setVerbosityPolicy(Verbosity verbosity, const std::regex& regex) +``` + ## System monitoring, server-side backends installation and configuration This guide explains manual installation. For `ansible` deployment see [AliceO2Group/system-configuration](https://gitlab.cern.ch/AliceO2Group/system-configuration/tree/master/ansible) gitlab repo. diff --git a/include/Monitoring/Metric.h b/include/Monitoring/Metric.h index 99dbaa250..9581dd813 100644 --- a/include/Monitoring/Metric.h +++ b/include/Monitoring/Metric.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include "Tags.h" @@ -103,6 +104,9 @@ class Metric /// Default metric verbosity static Verbosity DefaultVerbosity; + + /// Regex policy setter + static void setVerbosityPolicy(Verbosity verbosity, const std::regex& regex); protected: /// Allow DerivedMetrics access to setTags friend class o2::monitoring::DerivedMetrics; @@ -124,6 +128,12 @@ class Metric /// Metric verbosity Verbosity mVerbosity; + + /// Regex policy map + static std::map::type, std::regex> mRegexPolicy; + + /// Overwirte verbosity using regex policy + void overwriteVerbosity(); }; } // namespace monitoring diff --git a/src/Metric.cxx b/src/Metric.cxx index b58080af4..4713d9ec5 100644 --- a/src/Metric.cxx +++ b/src/Metric.cxx @@ -32,24 +32,23 @@ const std::string& Metric::getName() const Metric::Metric(int value, const std::string& name, Verbosity verbosity) : mValue(value), mName(name), mTimestamp(Metric::getCurrentTimestamp()), mVerbosity(verbosity) -{ -} +{ overwriteVerbosity(); } Metric::Metric(std::string value, const std::string& name, Verbosity verbosity) : mValue(value), mName(name), mTimestamp(Metric::getCurrentTimestamp()), mVerbosity(verbosity) -{} +{ overwriteVerbosity(); } Metric::Metric(double value, const std::string& name, Verbosity verbosity) : mValue(value), mName(name), mTimestamp(Metric::getCurrentTimestamp()), mVerbosity(verbosity) -{} +{ overwriteVerbosity(); } Metric::Metric(uint64_t value, const std::string& name, Verbosity verbosity) : mValue(value), mName(name), mTimestamp(Metric::getCurrentTimestamp()), mVerbosity(verbosity) -{} +{ overwriteVerbosity(); } Metric::Metric(boost::variant< int, std::string, double, uint64_t > value, const std::string& name, Verbosity verbosity) : mValue(value), mName(name), mTimestamp(Metric::getCurrentTimestamp()), mVerbosity(verbosity) -{} +{ overwriteVerbosity(); } boost::variant< int, std::string, double, uint64_t > Metric::getValue() const { @@ -61,6 +60,20 @@ Verbosity Metric::getVerbosity() return mVerbosity; } +void Metric::setVerbosityPolicy(Verbosity verbosity, const std::regex& regex) +{ + mRegexPolicy.insert({static_cast::type>(verbosity), regex}); +} + +void Metric::overwriteVerbosity() +{ + for (auto const& [verbosity, regex] : mRegexPolicy) { + if (std::regex_match(mName, regex)) { + mVerbosity = static_cast(verbosity); + } + } +} + Metric&& Metric::addTag(tags::Key key, tags::Value value) { mTags.push_back({static_cast::type>(key), static_cast::type>(value)}); @@ -95,6 +108,7 @@ void Metric::setDefaultVerbosity(Verbosity verbosity) } Verbosity Metric::DefaultVerbosity = Verbosity::Info; +std::map::type, std::regex> Metric::mRegexPolicy; } // namespace monitoring } // namespace o2 diff --git a/test/testMetric.cxx b/test/testMetric.cxx index ba73a74dd..b48412b2b 100644 --- a/test/testMetric.cxx +++ b/test/testMetric.cxx @@ -103,8 +103,21 @@ BOOST_AUTO_TEST_CASE(customCopyConstructor) { BOOST_CHECK_EQUAL(sum, -109); } +BOOST_AUTO_TEST_CASE(regexVerbosityPolicy) +{ + Metric::setVerbosityPolicy(Verbosity::Prod, std::regex("myMetric", std::regex::optimize)); + Metric metric = Metric{10, "myMetric"}; + Metric metric2 = Metric{10, "myValue"}; + BOOST_CHECK_EQUAL(static_cast::type>(metric.getVerbosity()), + static_cast::type>(Verbosity::Prod)); + BOOST_CHECK_EQUAL(static_cast::type>(metric2.getVerbosity()), + static_cast::type>(Verbosity::Info)); +} + BOOST_AUTO_TEST_CASE(verbosity) { - Metric{10, "myMetric", Verbosity::Prod}; + Metric metric = Metric{10, "myMetric", Verbosity::Prod}; + BOOST_CHECK_EQUAL(static_cast::type>(metric.getVerbosity()), + static_cast::type>(Verbosity::Prod)); } } // namespace Test