Skip to content

Commit

Permalink
Metric verbositiy policy with regex (#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
awegrzyn authored Feb 21, 2019
1 parent 1eb5b6c commit 548fc14
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 7 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
10 changes: 10 additions & 0 deletions include/Monitoring/Metric.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <string>
#include <chrono>
#include <vector>
#include <regex>
#include <boost/variant.hpp>
#include "Tags.h"

Expand Down Expand Up @@ -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;
Expand All @@ -124,6 +128,12 @@ class Metric

/// Metric verbosity
Verbosity mVerbosity;

/// Regex policy map
static std::map<std::underlying_type<Verbosity>::type, std::regex> mRegexPolicy;

/// Overwirte verbosity using regex policy
void overwriteVerbosity();
};

} // namespace monitoring
Expand Down
26 changes: 20 additions & 6 deletions src/Metric.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -61,6 +60,20 @@ Verbosity Metric::getVerbosity()
return mVerbosity;
}

void Metric::setVerbosityPolicy(Verbosity verbosity, const std::regex& regex)
{
mRegexPolicy.insert({static_cast<std::underlying_type<tags::Value>::type>(verbosity), regex});
}

void Metric::overwriteVerbosity()
{
for (auto const& [verbosity, regex] : mRegexPolicy) {
if (std::regex_match(mName, regex)) {
mVerbosity = static_cast<Verbosity>(verbosity);
}
}
}

Metric&& Metric::addTag(tags::Key key, tags::Value value)
{
mTags.push_back({static_cast<std::underlying_type<tags::Key>::type>(key), static_cast<std::underlying_type<tags::Value>::type>(value)});
Expand Down Expand Up @@ -95,6 +108,7 @@ void Metric::setDefaultVerbosity(Verbosity verbosity)
}

Verbosity Metric::DefaultVerbosity = Verbosity::Info;
std::map<std::underlying_type<Verbosity>::type, std::regex> Metric::mRegexPolicy;

} // namespace monitoring
} // namespace o2
15 changes: 14 additions & 1 deletion test/testMetric.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::underlying_type<o2::monitoring::tags::Value>::type>(metric.getVerbosity()),
static_cast<std::underlying_type<o2::monitoring::tags::Value>::type>(Verbosity::Prod));
BOOST_CHECK_EQUAL(static_cast<std::underlying_type<o2::monitoring::tags::Value>::type>(metric2.getVerbosity()),
static_cast<std::underlying_type<o2::monitoring::tags::Value>::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<std::underlying_type<o2::monitoring::tags::Value>::type>(metric.getVerbosity()),
static_cast<std::underlying_type<o2::monitoring::tags::Value>::type>(Verbosity::Prod));
}

} // namespace Test
Expand Down

0 comments on commit 548fc14

Please sign in to comment.