From 55033b82ff54eb63886e3dc6f7f1af2b6ce50b5b Mon Sep 17 00:00:00 2001 From: Adam Wegrzynek Date: Fri, 19 Mar 2021 09:52:02 +0100 Subject: [PATCH] Add concept of run number (#234) --- CMakeLists.txt | 3 ++- README.md | 15 ++++++++++----- examples/9-RunNumber.cxx | 22 ++++++++++++++++++++++ include/Monitoring/Backend.h | 13 ++++++++++++- include/Monitoring/Monitoring.h | 8 +++----- include/Monitoring/Tags.h | 4 +--- src/Backends/ApMonBackend.cxx | 1 + src/Backends/InfluxDB.cxx | 1 + src/Backends/StdOut.cxx | 1 + src/Monitoring.cxx | 7 +++++++ 10 files changed, 60 insertions(+), 15 deletions(-) create mode 100644 examples/9-RunNumber.cxx diff --git a/CMakeLists.txt b/CMakeLists.txt index 847febf15..bd5695430 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,7 +17,7 @@ endif() # Define project project(Monitoring - VERSION 3.6.0 + VERSION 3.7.0 DESCRIPTION "O2 Monitoring library" LANGUAGES CXX ) @@ -188,6 +188,7 @@ set(EXAMPLES examples/6-Increment.cxx examples/7-InternalBenchamrk.cxx examples/8-DbFiller.cxx + examples/9-RunNumber.cxx examples/10-Buffering.cxx ) diff --git a/README.md b/README.md index 0ee575768..1b94b3232 100644 --- a/README.md +++ b/README.md @@ -80,6 +80,7 @@ Metric{"name"}.addValue(10, "value") ``` #### Tags +1. Metric tags Each metric can be tagged with any number of [predefined tags](include/Monitoring/Tags.h). In order to do so use `addTag(tags::Key, tags::Value)` or `addTag(tags::Key, unsigned short)` methods. The latter method allows assigning numeric value to a tag. @@ -89,6 +90,15 @@ Metric{10, "name"}.addTag(tags::Key::Subsystem, tags::Value::QC) See the example: [examples/2-TaggedMetrics.cxx](examples/2-TaggedMetrics.cxx). +2. Global tags +Global tags are added to each metric sent eg. `hostname` tag is added by default by the library. + +You can add your own global tag by calling `addGlobalTag(std::string_view key, std::string_view value)` or `addGlobalTag(tags::Key, tags::Value)` on Monitoring object. + +3. Run number +Run number is special case of a global tag, its value can be overwritten at any time, therefore it benefits simplified handling: `setRunNumber(uint32_t)` + + ### Sending metric Pass metric object to `send` method as l-value reference: ```cpp @@ -137,11 +147,6 @@ The derived value is generated only from the first value of the metric and it is See how it works in the example: [examples/4-RateDerivedMetric.cxx](examples/4-RateDerivedMetric.cxx). -### Global tags -Global tags are added to each metric sent using given monitoring instance. `hostname` is set as global by default. - -You can add your own global tag by calling `addGlobalTag(std::string_view key, std::string_view value)` or `addGlobalTag(tags::Key, tags::Value)`. - ### Process monitoring This feature provides basic performance status of the process. Note that is runs in separate thread (without mutex). diff --git a/examples/9-RunNumber.cxx b/examples/9-RunNumber.cxx new file mode 100644 index 000000000..208bb0310 --- /dev/null +++ b/examples/9-RunNumber.cxx @@ -0,0 +1,22 @@ +/// +/// \file 9-RunNumber.cxx +/// \author Adam Wegrzynek +/// + +#include "Monitoring/MonitoringFactory.h" + +using namespace o2::monitoring; + +int main() +{ + + // Configure monitoring + // Pass string with list of URLs as parameter + auto monitoring = MonitoringFactory::Get("influxdb-stdout://"); + + monitoring->send(Metric{10, "myMetric"}); + monitoring->setRunNumber(1); + monitoring->send(Metric{10, "myMetric"}); + monitoring->setRunNumber(2); + monitoring->send(Metric{10, "myMetric"}); +} diff --git a/include/Monitoring/Backend.h b/include/Monitoring/Backend.h index 672af955d..a5535ac71 100644 --- a/include/Monitoring/Backend.h +++ b/include/Monitoring/Backend.h @@ -35,10 +35,21 @@ class Backend private: /// Verbosity level Verbosity verbosityLevel; +protected: + /// Run number + uint32_t mRunNumber; public: /// Default constructor - Backend() { verbosityLevel = Verbosity::Info; } + Backend() { + mRunNumber = 0; + verbosityLevel = Verbosity::Info; + } + + /// Run number setter + void setRunNumber(uint32_t runNumber) { + mRunNumber = runNumber; + } /// Default destructor virtual ~Backend() = default; diff --git a/include/Monitoring/Monitoring.h b/include/Monitoring/Monitoring.h index 4079d9266..edfcdcd8c 100644 --- a/include/Monitoring/Monitoring.h +++ b/include/Monitoring/Monitoring.h @@ -88,11 +88,9 @@ class Monitoring /// \param value tag value void addGlobalTag(tags::Key key, tags::Value value); - /// Returns a metric which will be periodically sent to backends - /// \param name metric name - /// \return periodically send metric - //ComplexMetric& getAutoPushMetric(std::string name, unsigned int interval = 1); - + /// Sets run number + /// \param name run run number + void setRunNumber(uint32_t run); private: /// Sends multiple (not related to each other) metrics /// \param metrics vector of metrics diff --git a/include/Monitoring/Tags.h b/include/Monitoring/Tags.h index 7455357f9..27c1d50b8 100644 --- a/include/Monitoring/Tags.h +++ b/include/Monitoring/Tags.h @@ -39,7 +39,6 @@ enum class Key : unsigned short int { FLP, EPN, Unit, - Run, ID, Type, CRORC, @@ -48,7 +47,7 @@ enum class Key : unsigned short int { }; /// Tag keys array -static constexpr std::array TAG_KEY = { +static constexpr std::array TAG_KEY = { "hostname"sv, "rolenane"sv, "name"sv, @@ -58,7 +57,6 @@ static constexpr std::array TAG_KEY = { "FLP"sv, "EPN"sv, "unit"sv, - "run"sv, "id"sv, "type"sv, "CRORC"sv, diff --git a/src/Backends/ApMonBackend.cxx b/src/Backends/ApMonBackend.cxx index 3542a4660..e415f5e35 100644 --- a/src/Backends/ApMonBackend.cxx +++ b/src/Backends/ApMonBackend.cxx @@ -68,6 +68,7 @@ void ApMonBackend::send(const Metric& metric) entity += '='; (value > 0) ? entity += tags::GetValue(value) : entity += std::to_string(0 - value); } + if (mRunNumber != 0) convert << ",run=" << mRunNumber; int valueSize = metric.getValuesSize(); char **paramNames, **paramValues; diff --git a/src/Backends/InfluxDB.cxx b/src/Backends/InfluxDB.cxx index d3b34de27..ce162d617 100644 --- a/src/Backends/InfluxDB.cxx +++ b/src/Backends/InfluxDB.cxx @@ -85,6 +85,7 @@ std::string InfluxDB::toInfluxLineProtocol(const Metric& metric) convert << "," << tags::TAG_KEY[key] << "="; (value > 0) ? convert << tags::GetValue(value) : convert << (0 - value); } + if (mRunNumber != 0) convert << ",run=" << mRunNumber; convert << ' '; for (const auto& [name, value] : metric.getValues()) { convert << name << '='; diff --git a/src/Backends/StdOut.cxx b/src/Backends/StdOut.cxx index 8dbdaaa8b..51cd7a948 100644 --- a/src/Backends/StdOut.cxx +++ b/src/Backends/StdOut.cxx @@ -84,6 +84,7 @@ void StdOut::send(const Metric& metric) convert << ',' << tags::TAG_KEY[key] << "="; (value > 0) ? convert << tags::GetValue(value) : convert << (0 - value); } + if (mRunNumber != 0) convert << ",run=" << mRunNumber; convert << '\n'; std::cout << convert.str(); } diff --git a/src/Monitoring.cxx b/src/Monitoring.cxx index c9905f250..aacaa757c 100644 --- a/src/Monitoring.cxx +++ b/src/Monitoring.cxx @@ -110,6 +110,13 @@ void Monitoring::addGlobalTag(tags::Key key, tags::Value value) } } +void Monitoring::setRunNumber(uint32_t run) +{ + for (auto& backend : mBackends) { + backend->setRunNumber(run); + } +} + void Monitoring::addBackend(std::unique_ptr backend) { ProcessDetails processDetails{};