Skip to content

Commit

Permalink
TIG-1327 make metrics::Operation not RAII and make counts cumulative (m…
Browse files Browse the repository at this point in the history
…ongodb#114)

* TIG-1327 make metrics::Operation not RAII and make counts cumulative
  • Loading branch information
jasonjhchan authored Jan 11, 2019
1 parent 1349e1e commit 2d56f84
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
5 changes: 3 additions & 2 deletions src/cast_core/src/actors/HelloWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ void HelloWorld::run() {
BOOST_LOG_TRIVIAL(info) << config->message;
++_hwCounter;
BOOST_LOG_TRIVIAL(info) << "Counter: " << _hwCounter;
ctx.setOps(1);
ctx.setBytes(config->message.size());
ctx.addOps(1);
ctx.addBytes(config->message.size());
ctx.success();
}
}
}
Expand Down
61 changes: 49 additions & 12 deletions src/gennylib/include/gennylib/metrics.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <vector>

#include <boost/core/noncopyable.hpp>
#include <boost/log/trivial.hpp>

namespace genny::metrics {

Expand Down Expand Up @@ -212,27 +213,41 @@ class TimerImpl : private boost::noncopyable {
class OperationImpl {

public:
OperationImpl(TimerImpl& timer, CounterImpl& iters, CounterImpl& docs, CounterImpl& bytes)
: _timer{std::addressof(timer)},
OperationImpl(const std::string& name,
TimerImpl& timer,
CounterImpl& iters,
CounterImpl& docs,
CounterImpl& bytes)
: _opName{name},
_timer{std::addressof(timer)},
_iters{std::addressof(iters)},
_docs{std::addressof(docs)},
_bytes{std::addressof(bytes)} {}

/**
* Operation name getter to help with exception reporting.
*/
const std::string& getOpName() const {
return _opName;
}


void report(const time_point& started) {
this->_timer->report(started);
this->_iters->reportValue(1);
}

void setBytes(const count_type& size) {
this->_bytes->reportValue(size);
void reportBytes(const count_type& total) {
this->_bytes->reportValue(total);
}

void setOps(const count_type& size) {
this->_docs->reportValue(size);
void reportOps(const count_type& total) {
this->_docs->reportValue(total);
}


private:
const std::string _opName;
TimerImpl* _timer;
CounterImpl* _iters;
CounterImpl* _docs;
Expand Down Expand Up @@ -442,24 +457,45 @@ class OperationContext {
: _op{std::addressof(op)}, _started{metrics::clock::now()} {}

~OperationContext() {
this->report();
if (!_isClosed) {
BOOST_LOG_TRIVIAL(warning)
<< "Metrics not reported because operation '" << this->_op->getOpName()
<< "' did not close with success() or fail().";
}
}

void setBytes(const count_type& size) {
this->_op->setBytes(size);
void addBytes(const count_type& size) {
_totalBytes += size;
}

void setOps(const count_type& size) {
this->_op->setOps(size);
void addOps(const count_type& size) {
_totalOps += size;
}

void success() {
this->report();
_isClosed = true;
}

/**
* An operation does not report metrics upon failure.
*/
void fail() {
_isClosed = true;
}

private:
void report() {
this->_op->report(_started);
this->_op->reportBytes(_totalBytes);
this->_op->reportOps(_totalOps);
}

V1::OperationImpl* _op;
const time_point _started;
count_type _totalBytes = 0;
count_type _totalOps = 0;
bool _isClosed = false;
};


Expand Down Expand Up @@ -516,7 +552,8 @@ class Registry {
return Gauge{this->_gauges[name]};
}
Operation operation(const std::string& name) {
auto op = V1::OperationImpl(this->_timers[name + "_timer"],
auto op = V1::OperationImpl(name,
this->_timers[name + "_timer"],
this->_counters[name + "_iters"],
this->_counters[name + "_docs"],
this->_counters[name + "_bytes"]);
Expand Down

0 comments on commit 2d56f84

Please sign in to comment.