From 2548c2c39bf798ac1a85c21920f343ea542ac581 Mon Sep 17 00:00:00 2001 From: "Eduardo Ramos Testillano (ert)" Date: Thu, 10 Aug 2023 11:33:37 +0200 Subject: [PATCH] Refactor metrics families' names Issue: https://github.com/testillano/http2comm/issues/23 --- include/ert/http2comm/Http2Client.hpp | 33 ++++++++++++++--------- include/ert/http2comm/Http2Server.hpp | 12 +++++++-- src/Http2Client.cpp | 38 ++++++++++++++------------- src/Http2Server.cpp | 36 +++++++++++++------------ 4 files changed, 70 insertions(+), 49 deletions(-) diff --git a/include/ert/http2comm/Http2Client.hpp b/include/ert/http2comm/Http2Client.hpp index e1243aa..4a70d46 100644 --- a/include/ert/http2comm/Http2Client.hpp +++ b/include/ert/http2comm/Http2Client.hpp @@ -112,7 +112,6 @@ class Http2Client std::chrono::microseconds receptionUs; }; - std::string name_{}; // used for metrics: // Metric names should be in lowercase and separated by underscores (_). // Metric names should start with a letter or an underscore (_). // Metric names should be descriptive and meaningful for their purpose. @@ -147,11 +146,29 @@ class Http2Client std::atomic reception_id_{}; std::atomic maximum_request_body_size_{}; + std::unique_ptr connection_; + std::string host_; + std::string port_; + bool secure_; + std::shared_timed_mutex mutex_; + + void reconnect(); + std::string getUri(const std::string &path, const std::string &scheme = "" /* http or https by default, but could be forced here */); + +protected: + std::string name_{}; + public: /** * Class constructor given host, port and secure connection indicator * - * @param name Client name (lower case, as it is used to name prometheus metrics). + * @param name client name. It may be used to prefix the name of metrics families + * (counters, gauges, histograms), so consider to provide a compatible name ([a-zA-Z0-9:_]). + * A good name convention would include the application name and the endpoint identification, + * for example: + * h2agent_traffic_client_myClient4 + * udp_server_h2client[_traffic_client] + * * @param host Endpoint host * @param port Endpoint port * @param secure Secure connection. False by default @@ -162,6 +179,8 @@ class Http2Client /** * Enable metrics * + * The name of families created will be prefixed by the class name given in the constructor. + * * @param metrics Optional metrics object to compute counters and histograms * @param responseDelaySecondsHistogramBucketBoundaries Optional bucket boundaries for response delay seconds histogram * @param messageSizeBytesHistogramBucketBoundaries Optional bucket boundaries for message size bytes histogram @@ -206,16 +225,6 @@ class Http2Client * @return string with connection status (NotOpen, Open, Closed) */ std::string getConnectionStatus() const; - -private: - std::unique_ptr connection_; - std::string host_; - std::string port_; - bool secure_; - std::shared_timed_mutex mutex_; - - void reconnect(); - std::string getUri(const std::string &path, const std::string &scheme = "" /* http or https by default, but could be forced here */); }; } diff --git a/include/ert/http2comm/Http2Server.hpp b/include/ert/http2comm/Http2Server.hpp index 5685bcd..b18877e 100644 --- a/include/ert/http2comm/Http2Server.hpp +++ b/include/ert/http2comm/Http2Server.hpp @@ -62,7 +62,6 @@ namespace http2comm class Http2Server { std::string server_key_password_{}; - std::string name_{}; // used for metrics: // Metric names should be in lowercase and separated by underscores (_). // Metric names should start with a letter or an underscore (_). // Metric names should be descriptive and meaningful for their purpose. @@ -106,13 +105,20 @@ class Http2Server protected: nghttp2::asio_http2::server::http2 server_; + std::string name_{}; public: /** * Class constructor * - * @param name Server name (lower case, as it is used to name prometheus metrics). + * @param name Server name. It may be used to prefix the name of metrics families + * (counters, gauges, histograms), so consider to provide a compatible name ([a-zA-Z0-9:_]). + * A good name convention would include the application name and the endpoint identification, + * for example: + * h2agent_traffic_server + * h2agentB_traffic_server + * * @param workerThreads number of worker threads. * @param maxWorkerThreads number of maximum worker threads which internal processing could grow to. Defaults to '0' which means that maximum equals to provided worker threads. * @param timerIoContext Optional io context to manage response delays @@ -154,6 +160,8 @@ class Http2Server /** * Enable metrics * + * The name of families created will be prefixed by the class name given in the constructor. + * * @param metrics Optional metrics object to compute counters and histograms * @param responseDelaySecondsHistogramBucketBoundaries Optional bucket boundaries for response delay seconds histogram * @param messageSizeBytesHistogramBucketBoundaries Optional bucket boundaries for message size bytes histogram diff --git a/src/Http2Client.cpp b/src/Http2Client.cpp index 1b13dac..8badd44 100644 --- a/src/Http2Client.cpp +++ b/src/Http2Client.cpp @@ -73,24 +73,26 @@ void Http2Client::enableMetrics(ert::metrics::Metrics *metrics, if (metrics_) { - observed_requests_sents_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_resquests_sents_total"), std::string("Http2 total requests sents observed in ") + name_)); - observed_requests_unsents_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_resquests_unsent_total"), std::string("Http2 total requests unsents observed in ") + name_)); - observed_responses_received_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_responses_received_total"), std::string("Http2 total responses received observed in ") + name_)); - observed_responses_timedout_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_responses_timedout_total"), std::string("Http2 total responses timed-out observed in ") + name_)); - - responses_delay_seconds_gauge_family_ptr_ = &(metrics_->addGaugeFamily(name_ + std::string("_responses_delay_seconds_gauge"), std::string("Http2 message responses delay gauge (seconds) in ") + name_)); - responses_delay_seconds_gauge_ = &(responses_delay_seconds_gauge_family_ptr_->Add({})); - sent_messages_size_bytes_gauge_family_ptr_ = &(metrics_->addGaugeFamily(name_ + std::string("_sent_messages_size_bytes_gauge"), std::string("Http2 sent messages sizes gauge (bytes) in ") + name_)); - sent_messages_size_bytes_gauge_ = &(sent_messages_size_bytes_gauge_family_ptr_->Add({})); - received_messages_size_bytes_gauge_family_ptr_ = &(metrics_->addGaugeFamily(name_ + std::string("_received_messages_size_bytes_gauge"), std::string("Http2 received messages sizes gauge (bytes) in ") + name_)); - received_messages_size_bytes_gauge_ = &(received_messages_size_bytes_gauge_family_ptr_->Add({})); - - responses_delay_seconds_histogram_family_ptr_ = &(metrics_->addHistogramFamily(name_ + std::string("_responses_delay_seconds_histogram"), std::string("Http2 message responses delay histogram (seconds) in ") + name_)); - responses_delay_seconds_histogram_ = &(responses_delay_seconds_histogram_family_ptr_->Add({}, responseDelaySecondsHistogramBucketBoundaries)); - sent_messages_size_bytes_histogram_family_ptr_ = &(metrics_->addHistogramFamily(name_ + std::string("_sent_messages_size_bytes_histogram"), std::string("Http2 sent messages sizes histogram (bytes) in ") + name_)); - sent_messages_size_bytes_histogram_ = &(sent_messages_size_bytes_histogram_family_ptr_->Add({}, messageSizeBytesHistogramBucketBoundaries)); - received_messages_size_bytes_histogram_family_ptr_ = &(metrics_->addHistogramFamily(name_ + std::string("_received_messages_size_bytes_histogram"), std::string("Http2 received messages sizes histogram (bytes) in ") + name_)); - received_messages_size_bytes_histogram_ = &(received_messages_size_bytes_histogram_family_ptr_->Add({}, messageSizeBytesHistogramBucketBoundaries)); + ert::metrics::labels_t familyLabels = {{"source", name_}}; + + observed_requests_sents_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_resquests_sents_counter"), std::string("Requests sents observed counter in ") + name_, familyLabels)); + observed_requests_unsents_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_resquests_unsent_counter"), std::string("Requests unsents observed counter in ") + name_, familyLabels)); + observed_responses_received_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_responses_received_counter"), std::string("Responses received observed counter in ") + name_, familyLabels)); + 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)); + + 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 + + responses_delay_seconds_histogram_family_ptr_ = &(metrics_->addHistogramFamily(name_ + std::string("_responses_delay_seconds_histogram"), std::string("Message responses delay histogram (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_histogram"), std::string("Sent messages sizes histogram (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_histogram"), std::string("Received messages sizes histogram (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 } } diff --git a/src/Http2Server.cpp b/src/Http2Server.cpp index ebd6c4c..5d74200 100644 --- a/src/Http2Server.cpp +++ b/src/Http2Server.cpp @@ -87,23 +87,25 @@ void Http2Server::enableMetrics(ert::metrics::Metrics *metrics, metrics_ = metrics; if (metrics_) { - observed_requests_accepted_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_resquests_accepted_total"), std::string("Http2 total requests accepted observed in ") + name_)); - observed_requests_errored_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_resquests_errored_total"), std::string("Http2 total requests errored observed in ") + name_)); - observed_responses_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_responses_total"), std::string("Http2 total responses observed in ") + name_)); - - responses_delay_seconds_gauge_family_ptr_ = &(metrics_->addGaugeFamily(name_ + std::string("_responses_delay_seconds_gauge"), std::string("Http2 message responses delay gauge (seconds) in ") + name_)); - responses_delay_seconds_gauge_ = &(responses_delay_seconds_gauge_family_ptr_->Add({})); - received_messages_size_bytes_gauge_family_ptr_ = &(metrics_->addGaugeFamily(name_ + std::string("_received_messages_size_bytes_gauge"), std::string("Http2 received messages sizes gauge (bytes) in ") + name_)); - received_messages_size_bytes_gauge_ = &(received_messages_size_bytes_gauge_family_ptr_->Add({})); - sent_messages_size_bytes_gauge_family_ptr_ = &(metrics_->addGaugeFamily(name_ + std::string("_sent_messages_size_bytes_gauge"), std::string("Http2 sent messages sizes gauge (bytes) in ") + name_)); - sent_messages_size_bytes_gauge_ = &(sent_messages_size_bytes_gauge_family_ptr_->Add({})); - - responses_delay_seconds_histogram_family_ptr_ = &(metrics_->addHistogramFamily(name_ + std::string("_responses_delay_seconds_histogram"), std::string("Http2 message responses delay histogram (seconds) in ") + name_)); - responses_delay_seconds_histogram_ = &(responses_delay_seconds_histogram_family_ptr_->Add({}, responseDelaySecondsHistogramBucketBoundaries)); - received_messages_size_bytes_histogram_family_ptr_ = &(metrics_->addHistogramFamily(name_ + std::string("_received_messages_size_bytes_histogram"), std::string("Http2 received messages sizes histogram (bytes) in ") + name_)); - received_messages_size_bytes_histogram_ = &(received_messages_size_bytes_histogram_family_ptr_->Add({}, messageSizeBytesHistogramBucketBoundaries)); - sent_messages_size_bytes_histogram_family_ptr_ = &(metrics_->addHistogramFamily(name_ + std::string("_sent_messages_size_bytes_histogram"), std::string("Http2 sent messages sizes histogram (bytes) in ") + name_)); - sent_messages_size_bytes_histogram_ = &(sent_messages_size_bytes_histogram_family_ptr_->Add({}, messageSizeBytesHistogramBucketBoundaries)); + ert::metrics::labels_t familyLabels = {{"source", name_}}; + + observed_requests_accepted_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_resquests_accepted_counter"), std::string("Requests accepted observed counter in ") + name_, familyLabels)); + observed_requests_errored_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_resquests_errored_counter"), std::string("Requests errored observed counter in ") + name_, familyLabels)); + observed_responses_counter_family_ptr_ = &(metrics_->addCounterFamily(name_ + std::string("_observed_responses_counter"), std::string("Responses observed counter in ") + name_, familyLabels)); + + 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 + + responses_delay_seconds_histogram_family_ptr_ = &(metrics_->addHistogramFamily(name_ + std::string("_responses_delay_seconds_histogram"), std::string("Message responses delay histogram (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_histogram"), std::string("Received messages sizes histogram (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_histogram"), std::string("Sent messages sizes histogram (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 } }