Add prometheus

This commit is contained in:
Barzan Hayati 2025-07-06 12:21:06 +00:00
parent 0969be1226
commit 6a240dae33
2 changed files with 54 additions and 1 deletions

View File

@ -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
nvdsgst_meta nvds_utils nvdsgst_helper
prometheus-cpp-core prometheus-cpp-pull # prometheus-cpp-exposer nvdsgst_metnvdsa
microhttpd)

View File

@ -3,6 +3,13 @@
#include <fstream>
#include <iostream>
#include <string>
#include <thread>
// prometheus-cpp headers
#include <prometheus/counter.h>
#include <prometheus/exposer.h>
#include <prometheus/gauge.h>
#include <prometheus/histogram.h>
#include <prometheus/registry.h>
#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<bool> running{true};
prometheus::Gauge *my_gauge = nullptr;
std::shared_ptr<prometheus::Registry> registry;
prometheus::Family<prometheus::Gauge> *gauge_family = nullptr;
std::unique_ptr<prometheus::Exposer> exposer = nullptr;
prometheus::Counter *counter = nullptr;
prometheus::Family<prometheus::Counter> *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<double>(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] << " <RTSP_URL>" << std::endl;
return 1;
}
const std::string address_prometheus = "0.0.0.0:8080";
exposer = std::make_unique<prometheus::Exposer>(address_prometheus);
// Set up Prometheus as before...
registry = std::make_shared<prometheus::Registry>();
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;
}