From eabcab26ed8acdb70148f06ea940d63fdada4128 Mon Sep 17 00:00:00 2001 From: gaschler Date: Wed, 21 Feb 2018 18:52:57 +0100 Subject: [PATCH] Expose metrics as http page. (#920) RFC=[0014](https://github.com/googlecartographer/rfcs/blob/master/text/0014-monitoring.md) --- CMakeLists.txt | 2 +- cartographer_grpc/map_builder_server_main.cc | 13 +++++++++++++ .../metrics/prometheus/metrics_test.cc | 11 +++++++++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index a0bea81..9161dd8 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -217,7 +217,7 @@ if(${BUILD_GRPC}) endif() if(${BUILD_PROMETHEUS}) target_link_libraries(${PROJECT_NAME} PUBLIC prometheus-cpp) - target_compile_definitions(${PROJECT_NAME} PRIVATE USE_PROMETHEUS=1) + target_compile_definitions(${PROJECT_NAME} PUBLIC USE_PROMETHEUS=1) endif() # Add the binary directory first, so that port.h is included after it has diff --git a/cartographer_grpc/map_builder_server_main.cc b/cartographer_grpc/map_builder_server_main.cc index 4b6f3cb..094a2dc 100644 --- a/cartographer_grpc/map_builder_server_main.cc +++ b/cartographer_grpc/map_builder_server_main.cc @@ -14,10 +14,15 @@ * limitations under the License. */ +#include "cartographer/metrics/register.h" #include "cartographer_grpc/map_builder_server.h" #include "cartographer_grpc/map_builder_server_options.h" #include "gflags/gflags.h" #include "glog/logging.h" +#if USE_PROMETHEUS +#include "cartographer_grpc/metrics/prometheus/family_factory.h" +#include "prometheus/exposer.h" +#endif DEFINE_string(configuration_directory, "", "First directory in which configuration files are searched, " @@ -31,6 +36,14 @@ namespace cartographer_grpc { void Run(const std::string& configuration_directory, const std::string& configuration_basename) { +#if USE_PROMETHEUS + metrics::prometheus::FamilyFactory registry; + cartographer::metrics::RegisterAllMetrics(®istry); + ::prometheus::Exposer exposer("0.0.0.0:9100"); + exposer.RegisterCollectable(registry.GetCollectable()); + LOG(INFO) << "Exposing metrics at http://localhost:9100/metrics"; +#endif + proto::MapBuilderServerOptions map_builder_server_options = LoadMapBuilderServerOptions(configuration_directory, configuration_basename); diff --git a/cartographer_grpc/metrics/prometheus/metrics_test.cc b/cartographer_grpc/metrics/prometheus/metrics_test.cc index 62dd51d..e1ddacd 100644 --- a/cartographer_grpc/metrics/prometheus/metrics_test.cc +++ b/cartographer_grpc/metrics/prometheus/metrics_test.cc @@ -131,6 +131,17 @@ TEST(MetricsTest, CollectHistogram) { EXPECT_EQ(collected[0].metric(0).histogram().bucket(0).cumulative_count(), 1); } +TEST(MetricsTest, RunExposerServer) { + FamilyFactory registry; + Algorithm::RegisterMetrics(®istry); + cartographer::metrics::RegisterAllMetrics(®istry); + ::prometheus::Exposer exposer("0.0.0.0:9100"); + exposer.RegisterCollectable(registry.GetCollectable()); + + Algorithm algorithm; + algorithm.Run(); +} + } // namespace } // namespace prometheus } // namespace metrics