diff --git a/core/CMakeLists.txt b/core/CMakeLists.txt index 180e3f1e3..b1db497e1 100644 --- a/core/CMakeLists.txt +++ b/core/CMakeLists.txt @@ -20,7 +20,7 @@ add_library(${PROJECT_NAME}::core ALIAS core) target_compile_features(core PUBLIC - cxx_std_11 + cxx_std_17 ) target_link_libraries(core diff --git a/core/include/prometheus/detail/ckms_quantiles.h b/core/include/prometheus/detail/ckms_quantiles.h index 771e816f1..a3b6617fd 100644 --- a/core/include/prometheus/detail/ckms_quantiles.h +++ b/core/include/prometheus/detail/ckms_quantiles.h @@ -9,8 +9,7 @@ // IWYU pragma: private, include "prometheus/summary.h" -namespace prometheus { -namespace detail { +namespace prometheus::detail { class PROMETHEUS_CPP_CORE_EXPORT CKMSQuantiles { public: @@ -53,5 +52,4 @@ class PROMETHEUS_CPP_CORE_EXPORT CKMSQuantiles { std::size_t buffer_count_; }; -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail diff --git a/core/include/prometheus/detail/future_std.h b/core/include/prometheus/detail/future_std.h deleted file mode 100644 index 55956aa0a..000000000 --- a/core/include/prometheus/detail/future_std.h +++ /dev/null @@ -1,16 +0,0 @@ -#pragma once - -#include -#include - -namespace prometheus { -namespace detail { - -// Remove as soon C++14 can be used. -template -std::unique_ptr make_unique(Args&&... args) { - return std::unique_ptr(new T(std::forward(args)...)); -} - -} // namespace detail -} // namespace prometheus diff --git a/core/include/prometheus/detail/time_window_quantiles.h b/core/include/prometheus/detail/time_window_quantiles.h index 498baed25..e436993ab 100644 --- a/core/include/prometheus/detail/time_window_quantiles.h +++ b/core/include/prometheus/detail/time_window_quantiles.h @@ -9,8 +9,7 @@ // IWYU pragma: private, include "prometheus/summary.h" -namespace prometheus { -namespace detail { +namespace prometheus::detail { class PROMETHEUS_CPP_CORE_EXPORT TimeWindowQuantiles { using Clock = std::chrono::steady_clock; @@ -33,5 +32,4 @@ class PROMETHEUS_CPP_CORE_EXPORT TimeWindowQuantiles { const Clock::duration rotation_interval_; }; -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail diff --git a/core/include/prometheus/detail/utils.h b/core/include/prometheus/detail/utils.h index 5cea6bdae..abf76137a 100644 --- a/core/include/prometheus/detail/utils.h +++ b/core/include/prometheus/detail/utils.h @@ -5,8 +5,7 @@ #include "prometheus/detail/core_export.h" #include "prometheus/labels.h" -namespace prometheus { -namespace detail { +namespace prometheus::detail { /// \brief Label hasher for use in STL containers. struct PROMETHEUS_CPP_CORE_EXPORT LabelHasher { @@ -18,5 +17,4 @@ struct PROMETHEUS_CPP_CORE_EXPORT LabelHasher { std::size_t operator()(const Labels& labels) const; }; -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail diff --git a/core/include/prometheus/family.h b/core/include/prometheus/family.h index 355596083..b480fc453 100644 --- a/core/include/prometheus/family.h +++ b/core/include/prometheus/family.h @@ -9,7 +9,6 @@ #include "prometheus/client_metric.h" #include "prometheus/collectable.h" #include "prometheus/detail/core_export.h" -#include "prometheus/detail/future_std.h" #include "prometheus/detail/utils.h" #include "prometheus/labels.h" #include "prometheus/metric_family.h" @@ -111,7 +110,7 @@ class PROMETHEUS_CPP_CORE_EXPORT Family : public Collectable { /// \throw std::invalid_argument on invalid label names. template T& Add(const Labels& labels, Args&&... args) { - return Add(labels, detail::make_unique(args...)); + return Add(labels, std::make_unique(args...)); } /// \brief Remove the given dimensional data. diff --git a/core/src/detail/ckms_quantiles.cc b/core/src/detail/ckms_quantiles.cc index 7ab6f1f21..395b6ff39 100644 --- a/core/src/detail/ckms_quantiles.cc +++ b/core/src/detail/ckms_quantiles.cc @@ -5,8 +5,7 @@ #include #include -namespace prometheus { -namespace detail { +namespace prometheus::detail { CKMSQuantiles::Quantile::Quantile(double quantile, double error) : quantile(quantile), @@ -149,5 +148,4 @@ void CKMSQuantiles::compress() { } } -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail diff --git a/core/src/detail/hash.h b/core/src/detail/hash.h index 0b465fff1..50c3ab4c8 100644 --- a/core/src/detail/hash.h +++ b/core/src/detail/hash.h @@ -3,9 +3,7 @@ #include #include -namespace prometheus { - -namespace detail { +namespace prometheus::detail { /// \brief Combine a hash value with nothing. /// It's the boundary condition of this serial functions. @@ -34,6 +32,4 @@ inline void hash_combine(std::size_t* seed, const T& value, hash_combine(seed, args...); } -} // namespace detail - -} // namespace prometheus +} // namespace prometheus::detail diff --git a/core/src/detail/time_window_quantiles.cc b/core/src/detail/time_window_quantiles.cc index 57f1e1c67..2a931906b 100644 --- a/core/src/detail/time_window_quantiles.cc +++ b/core/src/detail/time_window_quantiles.cc @@ -3,8 +3,7 @@ #include #include -namespace prometheus { -namespace detail { +namespace prometheus::detail { TimeWindowQuantiles::TimeWindowQuantiles( const std::vector& quantiles, @@ -42,5 +41,4 @@ CKMSQuantiles& TimeWindowQuantiles::rotate() const { return ckms_quantiles_[current_bucket_]; } -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail diff --git a/core/src/detail/utils.cc b/core/src/detail/utils.cc index bf67d8c29..4684bb1a4 100644 --- a/core/src/detail/utils.cc +++ b/core/src/detail/utils.cc @@ -6,9 +6,7 @@ #include "hash.h" -namespace prometheus { - -namespace detail { +namespace prometheus::detail { std::size_t LabelHasher::operator()(const Labels& labels) const { std::size_t seed = 0; @@ -19,6 +17,4 @@ std::size_t LabelHasher::operator()(const Labels& labels) const { return seed; } -} // namespace detail - -} // namespace prometheus +} // namespace prometheus::detail diff --git a/core/src/family.cc b/core/src/family.cc index 8827fa477..6a9945165 100644 --- a/core/src/family.cc +++ b/core/src/family.cc @@ -22,8 +22,7 @@ Family::Family(const std::string& name, const std::string& help, if (!CheckMetricName(name_)) { throw std::invalid_argument("Invalid metric name"); } - for (auto& label_pair : constant_labels_) { - auto& label_name = label_pair.first; + for (auto& [label_name, label_value] : constant_labels_) { if (!CheckLabelName(label_name, T::metric_type)) { throw std::invalid_argument("Invalid label name"); } @@ -34,25 +33,23 @@ template T& Family::Add(const Labels& labels, std::unique_ptr object) { std::lock_guard lock{mutex_}; - auto insert_result = - metrics_.insert(std::make_pair(labels, std::move(object))); + auto [it, inserted] = metrics_.emplace(labels, std::move(object)); - if (insert_result.second) { + if (inserted) { // insertion took place, retroactively check for unlikely issues - for (auto& label_pair : labels) { - const auto& label_name = label_pair.first; + for (auto& [label_name, label_value] : labels) { if (!CheckLabelName(label_name, T::metric_type)) { - metrics_.erase(insert_result.first); + metrics_.erase(it); throw std::invalid_argument("Invalid label name"); } if (constant_labels_.count(label_name)) { - metrics_.erase(insert_result.first); + metrics_.erase(it); throw std::invalid_argument("Duplicate label name"); } } } - auto& stored_object = insert_result.first->second; + auto& stored_object = it->second; assert(stored_object); return *stored_object; } @@ -61,11 +58,11 @@ template void Family::Remove(T* metric) { std::lock_guard lock{mutex_}; - for (auto it = metrics_.begin(); it != metrics_.end(); ++it) { - if (it->second.get() == metric) { - metrics_.erase(it); - break; - } + const auto it = std::find_if( + metrics_.begin(), metrics_.end(), + [metric](const auto& entry) { return entry.second.get() == metric; }); + if (it != metrics_.end()) { + metrics_.erase(it); } } @@ -98,8 +95,8 @@ std::vector Family::Collect() const { family.help = help_; family.type = T::metric_type; family.metric.reserve(metrics_.size()); - for (const auto& m : metrics_) { - family.metric.push_back(std::move(CollectMetric(m.first, m.second.get()))); + for (const auto& [metric_labels, metric] : metrics_) { + family.metric.push_back(CollectMetric(metric_labels, metric.get())); } return {family}; } diff --git a/core/src/registry.cc b/core/src/registry.cc index 9e58b0bc7..11b9b5b0a 100644 --- a/core/src/registry.cc +++ b/core/src/registry.cc @@ -6,7 +6,6 @@ #include #include "prometheus/counter.h" -#include "prometheus/detail/future_std.h" #include "prometheus/gauge.h" #include "prometheus/histogram.h" #include "prometheus/info.h" @@ -135,7 +134,7 @@ Family& Registry::Add(const std::string& name, const std::string& help, } } - auto family = detail::make_unique>(name, help, labels); + auto family = std::make_unique>(name, help, labels); auto& ref = *family; families.push_back(std::move(family)); return ref; diff --git a/core/tests/family_test.cc b/core/tests/family_test.cc index a263d4df7..29fcace47 100644 --- a/core/tests/family_test.cc +++ b/core/tests/family_test.cc @@ -7,7 +7,6 @@ #include "prometheus/client_metric.h" #include "prometheus/counter.h" -#include "prometheus/detail/future_std.h" #include "prometheus/histogram.h" #include "prometheus/labels.h" #include "prometheus/summary.h" @@ -82,14 +81,14 @@ TEST(FamilyTest, add_twice) { TEST(FamilyTest, throw_on_invalid_metric_name) { auto create_family_with_invalid_name = []() { - return detail::make_unique>("", "empty name", Labels{}); + return std::make_unique>("", "empty name", Labels{}); }; EXPECT_ANY_THROW(create_family_with_invalid_name()); } TEST(FamilyTest, throw_on_invalid_constant_label_name) { auto create_family_with_invalid_labels = []() { - return detail::make_unique>( + return std::make_unique>( "total_requests", "Counts all requests", Labels{{"__inavlid", "counter1"}}); }; @@ -120,7 +119,7 @@ TEST(FamilyTest, query_family_if_metric_already_exists) { TEST(FamilyTest, reject_histogram_with_constant_le_label) { auto labels = Labels{{"le", "test"}}; EXPECT_ANY_THROW( - detail::make_unique>("name", "help", labels)); + std::make_unique>("name", "help", labels)); } TEST(FamilyTest, reject_histogram_with_le_label) { @@ -132,7 +131,7 @@ TEST(FamilyTest, reject_histogram_with_le_label) { TEST(FamilyTest, reject_summary_with_constant_quantile_label) { auto labels = Labels{{"quantile", "test"}}; EXPECT_ANY_THROW( - detail::make_unique>("name", "help", labels)); + std::make_unique>("name", "help", labels)); } TEST(FamilyTest, reject_summary_with_quantile_label) { diff --git a/core/tests/serializer_test.cc b/core/tests/serializer_test.cc index ebf8fd73c..60908ac64 100644 --- a/core/tests/serializer_test.cc +++ b/core/tests/serializer_test.cc @@ -7,7 +7,6 @@ #include #include "prometheus/counter.h" -#include "prometheus/detail/future_std.h" #include "prometheus/family.h" #include "prometheus/metric_family.h" #include "prometheus/text_serializer.h" @@ -40,8 +39,7 @@ TEST_F(SerializerTest, shouldSerializeLocaleIndependent) { // ignore missing locale and skip test if setup fails try { - localeWithCommaDecimalSeparator = - detail::make_unique("de_DE.UTF-8"); + localeWithCommaDecimalSeparator = std::make_unique("de_DE.UTF-8"); } catch (std::runtime_error&) { GTEST_SKIP(); } diff --git a/pull/CMakeLists.txt b/pull/CMakeLists.txt index 162987de3..a35446d0d 100644 --- a/pull/CMakeLists.txt +++ b/pull/CMakeLists.txt @@ -37,7 +37,7 @@ add_library(${PROJECT_NAME}::pull ALIAS pull) target_compile_features(pull PUBLIC - cxx_std_11 + cxx_std_17 ) target_link_libraries(pull diff --git a/pull/src/endpoint.cc b/pull/src/endpoint.cc index bd98cef4a..3ba8cc56d 100644 --- a/pull/src/endpoint.cc +++ b/pull/src/endpoint.cc @@ -4,17 +4,15 @@ #include "basic_auth.h" #include "handler.h" -#include "prometheus/detail/future_std.h" -namespace prometheus { -namespace detail { +namespace prometheus::detail { Endpoint::Endpoint(CivetServer& server, std::string uri) : server_(server), uri_(std::move(uri)), endpoint_registry_(std::make_shared()), metrics_handler_( - detail::make_unique(*endpoint_registry_)) { + std::make_unique(*endpoint_registry_)) { RegisterCollectable(endpoint_registry_); server_.addHandler(uri_, metrics_handler_.get()); } @@ -37,7 +35,7 @@ void Endpoint::RegisterAuth( // split creating, assigning, and storing to avoid a race-condition when // being called the second time and the handler is replaced auto new_handler = - detail::make_unique(std::move(authCB), realm); + std::make_unique(std::move(authCB), realm); server_.addAuthHandler(uri_, new_handler.get()); auth_handler_ = std::move(new_handler); } @@ -49,5 +47,4 @@ void Endpoint::RemoveCollectable( const std::string& Endpoint::GetURI() const { return uri_; } -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail diff --git a/pull/src/endpoint.h b/pull/src/endpoint.h index 81bb48f9f..6e582fb5b 100644 --- a/pull/src/endpoint.h +++ b/pull/src/endpoint.h @@ -9,8 +9,7 @@ #include "prometheus/collectable.h" #include "prometheus/registry.h" -namespace prometheus { -namespace detail { +namespace prometheus::detail { class MetricsHandler; class Endpoint { @@ -40,5 +39,4 @@ class Endpoint { std::unique_ptr auth_handler_; }; -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail diff --git a/pull/src/exposer.cc b/pull/src/exposer.cc index f7dd30d97..4b51b98e0 100644 --- a/pull/src/exposer.cc +++ b/pull/src/exposer.cc @@ -7,7 +7,6 @@ #include "CivetServer.h" #include "endpoint.h" -#include "prometheus/detail/future_std.h" namespace prometheus { @@ -67,7 +66,7 @@ detail::Endpoint& Exposer::GetEndpointForUri(const std::string& uri) { return *it->get(); } - endpoints_.emplace_back(detail::make_unique(*server_, uri)); + endpoints_.emplace_back(std::make_unique(*server_, uri)); return *endpoints_.back().get(); } diff --git a/pull/src/handler.cc b/pull/src/handler.cc index 784fd5e72..27b9af912 100644 --- a/pull/src/handler.cc +++ b/pull/src/handler.cc @@ -24,8 +24,7 @@ #error "Civetweb version 1.14 or higher required" #endif -namespace prometheus { -namespace detail { +namespace prometheus::detail { MetricsHandler::MetricsHandler(Registry& registry) : bytes_transferred_family_( @@ -178,5 +177,4 @@ void MetricsHandler::CleanupStalePointers( }), std::end(collectables)); } -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail diff --git a/pull/src/handler.h b/pull/src/handler.h index 10c90f9fb..f829cac4b 100644 --- a/pull/src/handler.h +++ b/pull/src/handler.h @@ -11,8 +11,7 @@ #include "prometheus/registry.h" #include "prometheus/summary.h" -namespace prometheus { -namespace detail { +namespace prometheus::detail { class MetricsHandler : public CivetHandler { public: explicit MetricsHandler(Registry& registry); @@ -35,5 +34,4 @@ class MetricsHandler : public CivetHandler { Family& request_latencies_family_; Summary& request_latencies_; }; -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail diff --git a/pull/src/metrics_collector.cc b/pull/src/metrics_collector.cc index 0372d6936..7dc979a6e 100644 --- a/pull/src/metrics_collector.cc +++ b/pull/src/metrics_collector.cc @@ -4,8 +4,7 @@ #include "prometheus/collectable.h" -namespace prometheus { -namespace detail { +namespace prometheus::detail { std::vector CollectMetrics( const std::vector>& collectables) { @@ -26,5 +25,4 @@ std::vector CollectMetrics( return collected_metrics; } -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail diff --git a/pull/tests/integration/integration_test.cc b/pull/tests/integration/integration_test.cc index c203ed535..c3a949223 100644 --- a/pull/tests/integration/integration_test.cc +++ b/pull/tests/integration/integration_test.cc @@ -10,7 +10,6 @@ #include #include "prometheus/counter.h" -#include "prometheus/detail/future_std.h" #include "prometheus/exposer.h" #include "prometheus/family.h" #include "prometheus/registry.h" @@ -23,7 +22,7 @@ using namespace testing; class IntegrationTest : public testing::Test { public: void SetUp() override { - exposer_ = detail::make_unique("127.0.0.1:0"); + exposer_ = std::make_unique("127.0.0.1:0"); auto ports = exposer_->GetListeningPorts(); base_url_ = std::string("http://127.0.0.1:") + std::to_string(ports.at(0)); } diff --git a/push/CMakeLists.txt b/push/CMakeLists.txt index 19f22e1f0..7790ecd6d 100644 --- a/push/CMakeLists.txt +++ b/push/CMakeLists.txt @@ -14,7 +14,7 @@ add_library(${PROJECT_NAME}::push ALIAS push) target_compile_features(push PUBLIC - cxx_std_11 + cxx_std_17 ) target_link_libraries(push diff --git a/push/include/prometheus/detail/http_method.h b/push/include/prometheus/detail/http_method.h index 21eb18206..fa44f78a3 100644 --- a/push/include/prometheus/detail/http_method.h +++ b/push/include/prometheus/detail/http_method.h @@ -1,12 +1,10 @@ #pragma once -namespace prometheus { -namespace detail { +namespace prometheus::detail { enum class HttpMethod { Post, Put, Delete, }; -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail diff --git a/push/src/detail/curl_wrapper.cc b/push/src/detail/curl_wrapper.cc index 997c3745c..1eedde1f2 100644 --- a/push/src/detail/curl_wrapper.cc +++ b/push/src/detail/curl_wrapper.cc @@ -2,8 +2,7 @@ #include -namespace prometheus { -namespace detail { +namespace prometheus::detail { static const char CONTENT_TYPE[] = "Content-Type: text/plain; version=0.0.4; charset=utf-8"; @@ -98,5 +97,4 @@ bool CurlWrapper::addHttpHeader(const std::string& header) { return true; } -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail diff --git a/push/src/detail/curl_wrapper.h b/push/src/detail/curl_wrapper.h index 69c6d6a18..a100e922b 100644 --- a/push/src/detail/curl_wrapper.h +++ b/push/src/detail/curl_wrapper.h @@ -6,8 +6,7 @@ #include "prometheus/detail/http_method.h" -namespace prometheus { -namespace detail { +namespace prometheus::detail { class CurlWrapper { public: @@ -31,5 +30,4 @@ class CurlWrapper { std::function presetupCurl_; }; -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail diff --git a/push/src/detail/label_encoder.cc b/push/src/detail/label_encoder.cc index 75a6fc112..f3427f487 100644 --- a/push/src/detail/label_encoder.cc +++ b/push/src/detail/label_encoder.cc @@ -5,8 +5,7 @@ #include "prometheus/detail/base64.h" -namespace prometheus { -namespace detail { +namespace prometheus::detail { namespace { // Does this character need encoding like in RFC 3986 section 2.3? @@ -38,5 +37,4 @@ void encodeLabel(std::ostream& os, const Label& label) { os << "/" << label.first << "/" << label.second; } } -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail diff --git a/push/src/detail/label_encoder.h b/push/src/detail/label_encoder.h index 2713f7eeb..d1f0cdf31 100644 --- a/push/src/detail/label_encoder.h +++ b/push/src/detail/label_encoder.h @@ -5,10 +5,8 @@ #include "prometheus/labels.h" #include "prometheus/detail/push_export.h" -namespace prometheus { -namespace detail { +namespace prometheus::detail { PROMETHEUS_CPP_PUSH_EXPORT void encodeLabel(std::ostream& os, const Label& label); -} -} // namespace prometheus \ No newline at end of file +} // namespace prometheus::detail \ No newline at end of file diff --git a/push/src/gateway.cc b/push/src/gateway.cc index 850441369..a14424d85 100644 --- a/push/src/gateway.cc +++ b/push/src/gateway.cc @@ -10,7 +10,6 @@ #include "detail/curl_wrapper.h" #include "detail/label_encoder.h" -#include "prometheus/detail/future_std.h" #include "prometheus/metric_family.h" // IWYU pragma: keep #include "prometheus/text_serializer.h" @@ -65,7 +64,7 @@ Gateway::Gateway(const std::string& host, const std::string& port, Gateway::Gateway(const std::string& url, std::function presetupCurl, const std::string& jobname, const Labels& labels) { - curlWrapper_ = detail::make_unique(presetupCurl); + curlWrapper_ = std::make_unique(presetupCurl); std::stringstream jobUriStream; jobUriStream << url; diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt index 971375e46..fa5978e46 100644 --- a/util/CMakeLists.txt +++ b/util/CMakeLists.txt @@ -4,7 +4,7 @@ add_library(${PROJECT_NAME}::util ALIAS util) target_compile_features(util INTERFACE - cxx_std_11 + cxx_std_17 ) target_include_directories(util diff --git a/util/include/prometheus/detail/base64.h b/util/include/prometheus/detail/base64.h index ebd711038..c5aa11330 100644 --- a/util/include/prometheus/detail/base64.h +++ b/util/include/prometheus/detail/base64.h @@ -6,8 +6,7 @@ #include #include -namespace prometheus { -namespace detail { +namespace prometheus::detail { /* Copyright (C) 2019-2020 by Martin Vorbrodt @@ -136,5 +135,4 @@ inline std::string base64_decode(const std::string& input) { return decoded; } -} // namespace detail -} // namespace prometheus +} // namespace prometheus::detail