Skip to content

Commit

Permalink
Add metrics labels to gauges and histograms
Browse files Browse the repository at this point in the history
Add method and status code labels to some metrics.

Jira:
  • Loading branch information
Eduardo Ramos Testillano (eramedu) committed Nov 26, 2024
1 parent 955168b commit 9d365ed
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 47 deletions.
8 changes: 2 additions & 6 deletions include/ert/http2comm/Http2Client.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,19 +129,15 @@ class Http2Client

// Idem for gauges:
ert::metrics::gauge_family_t *responses_delay_seconds_gauge_family_ptr_{};
ert::metrics::gauge_t *responses_delay_seconds_gauge_{};
ert::metrics::gauge_family_t *sent_messages_size_bytes_gauge_family_ptr_{};
ert::metrics::gauge_t *sent_messages_size_bytes_gauge_{};
ert::metrics::gauge_family_t *received_messages_size_bytes_gauge_family_ptr_{};
ert::metrics::gauge_t *received_messages_size_bytes_gauge_{};

// Idem for histograms:
ert::metrics::histogram_family_t *responses_delay_seconds_histogram_family_ptr_{};
ert::metrics::histogram_t *responses_delay_seconds_histogram_{};
ert::metrics::histogram_family_t *sent_messages_size_bytes_histogram_family_ptr_{};
ert::metrics::histogram_t *sent_messages_size_bytes_histogram_{};
ert::metrics::histogram_family_t *received_messages_size_bytes_histogram_family_ptr_{};
ert::metrics::histogram_t *received_messages_size_bytes_histogram_{};
ert::metrics::bucket_boundaries_t response_delay_seconds_histogram_bucket_boundaries_;
ert::metrics::bucket_boundaries_t message_size_bytes_histogram_bucket_boundaries_; // both received/sent (simplification)

std::atomic<std::uint64_t> reception_id_{};
std::atomic<std::size_t> maximum_request_body_size_{};
Expand Down
8 changes: 2 additions & 6 deletions include/ert/http2comm/Http2Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,15 @@ class Http2Server

// Idem for gauges:
ert::metrics::gauge_family_t *responses_delay_seconds_gauge_family_ptr_{};
ert::metrics::gauge_t *responses_delay_seconds_gauge_{};
ert::metrics::gauge_family_t *received_messages_size_bytes_gauge_family_ptr_{};
ert::metrics::gauge_t *received_messages_size_bytes_gauge_{};
ert::metrics::gauge_family_t *sent_messages_size_bytes_gauge_family_ptr_{};
ert::metrics::gauge_t *sent_messages_size_bytes_gauge_{};

// Idem for histograms:
ert::metrics::histogram_family_t *responses_delay_seconds_histogram_family_ptr_{};
ert::metrics::histogram_t *responses_delay_seconds_histogram_{};
ert::metrics::histogram_family_t *received_messages_size_bytes_histogram_family_ptr_{};
ert::metrics::histogram_t *received_messages_size_bytes_histogram_{};
ert::metrics::histogram_family_t *sent_messages_size_bytes_histogram_family_ptr_{};
ert::metrics::histogram_t *sent_messages_size_bytes_histogram_{};
ert::metrics::bucket_boundaries_t response_delay_seconds_histogram_bucket_boundaries_;
ert::metrics::bucket_boundaries_t message_size_bytes_histogram_bucket_boundaries_; // both received/sent (simplification)

