diff --git a/CMakeLists.txt b/CMakeLists.txt index 19b0bdb..c4ff61f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,7 @@ endif() find_package(PkgConfig REQUIRED) find_package(CUDA REQUIRED) +find_package(prometheus-cpp REQUIRED) pkg_check_modules(GLIB REQUIRED glib-2.0 gobject-2.0 nlohmann_json gstreamer-base-1.0 gstreamer-rtsp-server-1.0 gstreamer-rtsp-1.0 gstreamer-1.0 gstreamer-video-1.0) @@ -95,4 +96,6 @@ target_link_libraries(${PROJECT_NAME} ${GLIB_LIBRARIES}) target_link_libraries(${PROJECT_NAME} gstbase-1.0 gstreamer-1.0 gstrtp-1.0 gstvideo-1.0 gstrtspserver-1.0) target_link_libraries(${PROJECT_NAME} cudart cuda) target_link_libraries(${PROJECT_NAME} nvdsgst_infer nvds_meta nvds_inferutils - nvdsgst_meta nvds_utils nvdsgst_helper)#nvdsgst_metnvdsa \ No newline at end of file + nvdsgst_meta nvds_utils nvdsgst_helper + prometheus-cpp-core prometheus-cpp-pull # prometheus-cpp-exposer nvdsgst_metnvdsa + microhttpd) \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index f2c771c..ea680c7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,6 +3,13 @@ #include #include #include +#include +// prometheus-cpp headers +#include +#include +#include +#include +#include #include "camera_manager.hpp" #include "pipeline_manager.hpp" @@ -15,6 +22,13 @@ gboolean *check_finished_streams = NULL; gboolean *g_source_enabled = NULL; guint num_sources; // number of input cameras GstElement **g_source_bin_list = NULL; +std::atomic running{true}; +prometheus::Gauge *my_gauge = nullptr; +std::shared_ptr registry; +prometheus::Family *gauge_family = nullptr; +std::unique_ptr exposer = nullptr; +prometheus::Counter *counter = nullptr; +prometheus::Family *counter_family = nullptr; void allocate_memory_variables_cameras(const int MAX_NUM_SOURCES) { g_source_id_list = (gint *)g_malloc0(sizeof(gint) * MAX_NUM_SOURCES); @@ -51,11 +65,44 @@ int load_rtsp_address(CameraManager *camera_manager, fs::path file_path) { return EXIT_SUCCESS; } +void metrics_loop(prometheus::Gauge *my_gauge) { + while (running) { + std::cout << "metrics_loop" << std::endl; + counter->Increment(); + // simulate updating a metric + my_gauge->Set(static_cast(rand() % 100)); + std::this_thread::sleep_for(std::chrono::seconds(2)); + } +} + int main(int argc, char *argv[]) { if (argc < 1) { std::cerr << "Usage: " << argv[0] << " " << std::endl; return 1; } + + const std::string address_prometheus = "0.0.0.0:8080"; + exposer = std::make_unique(address_prometheus); + + // Set up Prometheus as before... + registry = std::make_shared(); + gauge_family = &prometheus::BuildGauge() + .Name("frame_delay") + .Help("Delay between frames") + .Register(*registry); + + my_gauge = &gauge_family->Add({{"source", "camera1"}}); + + counter_family = &prometheus::BuildCounter() + .Name("frames_received_total") + .Help("Total frames received") + .Register(*registry); + + counter = &counter_family->Add({{"label", "value"}}); + exposer->RegisterCollectable(registry); + + std::thread metrics_thread(metrics_loop, my_gauge); + CameraManager *camera_manager = new CameraManager(); // Path handling works across platforms fs::path data_dir = "../data"; @@ -77,5 +124,8 @@ int main(int argc, char *argv[]) { pipeline_manager->create_pipeline(); pipeline_manager->create_pipeline_elements(num_sources, url_camera); + // On shutdown: + running = false; + metrics_thread.join(); // optional: wait on thread before exiting return 0; } \ No newline at end of file