Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unique values in the buffer #150

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ monitoring->flushBuffer();

See how it works in the example: [examples/10-Buffering.cxx](examples/10-Buffering.cxx).

### Unique metric buffering
In addition to above, you may want to keep only unique metrics (defined by metric name) within the buffer (only the last metric is kept).
```cpp
monitoring->enableUniqueBuffering(const std::size_t maxSize)
```

### Calculating derived metrics
The module can calculate derived metrics. To do so, use optional `DerivedMetricMode mode` parameter of `send` method:
```
Expand Down
7 changes: 7 additions & 0 deletions include/Monitoring/Monitoring.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ class Monitoring
/// Flushes metric buffer (this can also happen when buffer is full)
void flushBuffer();

/// Enables unique metric buffering
/// There might be only unique metric name within a buffer
void enableUniqueBuffering(const std::size_t size = 128);

/// Enables metric buffering
/// \param size buffer size
void enableBuffering(const std::size_t size = 128);
Expand Down Expand Up @@ -137,6 +141,9 @@ class Monitoring
/// Metric buffer
std::unordered_map<std::underlying_type<Verbosity>::type, std::vector<Metric>> mStorage;

/// Flag that states whether unique buffering is enabled
bool mUniqueBuffering;

/// Flag stating whether metric buffering is enabled
bool mBuffering;

Expand Down
20 changes: 19 additions & 1 deletion src/Monitoring.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,19 @@ Monitoring::Monitoring()
{
mProcessMonitor = std::make_unique<ProcessMonitor>();
mDerivedHandler = std::make_unique<DerivedMetrics>();
mUniqueBuffering = false;
mBuffering = false;
mProcessMonitoringInterval = 0;
mAutoPushInterval = 0;
mMonitorRunning = false;
}

void Monitoring::enableUniqueBuffering(const std::size_t size)
{
mUniqueBuffering = true;
enableBuffering(size);
}

void Monitoring::enableBuffering(const std::size_t size)
{
mBufferSize = size;
Expand Down Expand Up @@ -182,7 +189,18 @@ void Monitoring::transmit(Metric&& metric)
{
if (mBuffering) {
auto index = static_cast<std::underlying_type<Verbosity>::type>(metric.getVerbosity());
mStorage[index].push_back(std::move(metric));
if (mUniqueBuffering) {
auto find = std::find_if(mStorage[index].begin(), mStorage[index].end(), [&metric](Metric const& m) {
return m.getName() == metric.getName();
});
if (find == std::end(mStorage[index])) {
mStorage[index].push_back(std::move(metric));
} else {
std::swap(*find, metric);
}
} else {
mStorage[index].push_back(std::move(metric));
}
if (mStorage[index].size() >= mBufferSize) {
flushBuffer(index);
}
Expand Down
15 changes: 15 additions & 0 deletions test/testMonitoring.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,21 @@ BOOST_AUTO_TEST_CASE(testSymbols)
BOOST_WARN_MESSAGE(BOOST_IS_DEFINED( O2_MONITORING_OS_MAC ), "Mac OS detected");
}

BOOST_AUTO_TEST_CASE(uniqueBuffering)
{
auto monitoring = Monitoring::Get("stdout://");
monitoring->enableUniqueBuffering(4);
monitoring->send({10, "myMetricIntUnique"});
monitoring->send({11, "myMetricInt"});
monitoring->send({12, "myMetricInt"});
monitoring->send({13, "myMetricIntAnother"});
monitoring->send({14, "myMetricInt"});
monitoring->send({15, "myMetricInt"});
monitoring->send({16, "myMetricInt"});
monitoring->send({17, "myMetricIntFlush"});
monitoring->send({18, "myMetricInt"});
}

} // namespace Test
} // namespace monitoring
} // namespace o2