std::atomic<std::uint64_t> reception_id_{};
std::atomic<std::size_t> maximum_request_body_size_{};
Expand Down
34 changes: 16 additions & 18 deletions src/Http2Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,28 +75,20 @@ void Http2Client::enableMetrics(ert::metrics::Metrics *metrics,
ert::metrics::labels_t familyLabels = {{"source", (source.empty() ? name_:source)}};

observed_requests_sents_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_resquests_sents_counter"), std::string("Requests sents observed counter in ") + name_, familyLabels));
ert::metrics::counter_t *observed_requests_sents_counter = &(observed_requests_sents_counter_family_ptr_->Add({{"method", ""}})); // preventive creation: this way it will be scraped even not processed and will ease dashboard creation
observed_requests_unsents_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_resquests_unsent_counter"), std::string("Requests unsents observed counter in ") + name_, familyLabels));
ert::metrics::counter_t *observed_requests_unsents_counter = &(observed_requests_unsents_counter_family_ptr_->Add({{"method", ""}})); // preventive creation: this way it will be scraped even not processed and will ease dashboard creation
observed_responses_received_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_responses_received_counter"), std::string("Responses received observed counter in ") + name_, familyLabels));
ert::metrics::counter_t *observed_responses_received_counter = &(observed_responses_received_counter_family_ptr_->Add({{"method", ""}, {"status_code", ""}})); // preventive creation: this way it will be scraped even not processed and will ease dashboard creation
observed_responses_timedout_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_responses_timedout_counter"), std::string("Responses timed-out observed counter in ") + name_, familyLabels));
ert::metrics::counter_t *observed_responses_timedout_counter = &(observed_responses_timedout_counter_family_ptr_->Add({{"method", ""}})); // preventive creation: this way it will be scraped even not processed and will ease dashboard creation

responses_delay_seconds_gauge_family_ptr_ = &(metrics_->addGaugeFamily(name_ + std::string("_responses_delay_seconds_gauge"), std::string("Message responses delay gauge (seconds) in ") + name_, familyLabels));
responses_delay_seconds_gauge_ = &(responses_delay_seconds_gauge_family_ptr_->Add({})); // we could create ad-hoc gauges for each http2 method, but it is probably overkilling
sent_messages_size_bytes_gauge_family_ptr_ = &(metrics_->addGaugeFamily(name_ + std::string("_sent_messages_size_bytes_gauge"), std::string("Sent messages sizes gauge (bytes) in ") + name_, familyLabels));
sent_messages_size_bytes_gauge_ = &(sent_messages_size_bytes_gauge_family_ptr_->Add({})); // we could create ad-hoc gauges for each http2 method, but it is probably overkilling
received_messages_size_bytes_gauge_family_ptr_ = &(metrics_->addGaugeFamily(name_ + std::string("_received_messages_size_bytes_gauge"), std::string("Received messages sizes gauge (bytes) in ") + name_, familyLabels));
received_messages_size_bytes_gauge_ = &(received_messages_size_bytes_gauge_family_ptr_->Add({})); // we could create ad-hoc gauges for each http2 method, but it is probably overkilling

// Omit 'histogram' from family name, because it is confusing: this kind of metric generates 'bucket', 'sum' and 'count' which are good enough to understand the concept behind (only bucket is histogram):
responses_delay_seconds_histogram_family_ptr_ = &(metrics_->addHistogramFamily(name_ + std::string("_responses_delay_seconds"), std::string("Message responses delay (seconds) in ") + name_, familyLabels));
responses_delay_seconds_histogram_ = &(responses_delay_seconds_histogram_family_ptr_->Add({}, responseDelaySecondsHistogramBucketBoundaries)); // we could create ad-hoc histograms for each http2 method, but it is probably overkilling
sent_messages_size_bytes_histogram_family_ptr_ = &(metrics_->addHistogramFamily(name_ + std::string("_sent_messages_size_bytes"), std::string("Sent messages sizes (bytes) in ") + name_, familyLabels));
sent_messages_size_bytes_histogram_ = &(sent_messages_size_bytes_histogram_family_ptr_->Add({}, messageSizeBytesHistogramBucketBoundaries)); // we could create ad-hoc histograms for each http2 method, but it is probably overkilling
received_messages_size_bytes_histogram_family_ptr_ = &(metrics_->addHistogramFamily(name_ + std::string("_received_messages_size_bytes"), std::string("Received messages sizes (bytes) in ") + name_, familyLabels));
received_messages_size_bytes_histogram_ = &(received_messages_size_bytes_histogram_family_ptr_->Add({}, messageSizeBytesHistogramBucketBoundaries)); // we could create ad-hoc histograms for each http2 method, but it is probably overkilling

response_delay_seconds_histogram_bucket_boundaries_ = responseDelaySecondsHistogramBucketBoundaries;
message_size_bytes_histogram_bucket_boundaries_ = messageSizeBytesHistogramBucketBoundaries;
}
}

Expand Down Expand Up @@ -152,8 +144,10 @@ Http2Client::response Http2Client::send(
counter.Increment();

std::size_t requestBodySize = body.size();
sent_messages_size_bytes_gauge_->Set(requestBodySize);
sent_messages_size_bytes_histogram_->Observe(requestBodySize);
auto& gauge = sent_messages_size_bytes_gauge_family_ptr_->Add({{"method", method}});
gauge.Set(requestBodySize);
auto& histogram = sent_messages_size_bytes_histogram_family_ptr_->Add({{"method", method}}, message_size_bytes_histogram_bucket_boundaries_);
histogram.Observe(requestBodySize);
}

