5#include <spdlog/spdlog.h>
8#include <sys/resource.h>
16 if ((dir = opendir(path.c_str())) !=
nullptr)
18 while (readdir(dir) !=
nullptr)
30 struct rusage r_usage {};
31 getrusage(RUSAGE_SELF, &r_usage);
32 return r_usage.ru_maxrss;
37 struct rusage r_usage {};
38 getrusage(RUSAGE_SELF, &r_usage);
39 return r_usage.ru_majflt;
55 auto nowCpuTime = times(&nowCpu);
57 const double usage = 100.0 *
static_cast<double>(nowCpu.tms_utime -
_oldCpu.tms_utime) /
70 size_t readBytes = buffer.empty() ? 0 : std::stoull(buffer);
74 size_t writeBytes = buffer.empty() ? 0 : std::stoull(buffer);
95 _pDiskRead->Set(
static_cast<double>(diskRead));
113 catch (
const std::exception &e)
115 spdlog::error(
"Self monitoring failed: {}", e.what());
123 const std::shared_ptr<prometheus::Registry> ®)
124 : _checkFlag(std::move(checkFlag))
128 throw std::invalid_argument(
"Registry is nullptr");
132 &prometheus::BuildGauge().Name(
"init_time").Help(
"Initialization time of application").Register(*reg).Add({});
134 &prometheus::BuildGauge().Name(
"current_time").Help(
"Current time of application").Register(*reg).Add({});
136 &prometheus::BuildGauge().Name(
"memory_usage").Help(
"Memory usage of application").Register(*reg).Add({});
138 &prometheus::BuildGauge().Name(
"page_faults").Help(
"Page faults of application").Register(*reg).Add({});
139 _pCpuUsage = &prometheus::BuildGauge().Name(
"cpu_usage").Help(
"CPU usage of application").Register(*reg).Add({});
140 _pDiskRead = &prometheus::BuildGauge().Name(
"disk_read").Help(
"Disk read of application").Register(*reg).Add({});
141 _pDiskWrite = &prometheus::BuildGauge().Name(
"disk_write").Help(
"Disk write of application").Register(*reg).Add({});
143 &prometheus::BuildGauge().Name(
"thread_count").Help(
"Thread count of application").Register(*reg).Add({});
145 .Name(
"file_descriptor_count")
146 .Help(
"File descriptor count of application")
std::vector< std::string > findFromFile(const std::string &filePath, const std::string &pattern, std::string &lastWord)
constexpr int SLEEP_INTERVAL_SEC
prometheus::Gauge * _pMemory
Pointer to the memory usage gauge.
clock_t _oldCpuTime
Variable to store the old CPU time.
ProcessMetrics(std::shared_ptr< std::atomic_flag > checkFlag, const std::shared_ptr< prometheus::Registry > ®)
prometheus::Gauge * _pInitTime
Pointer to initialization time gauge.
prometheus::Gauge * _pCurrentTime
Pointer to the current time gauge.
size_t _oldWriteBytes
Variable to store the old write bytes.
std::atomic_flag _shouldStop
Flag to stop monitoring.
prometheus::Gauge * _pCpuUsage
Pointer to the CPU usage gauge.
void threadRunner() noexcept
prometheus::Gauge * _pPageFaults
Pointer to the page faults gauge.
prometheus::Gauge * _pFileDescriptorCount
Pointer to the file descriptor count gauge.
std::pair< size_t, size_t > getDiskIO()
prometheus::Gauge * _pDiskRead
Pointer to the disk read gauge.
size_t _oldReadBytes
Variable to store the old read bytes.
prometheus::Gauge * _pThreadCount
Pointer to the thread count gauge.
static size_t countDirectoryEntries(const std::string &path)
static long int getMemoryUsage()
std::shared_ptr< std::atomic_flag > _checkFlag
Runtime check flag.
std::unique_ptr< std::thread > _thread
Thread handler.
static size_t getThreadCount()
static long int getPageFaults()
static size_t getFileDescriptorCount()
prometheus::Gauge * _pDiskWrite
Pointer to the disk write gauge.
Structure to store the old CPU times.