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_; diff --git a/drakefile b/drakefile new file mode 100644 index 00000000..747efdbb --- /dev/null +++ b/drakefile @@ -0,0 +1,89 @@ +import drake +import drake.cmake +import drake.cxx +import drake.git +import os + +prometheus_libs = None +sources = None +config = None + +def configure( + cxx_toolkit, + cxx_config, + protoc, + protobuf_config, + protobuf_lib, + 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 [ + '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') + print(zlib_library) + srcs = [protoc, protobuf_lib, curl_library, zlib_library] + drake.nodes(*files) + + # Makefile target to run. + targets = [ + 'all', + ] + + class PrometheusCMakeBuilder(drake.cmake.CMakeBuilder): + + def execute(self): + if not super().execute(): + return False + 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({ + 'BUILD_SHARED_LIBS': 'ON', + '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" + # 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 + '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'), + }) + if drake.path_source().absolute(): + 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, prometheus_libs, cmake_vars, + targets = targets, + path_to_cmake_source = cmake_source) + global config + config = drake.cxx.Config(cxx_config) + config += protobuf_config + # libprometheus-cpp's header. + 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. + config.add_local_include_path('lib/cpp') + +# Local Variables: +# mode: python +# End: diff --git a/pull/include/prometheus/exposer.h b/pull/include/prometheus/exposer.h index c318f751..e130c94f 100644 --- a/pull/include/prometheus/exposer.h +++ b/pull/include/prometheus/exposer.h @@ -21,6 +21,10 @@ class Exposer { public: explicit Exposer(const std::string& bind_address, const std::string& uri = std::string("/metrics")); + /// Change the exposition port. + void + rebind(const std::string& bind_address, + const std::string& uri = std::string("/metrics")); ~Exposer(); void RegisterCollectable(const std::weak_ptr& collectable); diff --git a/pull/src/exposer.cc b/pull/src/exposer.cc index be8fa730..51cdb48c 100644 --- a/pull/src/exposer.cc +++ b/pull/src/exposer.cc @@ -1,6 +1,7 @@ #include "prometheus/exposer.h" #include +#include #include #include @@ -12,18 +13,26 @@ namespace prometheus { Exposer::Exposer(const std::string& bind_address, const std::string& uri) - : server_(new CivetServer{ - {"listening_ports", bind_address.c_str(), "num_threads", "2"}}), - exposer_registry_(std::make_shared()), + : exposer_registry_(std::make_shared()), metrics_handler_( new detail::MetricsHandler{collectables_, *exposer_registry_}), uri_(uri) { 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, const std::string& uri) { + server_.reset(new CivetServer{{ + "listening_ports", bind_address.c_str(), + "num_threads", "2", + }}); + server_->addHandler(uri, metrics_handler_.get()); + uri_ = uri; +} + void Exposer::RegisterCollectable( const std::weak_ptr& collectable) { collectables_.push_back(collectable);