auto url = getUri(path);
Expand Down Expand Up @@ -214,12 +208,14 @@ Http2Client::response Http2Client::send(
std::string msg = ert::tracing::Logger::asString("Context duration: %d us", durationUs);
ert::tracing::Logger::debug(msg, ERT_FILE_LOCATION);
);
responses_delay_seconds_gauge_->Set(durationSeconds);
responses_delay_seconds_histogram_->Observe(durationSeconds);
auto& gauge = responses_delay_seconds_gauge_family_ptr_->Add({{"method", method}, {"status_code", std::to_string(res.status_code())}});
gauge.Set(durationSeconds);
auto& histogram = responses_delay_seconds_histogram_family_ptr_->Add({{"method", method}, {"status_code", std::to_string(res.status_code())}}, response_delay_seconds_histogram_bucket_boundaries_);
histogram.Observe(durationSeconds);
}

res.on_data(
[task, &res, this](const uint8_t* data, std::size_t len)
[task, &res, &method, this](const uint8_t* data, std::size_t len)
{
if (len > 0)
{
Expand All @@ -235,8 +231,10 @@ Http2Client::response Http2Client::send(
// metrics
if (metrics_) {
std::size_t responseBodySize = task->data.size();
received_messages_size_bytes_gauge_->Set(responseBodySize);
received_messages_size_bytes_histogram_->Observe(responseBodySize);
auto& gauge = received_messages_size_bytes_gauge_family_ptr_->Add({{"method", method}, {"status_code", std::to_string(res.status_code())}});
gauge.Set(responseBodySize);
auto& histogram = received_messages_size_bytes_histogram_family_ptr_->Add({{"method", method}, {"status_code", std::to_string(res.status_code())}}, message_size_bytes_histogram_bucket_boundaries_);
histogram.Observe(responseBodySize);
}
}
});
Expand Down
13 changes: 3 additions & 10 deletions src/Http2Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,26 +90,19 @@ void Http2Server::enableMetrics(ert::metrics::Metrics *metrics,
ert::metrics::labels_t familyLabels = {{"source", (source.empty() ? name_:source)}};

observed_requests_accepted_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_resquests_accepted_counter"), std::string("Requests accepted observed counter in ") + name_, familyLabels));
ert::metrics::counter_t *observed_requests_accepted_counter = &(observed_requests_accepted_counter_family_ptr_->Add({{"method", ""}})); // preventive creation: this way it will be scraped even not processed and will ease dashboard creation
observed_requests_errored_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_resquests_errored_counter"), std::string("Requests errored observed counter in ") + name_, familyLabels));
ert::metrics::counter_t *observed_requests_errored_counter = &(observed_requests_errored_counter_family_ptr_->Add({{"method", ""}})); // preventive creation: this way it will be scraped even not processed and will ease dashboard creation
observed_responses_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_responses_counter"), std::string("Responses observed counter in ") + name_, familyLabels));
ert::metrics::counter_t *observed_responses_counter = &(observed_responses_counter_family_ptr_->Add({{"method", ""}, {"status_code", ""}})); // preventive creation: this way it will be scraped even not processed and will ease dashboard creation

responses_delay_seconds_gauge_family_ptr_ = &(metrics_->addGaugeFamily(name_ + std::string("_responses_delay_seconds_gauge"), std::string("Message responses delay gauge (seconds) in ") + name_, familyLabels));
responses_delay_seconds_gauge_ = &(responses_delay_seconds_gauge_family_ptr_->Add({})); // we could create ad-hoc gauges for each http2 method, but it is probably overkilling
received_messages_size_bytes_gauge_family_ptr_ = &(metrics_->addGaugeFamily(name_ + std::string("_received_messages_size_bytes_gauge"), std::string("Received messages sizes gauge (bytes) in ") + name_, familyLabels));
received_messages_size_bytes_gauge_ = &(received_messages_size_bytes_gauge_family_ptr_->Add({})); // we could create ad-hoc gauges for each http2 method, but it is probably overkilling
sent_messages_size_bytes_gauge_family_ptr_ = &(metrics_->addGaugeFamily(name_ + std::string("_sent_messages_size_bytes_gauge"), std::string("Sent messages sizes gauge (bytes) in ") + name_, familyLabels));
sent_messages_size_bytes_gauge_ = &(sent_messages_size_bytes_gauge_family_ptr_->Add({})); // we could create ad-hoc gauges for each http2 method, but it is probably overkilling

// Omit 'histogram' from family name, because it is confusing: this kind of metric generates 'bucket', 'sum' and 'count' which are good enough to understand the concept behind (only bucket is histogram):
responses_delay_seconds_histogram_family_ptr_ = &(metrics_->addHistogramFamily(name_ + std::string("_responses_delay_seconds"), std::string("Message responses delay (seconds) in ") + name_, familyLabels));
responses_delay_seconds_histogram_ = &(responses_delay_seconds_histogram_family_ptr_->Add({}, responseDelaySecondsHistogramBucketBoundaries)); // we could create ad-hoc histograms for each http2 method, but it is probably overkilling
received_messages_size_bytes_histogram_family_ptr_ = &(metrics_->addHistogramFamily(name_ + std::string("_received_messages_size_bytes"), std::string("Received messages sizes (bytes) in ") + name_, familyLabels));
received_messages_size_bytes_histogram_ = &(received_messages_size_bytes_histogram_family_ptr_->Add({}, messageSizeBytesHistogramBucketBoundaries)); // we could create ad-hoc histograms for each http2 method, but it is probably overkilling
sent_messages_size_bytes_histogram_family_ptr_ = &(metrics_->addHistogramFamily(name_ + std::string("_sent_messages_size_bytes"), std::string("Sent messages sizes (bytes) in ") + name_, familyLabels));
sent_messages_size_bytes_histogram_ = &(sent_messages_size_bytes_histogram_family_ptr_->Add({}, messageSizeBytesHistogramBucketBoundaries)); // we could create ad-hoc histograms for each http2 method, but it is probably overkilling

response_delay_seconds_histogram_bucket_boundaries_ = responseDelaySecondsHistogramBucketBoundaries;
message_size_bytes_histogram_bucket_boundaries_ = messageSizeBytesHistogramBucketBoundaries;
}
}

