From 0f2fe70a2d44855751e8a967a04de31f859c2623 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Mon, 27 Mar 2017 17:16:52 +0200 Subject: [PATCH 01/27] build: add drake support --- drakefile | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 drakefile diff --git a/drakefile b/drakefile new file mode 100644 index 00000000..86dc3e76 --- /dev/null +++ b/drakefile @@ -0,0 +1,57 @@ +import drake +import drake.cmake +import drake.cxx +import os + +# The paths to libprometheus-cpp headers. +prometheus_include_path = [] +# The path to libprometheus-cpp library. +prometheus_lib = None +sources = None +cxx_config = None + +def configure( + cxx_toolkit, + in_cxx_config, + protoc, + protobuf_include, + protobuf_lib, + cmake_env = {} +): + global prometheus_include_path, prometheus_lib, sources + prometheus_include_path = [ + # libprometheus-cpp's header. + drake.path_root() / drake.path_source('include'), + # prometheus_client_model, a submodule, that contains the protobuf + # support: metrics.pb.h. + drake.path_root() / drake.path_build('lib/cpp'), + ] + prometheus_lib = drake.cxx.StaticLib('lib/libprometheus-cpp.a') + srcs = drake.nodes( + 'CMakeLists.txt', + ) + dsts = [prometheus_lib] + targets = [ + 'prometheus-cpp', + ] + cmake_env.update({ + 'Protobuf_INCLUDE_DIR': protobuf_include, + 'Protobuf_LIBRARIES': protobuf_lib.path(absolute = True), + 'Protobuf_PROTOC_EXECUTABLE': protoc.path(absolute = True), + }) + if drake.path_source().absolute(): + cmake_source = drake.path_source() / drake.Drake.current.prefix + else: + cmake_source = '../../../../prometheus/prometheus-cpp' + print("SRC:", cmake_source) + cmake = drake.cmake.CMakeBuilder(cxx_toolkit, srcs, dsts, cmake_env, targets = targets, + path_to_cmake_source = cmake_source) + global cxx_config + cxx_config = drake.cxx.Config(in_cxx_config) + cxx_config.add_system_include_path(protobuf_include) + + + +# Local Variables: +# mode: python +# End: From fdf9ae982b329de500b0da2f21c7c0123b4242d9 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 30 Mar 2017 13:10:49 +0200 Subject: [PATCH 02/27] drake: build a shared library The BF is unhappy about the static library. Well, rather about the position dependent code. Link lib/libinfinit.so /usr/lib/gcc/x86_64-alpine-linux-musl/6.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: lib/libprometheus-cpp.a(exposer.cc.o): relocation R_X86_64_PC32 against symbol `_ZNSaINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEC1Ev' can not be used when making a shared object; recompile with -fPIC /usr/lib/gcc/x86_64-alpine-linux-musl/6.2.1/../../../../x86_64-alpine-linux-musl/bin/ld: final link failed: Bad value collect2: error: ld returned 1 exit status drake: *** dynamic linkage of lib/libinfinit.so failed --- drakefile | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/drakefile b/drakefile index 86dc3e76..80cddfdf 100644 --- a/drakefile +++ b/drakefile @@ -3,20 +3,29 @@ import drake.cmake import drake.cxx import os -# The paths to libprometheus-cpp headers. +# The paths to the libprometheus-cpp headers. prometheus_include_path = [] -# The path to libprometheus-cpp library. +# The path to the libprometheus-cpp library. prometheus_lib = None sources = None cxx_config = None +shlibexts = { + drake.os.android: 'so', + drake.os.ios: 'dylib', + drake.os.ios_simulator: 'dylib', + drake.os.linux: 'so', + drake.os.macos: 'dylib', + drake.os.windows: 'dll', +} + def configure( cxx_toolkit, in_cxx_config, protoc, protobuf_include, protobuf_lib, - cmake_env = {} + cmake_vars = {} ): global prometheus_include_path, prometheus_lib, sources prometheus_include_path = [ @@ -26,15 +35,18 @@ def configure( # support: metrics.pb.h. drake.path_root() / drake.path_build('lib/cpp'), ] - prometheus_lib = drake.cxx.StaticLib('lib/libprometheus-cpp.a') + prometheus_lib = drake.cxx.DynLib('lib/libprometheus-cpp.{}' + .format(shlibexts[cxx_toolkit.os])) srcs = drake.nodes( 'CMakeLists.txt', ) dsts = [prometheus_lib] + # Makefile target to run. targets = [ 'prometheus-cpp', ] - cmake_env.update({ + cmake_vars.update({ + 'BUILD_SHARED_LIBS': 'ON', 'Protobuf_INCLUDE_DIR': protobuf_include, 'Protobuf_LIBRARIES': protobuf_lib.path(absolute = True), 'Protobuf_PROTOC_EXECUTABLE': protoc.path(absolute = True), @@ -44,7 +56,7 @@ def configure( else: cmake_source = '../../../../prometheus/prometheus-cpp' print("SRC:", cmake_source) - cmake = drake.cmake.CMakeBuilder(cxx_toolkit, srcs, dsts, cmake_env, targets = targets, + cmake = drake.cmake.CMakeBuilder(cxx_toolkit, srcs, dsts, cmake_vars, targets = targets, path_to_cmake_source = cmake_source) global cxx_config cxx_config = drake.cxx.Config(in_cxx_config) From daf9a864da6aea39ae10917f42d3817b3e167d12 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 30 Mar 2017 22:22:00 +0200 Subject: [PATCH 03/27] family: allow several Add, and free only when Removed enough --- include/prometheus/family.h | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/include/prometheus/family.h b/include/prometheus/family.h index 4da04699..4f0dd6f4 100644 --- a/include/prometheus/family.h +++ b/include/prometheus/family.h @@ -33,7 +33,8 @@ class Family : public Collectable { std::vector Collect() override; private: - std::unordered_map> metrics_; + /// hash -> (unique_ptr, refcount). + std::unordered_map, int>> metrics_; std::unordered_map> labels_; std::unordered_map labels_reverse_lookup_; @@ -59,11 +60,20 @@ T& Family::Add(const std::map& labels, Args&&... args) { std::lock_guard lock{mutex_}; auto hash = hash_labels(labels); - auto metric = new T(std::forward(args)...); + auto p = metrics_.emplace(hash, std::make_pair(nullptr, 0)); + auto& metric = p.first->second.first; + if (p.second) + { + // Insertion. + metric = std::make_unique(std::forward(args)...); + p.first->second.second = 1; + labels_.emplace(hash, labels); + labels_reverse_lookup_.emplace(metric.get(), hash); + } + else + // Update. + ++p.first->second.second; - metrics_.insert(std::make_pair(hash, std::unique_ptr{metric})); - labels_.insert({hash, labels}); - labels_reverse_lookup_.insert({metric, hash}); return *metric; } @@ -87,9 +97,14 @@ void Family::Remove(T* metric) { } auto hash = labels_reverse_lookup_.at(metric); - metrics_.erase(hash); - labels_.erase(hash); - labels_reverse_lookup_.erase(metric); + auto i = metrics_.find(hash); + assert(i != metrics_.end()); + if (--i->second.second == 0) + { + metrics_.erase(i); + labels_.erase(hash); + labels_reverse_lookup_.erase(metric); + } } template @@ -100,7 +115,7 @@ std::vector Family::Collect() { family.set_help(help_); family.set_type(T::metric_type); for (const auto& m : metrics_) { - *family.add_metric() = std::move(CollectMetric(m.first, m.second.get())); + *family.add_metric() = std::move(CollectMetric(m.first, m.second.first.get())); } return {family}; } From c496ce289d45ccc9e52a12001ec47b91a408589d Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Fri, 31 Mar 2017 07:11:26 +0200 Subject: [PATCH 04/27] family: add missing include --- include/prometheus/family.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/prometheus/family.h b/include/prometheus/family.h index 4f0dd6f4..3b929363 100644 --- a/include/prometheus/family.h +++ b/include/prometheus/family.h @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include From 802621befafa48a713e4fd02019aacdcf5b7be10 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Fri, 31 Mar 2017 09:26:31 +0200 Subject: [PATCH 05/27] family: don't use C++14 features --- include/prometheus/family.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/prometheus/family.h b/include/prometheus/family.h index 3b929363..5bf7e1ec 100644 --- a/include/prometheus/family.h +++ b/include/prometheus/family.h @@ -66,7 +66,7 @@ T& Family::Add(const std::map& labels, if (p.second) { // Insertion. - metric = std::make_unique(std::forward(args)...); + metric = std::unique_ptr(new T(std::forward(args)...)); p.first->second.second = 1; labels_.emplace(hash, labels); labels_reverse_lookup_.emplace(metric.get(), hash); From a2e3ab2ea9a24170c6d30d466dbb2a50f3c8460f Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Fri, 31 Mar 2017 13:15:23 +0200 Subject: [PATCH 06/27] drake: more extensive list of sources --- drakefile | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/drakefile b/drakefile index 80cddfdf..1ee3f4ac 100644 --- a/drakefile +++ b/drakefile @@ -39,7 +39,36 @@ def configure( .format(shlibexts[cxx_toolkit.os])) srcs = drake.nodes( 'CMakeLists.txt', + 'include/prometheus/collectable.h', + 'include/prometheus/counter.h', + 'include/prometheus/counter_builder.h', + 'include/prometheus/exposer.h', + 'include/prometheus/family.h', + 'include/prometheus/gauge.h', + 'include/prometheus/gauge_builder.h', + 'include/prometheus/histogram.h', + 'include/prometheus/histogram_builder.h', + 'include/prometheus/metric.h', + 'include/prometheus/registry.h', + 'lib/counter.cc', + 'lib/counter_builder.cc', + 'lib/exposer.cc', + 'lib/gauge.cc', + 'lib/gauge_builder.cc', + 'lib/handler.cc', + 'lib/handler.h', + 'lib/histogram.cc', + 'lib/histogram_builder.cc', + 'lib/json_serializer.cc', + 'lib/json_serializer.h', + 'lib/protobuf_delimited_serializer.cc', + 'lib/protobuf_delimited_serializer.h', + 'lib/registry.cc', + 'lib/serializer.h', + 'lib/text_serializer.cc', + 'lib/text_serializer.h', ) + dsts = [prometheus_lib] # Makefile target to run. targets = [ From e5b7903f81de3377582acd2d6476b3d8f077d9e3 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sun, 2 Apr 2017 14:13:07 +0200 Subject: [PATCH 07/27] drake: compile Civet with -fPIC --- drakefile | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/drakefile b/drakefile index 1ee3f4ac..9cf28197 100644 --- a/drakefile +++ b/drakefile @@ -79,6 +79,13 @@ def configure( 'Protobuf_INCLUDE_DIR': protobuf_include, 'Protobuf_LIBRARIES': protobuf_lib.path(absolute = True), 'Protobuf_PROTOC_EXECUTABLE': protoc.path(absolute = True), + # CivetWeb, a small HTTP in C and C++, is used an a "object" + # library (aka "convenience library" in Libtool parlance) in + # libprometheus-cpp. But compiling the latter as a shared library + # does not make the former PIC. So force -fPIC. + # https://cmake.org/pipermail/cmake/2012-June/050941.html + 'CMAKE_C_FLAGS': '-fPIC', + 'CMAKE_CXX_FLAGS': '-fPIC', }) if drake.path_source().absolute(): cmake_source = drake.path_source() / drake.Drake.current.prefix From 581aac4505be5a4070b552351e06f7ee0041f3bd Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Wed, 5 Apr 2017 17:16:10 +0200 Subject: [PATCH 08/27] drake: expose metrics.pb.h And fix the name of PROTOBUF_LIBRARY. --- drakefile | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/drakefile b/drakefile index 9cf28197..483b3c45 100644 --- a/drakefile +++ b/drakefile @@ -69,16 +69,19 @@ def configure( 'lib/text_serializer.h', ) - dsts = [prometheus_lib] + dsts = [prometheus_lib] + drake.nodes( + 'lib/cpp/metrics.pb.h' + ) + # Makefile target to run. targets = [ 'prometheus-cpp', ] cmake_vars.update({ 'BUILD_SHARED_LIBS': 'ON', - 'Protobuf_INCLUDE_DIR': protobuf_include, - 'Protobuf_LIBRARIES': protobuf_lib.path(absolute = True), - 'Protobuf_PROTOC_EXECUTABLE': protoc.path(absolute = True), + 'PROTOBUF_INCLUDE_DIR': protobuf_include, + 'PROTOBUF_LIBRARY': protobuf_lib.path(absolute = True), + 'PROTOBUF_PROTOC_EXECUTABLE': protoc.path(absolute = True), # CivetWeb, a small HTTP in C and C++, is used an a "object" # library (aka "convenience library" in Libtool parlance) in # libprometheus-cpp. But compiling the latter as a shared library From b07ba6b02534a553d796ec2ebaa1d824ae711c9d Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 13 Apr 2017 11:42:47 +0200 Subject: [PATCH 09/27] drake: we depend upon protoc --- drakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drakefile b/drakefile index 483b3c45..c76c9e5e 100644 --- a/drakefile +++ b/drakefile @@ -37,7 +37,7 @@ def configure( ] prometheus_lib = drake.cxx.DynLib('lib/libprometheus-cpp.{}' .format(shlibexts[cxx_toolkit.os])) - srcs = drake.nodes( + srcs = [protoc, protobuf_lib] + drake.nodes( 'CMakeLists.txt', 'include/prometheus/collectable.h', 'include/prometheus/counter.h', From b81742037ba29e376cd9c8cf56710c0784f4c3ab Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Tue, 25 Apr 2017 17:03:10 +0200 Subject: [PATCH 10/27] exposer::rebind Provide a means to change the exposition port at runtime. Civetweb does not seem to allow this at runtime, so let's just use another server. --- include/prometheus/exposer.h | 3 +++ lib/exposer.cc | 14 +++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/include/prometheus/exposer.h b/include/prometheus/exposer.h index 6d4a2b0b..fdda3db8 100644 --- a/include/prometheus/exposer.h +++ b/include/prometheus/exposer.h @@ -18,6 +18,9 @@ class MetricsHandler; class Exposer { public: Exposer(const std::string& bind_address); + /// Change the exposition port. + void + rebind(const std::string& bind_address); ~Exposer(); void RegisterCollectable(const std::weak_ptr& collectable); diff --git a/lib/exposer.cc b/lib/exposer.cc index 65303e49..9d5f05cb 100644 --- a/lib/exposer.cc +++ b/lib/exposer.cc @@ -1,4 +1,5 @@ #include +#include #include #include @@ -13,16 +14,23 @@ namespace prometheus { static const auto uri = std::string{"/metrics"}; Exposer::Exposer(const std::string& bind_address) - : server_(new CivetServer{{"listening_ports", bind_address.c_str()}}), - exposer_registry_(std::make_shared()), + : exposer_registry_(std::make_shared()), metrics_handler_( new detail::MetricsHandler{collectables_, *exposer_registry_}) { RegisterCollectable(exposer_registry_); - server_->addHandler(uri, metrics_handler_.get()); + rebind(bind_address); } Exposer::~Exposer() { server_->removeHandler(uri); } +void +Exposer::rebind(const std::string& bind_address) { + server_.reset(new CivetServer{ + {"listening_ports", bind_address.c_str()} + }); + server_->addHandler(uri, metrics_handler_.get()); +} + void Exposer::RegisterCollectable( const std::weak_ptr& collectable) { collectables_.push_back(collectable); From 0d5888f4dc5e51675c8598b79f7bbb729cb119c0 Mon Sep 17 00:00:00 2001 From: mefyl Date: Wed, 3 May 2017 17:39:23 +0200 Subject: [PATCH 11/27] drakefile: Remove debug log. --- drakefile | 1 - 1 file changed, 1 deletion(-) diff --git a/drakefile b/drakefile index c76c9e5e..f9fdf488 100644 --- a/drakefile +++ b/drakefile @@ -94,7 +94,6 @@ def configure( cmake_source = drake.path_source() / drake.Drake.current.prefix else: cmake_source = '../../../../prometheus/prometheus-cpp' - print("SRC:", cmake_source) cmake = drake.cmake.CMakeBuilder(cxx_toolkit, srcs, dsts, cmake_vars, targets = targets, path_to_cmake_source = cmake_source) global cxx_config From e1e80812425dbb93a833f66a447b6719f243e8b3 Mon Sep 17 00:00:00 2001 From: mefyl Date: Tue, 9 May 2017 12:52:52 +0200 Subject: [PATCH 12/27] drakefile: Cleanup. --- drakefile | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/drakefile b/drakefile index f9fdf488..de36217a 100644 --- a/drakefile +++ b/drakefile @@ -23,7 +23,7 @@ def configure( cxx_toolkit, in_cxx_config, protoc, - protobuf_include, + protobuf_config, protobuf_lib, cmake_vars = {} ): @@ -77,9 +77,20 @@ def configure( targets = [ 'prometheus-cpp', ] + + class PrometheusCMakeBuilder(drake.cmake.CMakeBuilder): + + def execute(self): + if not super().execute(): + return False + self.cmd( + 'Fix rpath for {}'.format(prometheus_lib), + cxx_toolkit.rpath_set_command(prometheus_lib, '.')) + return True + cmake_vars.update({ 'BUILD_SHARED_LIBS': 'ON', - 'PROTOBUF_INCLUDE_DIR': protobuf_include, + 'PROTOBUF_INCLUDE_DIR': protobuf_config.protobuf_include_dir, 'PROTOBUF_LIBRARY': protobuf_lib.path(absolute = True), 'PROTOBUF_PROTOC_EXECUTABLE': protoc.path(absolute = True), # CivetWeb, a small HTTP in C and C++, is used an a "object" @@ -94,11 +105,12 @@ def configure( cmake_source = drake.path_source() / drake.Drake.current.prefix else: cmake_source = '../../../../prometheus/prometheus-cpp' - cmake = drake.cmake.CMakeBuilder(cxx_toolkit, srcs, dsts, cmake_vars, targets = targets, - path_to_cmake_source = cmake_source) + cmake = PrometheusCMakeBuilder( + cxx_toolkit, srcs, dsts, cmake_vars, targets = targets, + path_to_cmake_source = cmake_source) global cxx_config cxx_config = drake.cxx.Config(in_cxx_config) - cxx_config.add_system_include_path(protobuf_include) + cxx_config += protobuf_config From 63b4631c5ea8f6baf7ba90221a64fb628bcd6974 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Wed, 10 May 2017 11:52:34 +0200 Subject: [PATCH 13/27] drake: let drake infer the lib extension itself Warning: requires that drake include the commit 19b62b62e66ae90b18ba44faedcd8146f88c91cb: "Node.path: void the cache when a node is finally from the build tree" --- drakefile | 12 +----------- 1 file changed, 1 insertion(+), 11 deletions(-) diff --git a/drakefile b/drakefile index de36217a..d1aa7ab6 100644 --- a/drakefile +++ b/drakefile @@ -10,15 +10,6 @@ prometheus_lib = None sources = None cxx_config = None -shlibexts = { - drake.os.android: 'so', - drake.os.ios: 'dylib', - drake.os.ios_simulator: 'dylib', - drake.os.linux: 'so', - drake.os.macos: 'dylib', - drake.os.windows: 'dll', -} - def configure( cxx_toolkit, in_cxx_config, @@ -35,8 +26,7 @@ def configure( # support: metrics.pb.h. drake.path_root() / drake.path_build('lib/cpp'), ] - prometheus_lib = drake.cxx.DynLib('lib/libprometheus-cpp.{}' - .format(shlibexts[cxx_toolkit.os])) + prometheus_lib = drake.cxx.DynLib('lib/prometheus-cpp', tk = cxx_toolkit) srcs = [protoc, protobuf_lib] + drake.nodes( 'CMakeLists.txt', 'include/prometheus/collectable.h', From d2665762269db637ff2281764ea91d620ee12d72 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Wed, 10 May 2017 14:17:40 +0200 Subject: [PATCH 14/27] drakefile: fix the lib id on macOS Needs drake fae984561b05447a645e412a5ffc68e9ea6ab4ec. --- drakefile | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/drakefile b/drakefile index d1aa7ab6..134a09d9 100644 --- a/drakefile +++ b/drakefile @@ -10,6 +10,15 @@ prometheus_lib = None sources = None cxx_config = None +class GRPCBuilder(drake.cmake.CMakeBuilder): + + def execute(self): + if super().execute(): + drake.cxx.set_lib_id(protobuf_lib.path(), self.toolkit) + return True + else: + return False + def configure( cxx_toolkit, in_cxx_config, @@ -76,6 +85,7 @@ def configure( self.cmd( 'Fix rpath for {}'.format(prometheus_lib), cxx_toolkit.rpath_set_command(prometheus_lib, '.')) + drake.cxx.set_lib_id(prometheus_lib.path(), self.toolkit) return True cmake_vars.update({ From 1334f68ea54b52024c23a3b9d4d86f970264aae3 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Wed, 10 May 2017 14:40:54 +0200 Subject: [PATCH 15/27] exposer: a single thread seems enough By default civetweb create 50 worker threads. One should suffice. --- lib/exposer.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/exposer.cc b/lib/exposer.cc index 9d5f05cb..928c2b45 100644 --- a/lib/exposer.cc +++ b/lib/exposer.cc @@ -25,9 +25,10 @@ Exposer::~Exposer() { server_->removeHandler(uri); } void Exposer::rebind(const std::string& bind_address) { - server_.reset(new CivetServer{ - {"listening_ports", bind_address.c_str()} - }); + server_.reset(new CivetServer{{ + "listening_ports", bind_address.c_str(), + "num_threads", "1", + }}); server_->addHandler(uri, metrics_handler_.get()); } From 0000a96e50652783a51fa317feca17351ec72f63 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Thu, 11 May 2017 11:01:03 +0200 Subject: [PATCH 16/27] drake: clean up --- drakefile | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/drakefile b/drakefile index 134a09d9..20ab0efa 100644 --- a/drakefile +++ b/drakefile @@ -10,15 +10,6 @@ prometheus_lib = None sources = None cxx_config = None -class GRPCBuilder(drake.cmake.CMakeBuilder): - - def execute(self): - if super().execute(): - drake.cxx.set_lib_id(protobuf_lib.path(), self.toolkit) - return True - else: - return False - def configure( cxx_toolkit, in_cxx_config, @@ -105,9 +96,10 @@ def configure( cmake_source = drake.path_source() / drake.Drake.current.prefix else: cmake_source = '../../../../prometheus/prometheus-cpp' - cmake = PrometheusCMakeBuilder( - cxx_toolkit, srcs, dsts, cmake_vars, targets = targets, - path_to_cmake_source = cmake_source) + + cmake = PrometheusCMakeBuilder(cxx_toolkit, srcs, dsts, cmake_vars, + targets = targets, + path_to_cmake_source = cmake_source) global cxx_config cxx_config = drake.cxx.Config(in_cxx_config) cxx_config += protobuf_config From af760ce0e5116498694cc2d14dc34d806dafc981 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Fri, 12 May 2017 09:16:06 +0200 Subject: [PATCH 17/27] gratuitous change To force recompilation. --- lib/exposer.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/exposer.cc b/lib/exposer.cc index 928c2b45..98e0d950 100644 --- a/lib/exposer.cc +++ b/lib/exposer.cc @@ -27,7 +27,7 @@ void Exposer::rebind(const std::string& bind_address) { server_.reset(new CivetServer{{ "listening_ports", bind_address.c_str(), - "num_threads", "1", + "num_threads", "1", }}); server_->addHandler(uri, metrics_handler_.get()); } From b6e0102749b0f10aaefa586c0bcc1ef865284431 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Fri, 12 May 2017 16:14:49 +0200 Subject: [PATCH 18/27] drake: set_lib_id no longer uses the cxx_toolkit --- drakefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drakefile b/drakefile index 20ab0efa..8cb5c906 100644 --- a/drakefile +++ b/drakefile @@ -76,7 +76,7 @@ def configure( self.cmd( 'Fix rpath for {}'.format(prometheus_lib), cxx_toolkit.rpath_set_command(prometheus_lib, '.')) - drake.cxx.set_lib_id(prometheus_lib.path(), self.toolkit) + drake.cxx.set_lib_id(prometheus_lib.path()) return True cmake_vars.update({ From 0803ccdb0439872e60237e34253314bbbf660432 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Tue, 16 May 2017 14:08:09 +0200 Subject: [PATCH 19/27] drake: use libc++ on macOS --- drakefile | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drakefile b/drakefile index 8cb5c906..a371e1ad 100644 --- a/drakefile +++ b/drakefile @@ -89,8 +89,9 @@ def configure( # libprometheus-cpp. But compiling the latter as a shared library # does not make the former PIC. So force -fPIC. # https://cmake.org/pipermail/cmake/2012-June/050941.html - 'CMAKE_C_FLAGS': '-fPIC', - 'CMAKE_CXX_FLAGS': '-fPIC', + 'CMAKE_C_FLAGS': '-fPIC' , + 'CMAKE_CXX_FLAGS': (('-stdlib=libc++' if cxx_toolkit.os == drake.os.macos else '') + + ' -fPIC'), }) if drake.path_source().absolute(): cmake_source = drake.path_source() / drake.Drake.current.prefix From cd14f5a2c03e9b09f41f37e85fe87f1a46cbf869 Mon Sep 17 00:00:00 2001 From: Antony MECHIN Date: Fri, 26 May 2017 14:16:56 +0200 Subject: [PATCH 20/27] drakefile: Let drake calculate the path for cmake. --- drakefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drakefile b/drakefile index a371e1ad..06e7c7af 100644 --- a/drakefile +++ b/drakefile @@ -96,7 +96,8 @@ def configure( if drake.path_source().absolute(): cmake_source = drake.path_source() / drake.Drake.current.prefix else: - cmake_source = '../../../../prometheus/prometheus-cpp' + cmake_source = drake.node('CMakeLists.txt').path().dirname() + cmake_source = cmake_source.without_prefix(drake.Drake.current.prefix) cmake = PrometheusCMakeBuilder(cxx_toolkit, srcs, dsts, cmake_vars, targets = targets, @@ -106,7 +107,6 @@ def configure( cxx_config += protobuf_config - # Local Variables: # mode: python # End: From 821f8c2bc380338d28e5a67082aee09a15a107ab Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Wed, 14 Jun 2017 09:28:21 +0200 Subject: [PATCH 21/27] family: expose the name --- include/prometheus/family.h | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/include/prometheus/family.h b/include/prometheus/family.h index a26b8f56..afd63308 100644 --- a/include/prometheus/family.h +++ b/include/prometheus/family.h @@ -27,6 +27,10 @@ class Family : public Collectable { Family(const std::string& name, const std::string& help, const std::map& constant_labels); + + /// Name of the family. + const std::string& name() const; + template T& Add(const std::map& labels, Args&&... args); void Remove(T* metric); @@ -58,6 +62,12 @@ Family::Family(const std::string& name, const std::string& help, assert(CheckMetricName(name_)); } +template +const std::string& + Family::name() const { + return name_; +} + template template T& Family::Add(const std::map& labels, From fc7ffeb4cffb874af05a573e567471749ba60e28 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Wed, 14 Jun 2017 09:37:42 +0200 Subject: [PATCH 22/27] drake: don't list the file we depend upon, compute this list --- drakefile | 37 ++++++------------------------------- 1 file changed, 6 insertions(+), 31 deletions(-) diff --git a/drakefile b/drakefile index 06e7c7af..eaa13ba1 100644 --- a/drakefile +++ b/drakefile @@ -1,6 +1,7 @@ import drake import drake.cmake import drake.cxx +import drake.git import os # The paths to the libprometheus-cpp headers. @@ -27,37 +28,11 @@ def configure( drake.path_root() / drake.path_build('lib/cpp'), ] prometheus_lib = drake.cxx.DynLib('lib/prometheus-cpp', tk = cxx_toolkit) - srcs = [protoc, protobuf_lib] + drake.nodes( - 'CMakeLists.txt', - 'include/prometheus/collectable.h', - 'include/prometheus/counter.h', - 'include/prometheus/counter_builder.h', - 'include/prometheus/exposer.h', - 'include/prometheus/family.h', - 'include/prometheus/gauge.h', - 'include/prometheus/gauge_builder.h', - 'include/prometheus/histogram.h', - 'include/prometheus/histogram_builder.h', - 'include/prometheus/metric.h', - 'include/prometheus/registry.h', - 'lib/counter.cc', - 'lib/counter_builder.cc', - 'lib/exposer.cc', - 'lib/gauge.cc', - 'lib/gauge_builder.cc', - 'lib/handler.cc', - 'lib/handler.h', - 'lib/histogram.cc', - 'lib/histogram_builder.cc', - 'lib/json_serializer.cc', - 'lib/json_serializer.h', - 'lib/protobuf_delimited_serializer.cc', - 'lib/protobuf_delimited_serializer.h', - 'lib/registry.cc', - 'lib/serializer.h', - 'lib/text_serializer.cc', - 'lib/text_serializer.h', - ) + # Prometheus git repo. + git = drake.git.Git(path = drake.path_source('.')) + # The files we depend upon. + files = git.ls_files('include', 'lib', 'CMakeLists.txt') + srcs = [protoc, protobuf_lib] + drake.nodes(*files) dsts = [prometheus_lib] + drake.nodes( 'lib/cpp/metrics.pb.h' From 9e1dbfa11bfb21ff41353b2f598943304d1ae0c7 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Tue, 18 Jul 2017 16:38:25 +0200 Subject: [PATCH 23/27] drake: avoid absolute paths They are a real nuisance with our BF cache: we want to be able to copy a build tree. --- drakefile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drakefile b/drakefile index eaa13ba1..eae90dfd 100644 --- a/drakefile +++ b/drakefile @@ -22,10 +22,10 @@ def configure( global prometheus_include_path, prometheus_lib, sources prometheus_include_path = [ # libprometheus-cpp's header. - drake.path_root() / drake.path_source('include'), + drake.path_source('include'), # prometheus_client_model, a submodule, that contains the protobuf # support: metrics.pb.h. - drake.path_root() / drake.path_build('lib/cpp'), + drake.path_build('lib/cpp'), ] prometheus_lib = drake.cxx.DynLib('lib/prometheus-cpp', tk = cxx_toolkit) # Prometheus git repo. From 866b7c9f199994fcd96b3d5c9d7b2991fac14b59 Mon Sep 17 00:00:00 2001 From: Antony MECHIN Date: Fri, 25 Aug 2017 11:06:57 +0200 Subject: [PATCH 24/27] drakefile: Use the cxx_config to pass include paths. --- drakefile | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/drakefile b/drakefile index eae90dfd..52c8d686 100644 --- a/drakefile +++ b/drakefile @@ -4,8 +4,6 @@ import drake.cxx import drake.git import os -# The paths to the libprometheus-cpp headers. -prometheus_include_path = [] # The path to the libprometheus-cpp library. prometheus_lib = None sources = None @@ -19,17 +17,10 @@ def configure( protobuf_lib, cmake_vars = {} ): - global prometheus_include_path, prometheus_lib, sources - prometheus_include_path = [ - # libprometheus-cpp's header. - drake.path_source('include'), - # prometheus_client_model, a submodule, that contains the protobuf - # support: metrics.pb.h. - drake.path_build('lib/cpp'), - ] + global prometheus_lib, sources prometheus_lib = drake.cxx.DynLib('lib/prometheus-cpp', tk = cxx_toolkit) # Prometheus git repo. - git = drake.git.Git(path = drake.path_source('.')) + git = drake.git.Git() # The files we depend upon. files = git.ls_files('include', 'lib', 'CMakeLists.txt') srcs = [protoc, protobuf_lib] + drake.nodes(*files) @@ -71,16 +62,18 @@ def configure( if drake.path_source().absolute(): cmake_source = drake.path_source() / drake.Drake.current.prefix else: - cmake_source = drake.node('CMakeLists.txt').path().dirname() - cmake_source = cmake_source.without_prefix(drake.Drake.current.prefix) - + cmake_source = drake.node('CMakeLists.txt').path(absolute = True).dirname() cmake = PrometheusCMakeBuilder(cxx_toolkit, srcs, dsts, cmake_vars, targets = targets, path_to_cmake_source = cmake_source) global cxx_config cxx_config = drake.cxx.Config(in_cxx_config) cxx_config += protobuf_config - + # libprometheus-cpp's header. + cxx_config.add_local_include_path('include') + # prometheus_client_model, a submodule, that contains the protobuf + # support: metrics.pb.h. + cxx_config.add_local_include_path('lib/cpp') # Local Variables: # mode: python From e7c6cefcedaeb3463bacb7efc970d49d87f6017b Mon Sep 17 00:00:00 2001 From: mefyl Date: Tue, 27 Nov 2018 20:55:31 +0100 Subject: [PATCH 25/27] Enable to retrieve gauge family names. --- core/include/prometheus/family.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/include/prometheus/family.h b/core/include/prometheus/family.h index 1c884589..1ff28b18 100644 --- a/core/include/prometheus/family.h +++ b/core/include/prometheus/family.h @@ -121,6 +121,12 @@ class Family : public Collectable { /// \return Zero or more samples for each dimensional data. std::vector Collect() override; + std::string + Name() + { + return name_; + } + private: std::unordered_map> metrics_; std::unordered_map> labels_; From ef509afb914c0a94158ba54da99625c008d11bd5 Mon Sep 17 00:00:00 2001 From: mefyl Date: Tue, 27 Nov 2018 20:55:57 +0100 Subject: [PATCH 26/27] drake: Adapt to new multi-library scheme. --- drakefile | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/drakefile b/drakefile index 52c8d686..aba0e428 100644 --- a/drakefile +++ b/drakefile @@ -4,8 +4,7 @@ import drake.cxx import drake.git import os -# The path to the libprometheus-cpp library. -prometheus_lib = None +prometheus_libs = None sources = None cxx_config = None @@ -17,21 +16,20 @@ def configure( protobuf_lib, cmake_vars = {} ): - global prometheus_lib, sources - prometheus_lib = drake.cxx.DynLib('lib/prometheus-cpp', tk = cxx_toolkit) + global prometheus_libs, sources + prometheus_libs = [drake.cxx.DynLib(p, tk = cxx_toolkit) for p in [ + 'core/prometheus-cpp-core', + 'pull/prometheus-cpp-pull', + ]] # Prometheus git repo. git = drake.git.Git() # The files we depend upon. files = git.ls_files('include', 'lib', 'CMakeLists.txt') srcs = [protoc, protobuf_lib] + drake.nodes(*files) - dsts = [prometheus_lib] + drake.nodes( - 'lib/cpp/metrics.pb.h' - ) - # Makefile target to run. targets = [ - 'prometheus-cpp', + 'all', ] class PrometheusCMakeBuilder(drake.cmake.CMakeBuilder): @@ -39,10 +37,11 @@ def configure( def execute(self): if not super().execute(): return False - self.cmd( - 'Fix rpath for {}'.format(prometheus_lib), - cxx_toolkit.rpath_set_command(prometheus_lib, '.')) - drake.cxx.set_lib_id(prometheus_lib.path()) + for l in prometheus_libs: + self.cmd( + 'Fix rpath for {}'.format(l), + cxx_toolkit.rpath_set_command(l, '.')) + drake.cxx.set_lib_id(l.path()) return True cmake_vars.update({ @@ -63,14 +62,15 @@ def configure( cmake_source = drake.path_source() / drake.Drake.current.prefix else: cmake_source = drake.node('CMakeLists.txt').path(absolute = True).dirname() - cmake = PrometheusCMakeBuilder(cxx_toolkit, srcs, dsts, cmake_vars, + cmake = PrometheusCMakeBuilder(cxx_toolkit, srcs, prometheus_libs, cmake_vars, targets = targets, path_to_cmake_source = cmake_source) global cxx_config cxx_config = drake.cxx.Config(in_cxx_config) cxx_config += protobuf_config # libprometheus-cpp's header. - cxx_config.add_local_include_path('include') + cxx_config.add_local_include_path('core/include') + cxx_config.add_local_include_path('pull/include') # prometheus_client_model, a submodule, that contains the protobuf # support: metrics.pb.h. cxx_config.add_local_include_path('lib/cpp') From cb17c88cf3149867e8f2d39d4cd64554446be69b Mon Sep 17 00:00:00 2001 From: mefyl Date: Wed, 28 Nov 2018 15:55:42 +0100 Subject: [PATCH 27/27] Adapt to new curl and zlib requirements. --- drakefile | 31 ++++++++++++++++++++----------- 1 file changed, 20 insertions(+), 11 deletions(-) diff --git a/drakefile b/drakefile index aba0e428..747efdbb 100644 --- a/drakefile +++ b/drakefile @@ -6,15 +6,19 @@ import os prometheus_libs = None sources = None -cxx_config = None +config = None def configure( cxx_toolkit, - in_cxx_config, + cxx_config, protoc, protobuf_config, protobuf_lib, - cmake_vars = {} + cmake_vars = {}, + curl_include_dir = None, + curl_library = None, + zlib_include_dir = None, + zlib_library = None, ): global prometheus_libs, sources prometheus_libs = [drake.cxx.DynLib(p, tk = cxx_toolkit) for p in [ @@ -25,7 +29,8 @@ def configure( git = drake.git.Git() # The files we depend upon. files = git.ls_files('include', 'lib', 'CMakeLists.txt') - srcs = [protoc, protobuf_lib] + drake.nodes(*files) + print(zlib_library) + srcs = [protoc, protobuf_lib, curl_library, zlib_library] + drake.nodes(*files) # Makefile target to run. targets = [ @@ -54,7 +59,11 @@ def configure( # libprometheus-cpp. But compiling the latter as a shared library # does not make the former PIC. So force -fPIC. # https://cmake.org/pipermail/cmake/2012-June/050941.html - 'CMAKE_C_FLAGS': '-fPIC' , + 'CURL_INCLUDE_DIR': str(curl_include_dir), + 'CURL_LIBRARY': str(curl_library.path(absolute = True)), + 'ZLIB_INCLUDE_DIR': str(zlib_include_dir), + 'ZLIB_LIBRARY': str(zlib_library.path(absolute = True)), + 'CMAKE_C_FLAGS': '-fPIC', 'CMAKE_CXX_FLAGS': (('-stdlib=libc++' if cxx_toolkit.os == drake.os.macos else '') + ' -fPIC'), }) @@ -65,15 +74,15 @@ def configure( cmake = PrometheusCMakeBuilder(cxx_toolkit, srcs, prometheus_libs, cmake_vars, targets = targets, path_to_cmake_source = cmake_source) - global cxx_config - cxx_config = drake.cxx.Config(in_cxx_config) - cxx_config += protobuf_config + global config + config = drake.cxx.Config(cxx_config) + config += protobuf_config # libprometheus-cpp's header. - cxx_config.add_local_include_path('core/include') - cxx_config.add_local_include_path('pull/include') + config.add_local_include_path('core/include') + config.add_local_include_path('pull/include') # prometheus_client_model, a submodule, that contains the protobuf # support: metrics.pb.h. - cxx_config.add_local_include_path('lib/cpp') + config.add_local_include_path('lib/cpp') # Local Variables: # mode: python