mirror of
https://github.com/typesense/typesense.git
synced 2025-05-16 11:28:44 +08:00
Expose network received/sent bytes metric.
This commit is contained in:
parent
a9de50c481
commit
13591fc017
@ -6,6 +6,7 @@
|
||||
#include "json.hpp"
|
||||
|
||||
const int NUM_CPU_STATES = 10;
|
||||
const int NUM_NETWORK_STATS = 16;
|
||||
|
||||
struct cpu_data_t {
|
||||
std::string cpu;
|
||||
@ -145,6 +146,8 @@ public:
|
||||
non_proc_mem_bytes = memory_used_bytes - get_memory_active_bytes();
|
||||
}
|
||||
|
||||
static void linux_get_network_data(const std::string & stat_path, uint64_t& received_bytes, uint64_t& sent_bytes);
|
||||
|
||||
void get(const std::string & data_dir_path, nlohmann::json& result);
|
||||
|
||||
static float used_memory_ratio();
|
||||
|
@ -14,6 +14,7 @@
|
||||
#include <mach/mach_host.h>
|
||||
#endif
|
||||
|
||||
#include "string_utils.h"
|
||||
#include "jemalloc.h"
|
||||
|
||||
#if __APPLE__
|
||||
@ -67,7 +68,7 @@ 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());
|
||||
|
||||
// CPU METRICS
|
||||
// CPU and Network metrics
|
||||
#if __linux__
|
||||
const std::vector<cpu_stat_t>& cpu_stats = get_cpu_stats();
|
||||
|
||||
@ -75,6 +76,11 @@ void SystemMetrics::get(const std::string &data_dir_path, nlohmann::json &result
|
||||
std::string cpu_id = (i == 0) ? "" : std::to_string(i);
|
||||
result["system_cpu" + cpu_id + "_active_percentage"] = cpu_stats[i].active;
|
||||
}
|
||||
|
||||
uint64_t received_bytes, sent_bytes;
|
||||
linux_get_network_data("/proc/net/dev", received_bytes, sent_bytes);
|
||||
result["system_network_received_bytes"] = std::to_string(received_bytes);
|
||||
result["system_network_sent_bytes"] = std::to_string(sent_bytes);
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -171,3 +177,51 @@ uint64_t SystemMetrics::get_memory_non_proc_bytes() {
|
||||
|
||||
return non_proc_mem_bytes;
|
||||
}
|
||||
|
||||
void SystemMetrics::linux_get_network_data(const std::string & stat_path,
|
||||
uint64_t &received_bytes, uint64_t &sent_bytes) {
|
||||
//std::ifstream stat_file("/proc/net/dev");
|
||||
std::ifstream stat_file(stat_path);
|
||||
std::string line;
|
||||
|
||||
// TODO: this probably needs to be handled better!
|
||||
const std::string STR_ENS5("ens5");
|
||||
const std::string STR_ETH0("eth0");
|
||||
|
||||
/*
|
||||
Inter-| Receive | Transmit
|
||||
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
|
||||
ens5: 324278716 897631 0 0 0 0 0 0 93933882 575535 0 0 0 0 0 0
|
||||
*/
|
||||
|
||||
received_bytes = 0;
|
||||
sent_bytes = 0;
|
||||
|
||||
while (std::getline(stat_file, line)) {
|
||||
StringUtils::trim(line);
|
||||
if (line.rfind(STR_ENS5, 0) == 0 || line.rfind(STR_ETH0, 0) == 0) {
|
||||
std::istringstream ss(line);
|
||||
std::string throwaway;
|
||||
|
||||
// read interface label
|
||||
ss >> throwaway;
|
||||
|
||||
uint64_t stat_value;
|
||||
|
||||
// read stats
|
||||
for (int i = 0; i < NUM_NETWORK_STATS; i++) {
|
||||
ss >> stat_value;
|
||||
|
||||
if(i == 0) {
|
||||
received_bytes = stat_value;
|
||||
}
|
||||
|
||||
if(i == 8) {
|
||||
sent_bytes = stat_value;
|
||||
}
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
4
test/resources/proc_net_dev.txt
Normal file
4
test/resources/proc_net_dev.txt
Normal file
@ -0,0 +1,4 @@
|
||||
Inter-| Receive | Transmit
|
||||
face |bytes packets errs drop fifo frame compressed multicast|bytes packets errs drop fifo colls carrier compressed
|
||||
ens5: 324278716 897631 0 0 0 0 0 0 93933882 575535 0 0 0 0 0 0
|
||||
lo: 1308386 5312 0 0 0 0 0 0 1308386 5312 0 0 0 0 0 0
|
10
test/system_metrics_test.cpp
Normal file
10
test/system_metrics_test.cpp
Normal file
@ -0,0 +1,10 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include "system_metrics.h"
|
||||
|
||||
TEST(SystemMetricsTest, ParsingNetworkStats) {
|
||||
std::string proc_net_dev_path = std::string(ROOT_DIR)+"test/resources/proc_net_dev.txt";
|
||||
uint64_t received_bytes, sent_bytes;
|
||||
SystemMetrics::linux_get_network_data(proc_net_dev_path, received_bytes, sent_bytes);
|
||||
ASSERT_EQ(324278716, received_bytes);
|
||||
ASSERT_EQ(93933882, sent_bytes);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user