Expand Down
21 changes: 14 additions & 7 deletions src/Stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -204,17 +204,24 @@ void Stream::close() {
std::string msg = ert::tracing::Logger::asString("Context duration: %d us", durationUs);
ert::tracing::Logger::debug(msg, ERT_FILE_LOCATION);
);
server_->responses_delay_seconds_gauge_->Set(durationSeconds);

auto& gauge = server_->responses_delay_seconds_gauge_family_ptr_->Add({{"method", req_.method()}, {"status_code", std::to_string(status_code_)}});
gauge.Set(durationSeconds);

std::size_t requestBodySize = request_body_.size();
std::size_t responseBodySize = response_body_.size();

server_->received_messages_size_bytes_gauge_->Set(requestBodySize);
server_->sent_messages_size_bytes_gauge_->Set(responseBodySize);

server_->responses_delay_seconds_histogram_->Observe(durationSeconds);
server_->received_messages_size_bytes_histogram_->Observe(requestBodySize);
server_->sent_messages_size_bytes_histogram_->Observe(responseBodySize);
auto& gauge2 = server_->received_messages_size_bytes_gauge_family_ptr_->Add({{"method", req_.method()}});
gauge2.Set(requestBodySize);
auto& gauge3 = server_->sent_messages_size_bytes_gauge_family_ptr_->Add({{"method", req_.method()}, {"status_code", std::to_string(status_code_)}});
gauge3.Set(responseBodySize);

auto& histogram = server_->responses_delay_seconds_histogram_family_ptr_->Add({{"method", req_.method()}, {"status_code", std::to_string(status_code_)}}, server_->response_delay_seconds_histogram_bucket_boundaries_);
histogram.Observe(durationSeconds);
auto& histogram2 = server_->received_messages_size_bytes_histogram_family_ptr_->Add({{"method", req_.method()}}, server_->message_size_bytes_histogram_bucket_boundaries_);
histogram2.Observe(durationSeconds);
auto& histogram3 = server_->sent_messages_size_bytes_histogram_family_ptr_->Add({{"method", req_.method()}, {"status_code", std::to_string(status_code_)}}, server_->message_size_bytes_histogram_bucket_boundaries_);
histogram3.Observe(durationSeconds);

// counters
auto& counter = server_->observed_responses_counter_family_ptr_->Add({{"method", req_.method()}, {"status_code", std::to_string(status_code_)}});
Expand Down

0 comments on commit 9d365ed

Please sign in to comment.