Skip to content

Commit

Permalink
Add concept of run number (#234)
Browse files Browse the repository at this point in the history
  • Loading branch information
awegrzyn authored Mar 19, 2021
1 parent ad665d1 commit 55033b8
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 15 deletions.
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ endif()

# Define project
project(Monitoring
VERSION 3.6.0
VERSION 3.7.0
DESCRIPTION "O2 Monitoring library"
LANGUAGES CXX
)
Expand Down Expand Up @@ -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
)

Expand Down
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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).
Expand Down
22 changes: 22 additions & 0 deletions examples/9-RunNumber.cxx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
///
/// \file 9-RunNumber.cxx
/// \author Adam Wegrzynek <[email protected]>
///

#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"});
}
13 changes: 12 additions & 1 deletion include/Monitoring/Backend.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 3 additions & 5 deletions include/Monitoring/Monitoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions include/Monitoring/Tags.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ enum class Key : unsigned short int {
FLP,
EPN,
Unit,
Run,
ID,
Type,
CRORC,
Expand All @@ -48,7 +47,7 @@ enum class Key : unsigned short int {
};

/// Tag keys array
static constexpr std::array<std::string_view, 15> TAG_KEY = {
static constexpr std::array<std::string_view, 14> TAG_KEY = {
"hostname"sv,
"rolenane"sv,
"name"sv,
Expand All @@ -58,7 +57,6 @@ static constexpr std::array<std::string_view, 15> TAG_KEY = {
"FLP"sv,
"EPN"sv,
"unit"sv,
"run"sv,
"id"sv,
"type"sv,
"CRORC"sv,
Expand Down
1 change: 1 addition & 0 deletions src/Backends/ApMonBackend.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/Backends/InfluxDB.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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 << '=';
Expand Down
1 change: 1 addition & 0 deletions src/Backends/StdOut.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
7 changes: 7 additions & 0 deletions src/Monitoring.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -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> backend)
{
ProcessDetails processDetails{};
Expand Down

0 comments on commit 55033b8

Please sign in to comment.