Skip to content

Commit

Permalink
Implement dynamic counters for status codes
Browse files Browse the repository at this point in the history
Implements: #22
  • Loading branch information
testillano authored and Eduardo Ramos Testillano (eramedu) committed Aug 5, 2023
1 parent 951798f commit f194859
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 5 deletions.
2 changes: 2 additions & 0 deletions include/ert/http2comm/Http2Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ class Http2Client
// metrics:
ert::metrics::Metrics *metrics_{};

std::string observed_responses_counter_family_name_{}; // dynamic

ert::metrics::counter_t *observed_requests_post_counter_{};
ert::metrics::counter_t *observed_requests_get_counter_{};
ert::metrics::counter_t *observed_requests_put_counter_{};
Expand Down
2 changes: 2 additions & 0 deletions include/ert/http2comm/Http2Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ class Http2Server
// metrics:
ert::metrics::Metrics *metrics_{};

std::string observed_responses_counter_family_name_{}; // dynamic

ert::metrics::counter_t *observed_requests_post_counter_{};
ert::metrics::counter_t *observed_requests_get_counter_{};
ert::metrics::counter_t *observed_requests_put_counter_{};
Expand Down
13 changes: 9 additions & 4 deletions src/Http2Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ void Http2Client::enableMetrics(ert::metrics::Metrics *metrics,
metrics_ = metrics;

if (metrics_) {
observed_responses_counter_family_name_ = name_ + std::string("_observed_responses_total");

ert::metrics::counter_family_ref_t cf = metrics->addCounterFamily(name_ + std::string("_observed_requests_total"), std::string("Http2 total requests observed in ") + name_);
observed_requests_post_counter_ = &(cf.Add({{"method", "POST"}}));
observed_requests_get_counter_ = &(cf.Add({{"method", "GET"}}));
Expand Down Expand Up @@ -188,8 +190,8 @@ Http2Client::response Http2Client::send(
}

std::size_t requestBodySize = body.size();
messages_size_bytes_rx_gauge_->Set(requestBodySize);
messages_size_bytes_rx_histogram_->Observe(requestBodySize);
messages_size_bytes_tx_gauge_->Set(requestBodySize);
messages_size_bytes_tx_histogram_->Observe(requestBodySize);
}

auto url = getUri(path);
Expand Down Expand Up @@ -240,6 +242,9 @@ Http2Client::response Http2Client::send(
}
// metrics
if (metrics_) {
// Dynamic counters (status code):
metrics_->increaseCounter(observed_responses_counter_family_name_, {{"statuscode", std::to_string(res.status_code())}});

// histograms
task->receptionUs = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch());
double durationUs = (task->receptionUs - task->sendingUs).count();
Expand Down Expand Up @@ -269,8 +274,8 @@ Http2Client::response Http2Client::send(
// metrics
if (metrics_) {
std::size_t responseBodySize = task->data.size();
messages_size_bytes_tx_gauge_->Set(responseBodySize);
messages_size_bytes_tx_histogram_->Observe(responseBodySize);
messages_size_bytes_rx_gauge_->Set(responseBodySize);
messages_size_bytes_rx_histogram_->Observe(responseBodySize);
}
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/Http2Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ Http2Connection::Http2Connection(const std::string& host,
session_(nghttp2::asio_http2::client::session(createSession(io_context_, host, port, secure)))
{
configureSession();
thread_ = std::thread([&] { io_context_.run(); });
thread_ = std::thread([&] { io_context_.run(); }); // only 1 thread !!! That's why Http2Client post is "blocking" and we must
// build an external IO CONTEXT with more workers than 1, to post sends there.
}

Http2Connection::~Http2Connection()
Expand Down
2 changes: 2 additions & 0 deletions src/Http2Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ void Http2Server::enableMetrics(ert::metrics::Metrics *metrics,
metrics_ = metrics;

if (metrics_) {
observed_responses_counter_family_name_ = name_ + std::string("_observed_responses_total");

ert::metrics::counter_family_ref_t cf = metrics->addCounterFamily(name_ + std::string("_observed_requests_total"), std::string("Http2 total requests observed in ") + name_);
observed_requests_post_counter_ = &(cf.Add({{"method", "POST"}}));
observed_requests_get_counter_ = &(cf.Add({{"method", "GET"}}));
Expand Down
4 changes: 4 additions & 0 deletions src/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ void Stream::close() {
server_->messages_size_bytes_tx_histogram_->Observe(responseBodySize);

// counters

// Dynamic counters (status code):
server_->metrics_->increaseCounter(server_->observed_responses_counter_family_name_, {{"statuscode", std::to_string(status_code_)}});

std::string method = req_.method();
if (method == "POST") {
server_->observed_requests_post_counter_->Increment();
Expand Down

0 comments on commit f194859

Please sign in to comment.