Expose metrics as http page. (#920)
RFC=[0014](https://github.com/googlecartographer/rfcs/blob/master/text/0014-monitoring.md)master
							parent
							
								
									30114e364a
								
							
						
					
					
						commit
						eabcab26ed
					
				|  | @ -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 | ||||
|  |  | |||
|  | @ -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); | ||||
|  |  | |||
|  | @ -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
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue