export swap used in /metrics.json end-point (#1499)

* export swap used in /metrics.json end-point

* remove logs

* add macro to run on linux only

* update the swap usage fetch approach

* remove check to maintain consistent API response
This commit is contained in:
Krunal Gandhi 2024-01-22 08:10:39 +00:00 committed by GitHub
parent 37f8ea0dbd
commit e463d2f0f1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 21 additions and 0 deletions

View File

@ -143,6 +143,8 @@ private:
static uint64_t get_memory_non_proc_bytes();
static uint64_t linux_get_swap_used_bytes();
public:
SystemMetrics() {

View File

@ -72,6 +72,10 @@ void SystemMetrics::get(const std::string &data_dir_path, nlohmann::json &result
result["system_memory_total_bytes"] = std::to_string(get_memory_total_bytes());
result["system_memory_used_bytes"] = std::to_string(get_memory_used_bytes());
#ifdef __linux__
auto swap_bytes = linux_get_swap_used_bytes();
result["system_memory_used_swap_bytes"] = std::to_string(swap_bytes);
#endif
// CPU and Network metrics
#if __linux__
const std::vector<cpu_stat_t>& cpu_stats = get_cpu_stats();
@ -227,3 +231,18 @@ void SystemMetrics::linux_get_network_data(const std::string & stat_path,
}
}
}
uint64_t SystemMetrics::linux_get_swap_used_bytes() {
std::string filename = "/proc/" + std::to_string(::getpid()) + "/status";
std::string token;
std::ifstream file(filename);
while(file >> token) {
if(token == "VmSwap:") {
uint64_t mem_kB;
if(file >> mem_kB) {
return mem_kB * 1024;
}
}
}
return 0;
}