Skip to content

Commit

Permalink
refactor(libsinsp): native memory conversion in metrics_v2
Browse files Browse the repository at this point in the history
Signed-off-by: Melissa Kilby <[email protected]>
  • Loading branch information
incertum committed Jan 27, 2024
1 parent 97bdcce commit 84c56dc
Show file tree
Hide file tree
Showing 5 changed files with 130 additions and 58 deletions.
1 change: 1 addition & 0 deletions userspace/libscap/metrics_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ typedef enum metrics_v2_value_unit{
METRIC_VALUE_UNIT_PERC,
METRIC_VALUE_UNIT_MEMORY_BYTES,
METRIC_VALUE_UNIT_MEMORY_KILOBYTES,
METRIC_VALUE_UNIT_MEMORY_MEGABYTES,
METRIC_VALUE_UNIT_TIME_NS,
}metrics_v2_value_unit;

Expand Down
47 changes: 29 additions & 18 deletions userspace/libsinsp/metrics_collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ limitations under the License.
#include <sys/times.h>
#include <sys/stat.h>
#include <libsinsp/metrics_collector.h>
#include <libscap/strl.h>

static const char *const sinsp_stats_v2_resource_utilization_names[] = {
[SINSP_RESOURCE_UTILIZATION_CPU_PERC] = "cpu_usage_perc",
Expand Down Expand Up @@ -293,11 +292,13 @@ uint64_t metrics_collector::get_container_memory_usage() const
return memory_used;
}

metrics_collector::metrics_collector(sinsp* inspector, const uint32_t flags) :
metrics_collector::metrics_collector(sinsp* inspector, const uint32_t& flags, const bool& convert_memory_to_mb) :
m_inspector(inspector),
m_metrics_flags((METRICS_V2_KERNEL_COUNTERS | METRICS_V2_LIBBPF_STATS | METRICS_V2_RESOURCE_UTILIZATION | METRICS_V2_STATE_COUNTERS))
m_metrics_flags((METRICS_V2_KERNEL_COUNTERS | METRICS_V2_LIBBPF_STATS | METRICS_V2_RESOURCE_UTILIZATION | METRICS_V2_STATE_COUNTERS)),
m_convert_memory_to_mb(true)
{
m_metrics_flags = flags;
m_convert_memory_to_mb = convert_memory_to_mb;
}

metrics_collector::~metrics_collector()
Expand Down Expand Up @@ -337,34 +338,44 @@ void metrics_collector::snapshot()
if((m_metrics_flags & METRICS_V2_RESOURCE_UTILIZATION))
{
const scap_agent_info* agent_info = m_inspector->get_agent_info();
uint32_t rss = 0;
uint32_t vsz = 0;
uint32_t pss = 0;
uint64_t memory_used_host = 0;
uint64_t open_fds_host = 0;
double cpu_usage_perc = 0.0;
double cpu_usage_perc_total_host = 0.0;
uint32_t procs_running_host = 0;
uint32_t rss{0}, vsz{0}, pss{0}, procs_running_host{0};
uint64_t memory_used_host{0}, open_fds_host{0};
double cpu_usage_perc{0.0}, cpu_usage_perc_total_host{0.0};
uint64_t container_memory_usage = get_container_memory_usage();
metrics_v2_value_unit rss_unit{METRIC_VALUE_UNIT_MEMORY_KILOBYTES}, vsz_unit{METRIC_VALUE_UNIT_MEMORY_KILOBYTES}, \
pss_unit{METRIC_VALUE_UNIT_MEMORY_KILOBYTES}, memory_used_host_unit{METRIC_VALUE_UNIT_MEMORY_KILOBYTES}, container_memory_usage_unit{METRIC_VALUE_UNIT_MEMORY_BYTES};
metrics_v2_value_type rss_type{METRIC_VALUE_TYPE_U32}, vsz_type{METRIC_VALUE_TYPE_U32}, \
pss_type{METRIC_VALUE_TYPE_U32}, memory_used_host_type{METRIC_VALUE_TYPE_U32}, container_memory_usage_type{METRIC_VALUE_TYPE_U64};
get_cpu_usage_and_total_procs(agent_info->start_time, cpu_usage_perc, cpu_usage_perc_total_host, procs_running_host);
get_rss_vsz_pss_total_memory_and_open_fds(rss, vsz, pss, memory_used_host, open_fds_host);
if(m_convert_memory_to_mb)
{
rss = convert_memory(rss_unit, METRIC_VALUE_UNIT_MEMORY_MEGABYTES, rss);
vsz = convert_memory(vsz_unit, METRIC_VALUE_UNIT_MEMORY_MEGABYTES, vsz);
pss = convert_memory(pss_unit, METRIC_VALUE_UNIT_MEMORY_MEGABYTES, pss);
memory_used_host = convert_memory(memory_used_host_unit, METRIC_VALUE_UNIT_MEMORY_MEGABYTES, memory_used_host);
container_memory_usage = convert_memory(container_memory_usage_unit, METRIC_VALUE_UNIT_MEMORY_MEGABYTES, container_memory_usage);
rss_unit = vsz_unit = pss_unit = memory_used_host_unit = container_memory_usage_unit = METRIC_VALUE_UNIT_MEMORY_MEGABYTES;
rss_type = vsz_type = pss_type = memory_used_host_type = container_memory_usage_type = METRIC_VALUE_TYPE_D;
}
// Resource utilization of the agent itself
m_metrics.push_back(new_metric(sinsp_stats_v2_resource_utilization_names[SINSP_RESOURCE_UTILIZATION_CPU_PERC], \
METRICS_V2_RESOURCE_UTILIZATION, METRIC_VALUE_TYPE_D, METRIC_VALUE_UNIT_PERC, METRIC_VALUE_NON_MONOTONIC_CURRENT, cpu_usage_perc));
m_metrics.push_back(new_metric(sinsp_stats_v2_resource_utilization_names[SINSP_RESOURCE_UTILIZATION_MEMORY_RSS], \
METRICS_V2_RESOURCE_UTILIZATION, METRIC_VALUE_TYPE_U32, METRIC_VALUE_UNIT_MEMORY_KILOBYTES, METRIC_VALUE_NON_MONOTONIC_CURRENT, rss));
METRICS_V2_RESOURCE_UTILIZATION, rss_type, rss_unit, METRIC_VALUE_NON_MONOTONIC_CURRENT, rss));
m_metrics.push_back(new_metric(sinsp_stats_v2_resource_utilization_names[SINSP_RESOURCE_UTILIZATION_MEMORY_VSZ], \
METRICS_V2_RESOURCE_UTILIZATION, METRIC_VALUE_TYPE_U32, METRIC_VALUE_UNIT_MEMORY_KILOBYTES, METRIC_VALUE_NON_MONOTONIC_CURRENT, vsz));
METRICS_V2_RESOURCE_UTILIZATION, vsz_type, vsz_unit, METRIC_VALUE_NON_MONOTONIC_CURRENT, vsz));
m_metrics.push_back(new_metric(sinsp_stats_v2_resource_utilization_names[SINSP_RESOURCE_UTILIZATION_MEMORY_PSS], \
METRICS_V2_RESOURCE_UTILIZATION, METRIC_VALUE_TYPE_U32, METRIC_VALUE_UNIT_MEMORY_KILOBYTES, METRIC_VALUE_NON_MONOTONIC_CURRENT, pss));
METRICS_V2_RESOURCE_UTILIZATION, pss_type, pss_unit, METRIC_VALUE_NON_MONOTONIC_CURRENT, pss));
m_metrics.push_back(new_metric(sinsp_stats_v2_resource_utilization_names[SINSP_RESOURCE_UTILIZATION_CONTAINER_MEMORY], \
METRICS_V2_RESOURCE_UTILIZATION, METRIC_VALUE_TYPE_U64, METRIC_VALUE_UNIT_MEMORY_BYTES, METRIC_VALUE_NON_MONOTONIC_CURRENT, get_container_memory_usage()));
METRICS_V2_RESOURCE_UTILIZATION, container_memory_usage_type, container_memory_usage_unit, METRIC_VALUE_NON_MONOTONIC_CURRENT, container_memory_usage));
// Resource utilization / load indicators of the underlying host
m_metrics.push_back(new_metric(sinsp_stats_v2_resource_utilization_names[SINSP_RESOURCE_UTILIZATION_CPU_PERC_TOTAL_HOST], \
METRICS_V2_RESOURCE_UTILIZATION, METRIC_VALUE_TYPE_D, METRIC_VALUE_UNIT_PERC, METRIC_VALUE_NON_MONOTONIC_CURRENT, cpu_usage_perc_total_host));
m_metrics.push_back(new_metric(sinsp_stats_v2_resource_utilization_names[SINSP_RESOURCE_UTILIZATION_PROCS_HOST], \
METRICS_V2_RESOURCE_UTILIZATION, METRIC_VALUE_TYPE_U32, METRIC_VALUE_UNIT_COUNT, METRIC_VALUE_NON_MONOTONIC_CURRENT, procs_running_host));
m_metrics.push_back(new_metric(sinsp_stats_v2_resource_utilization_names[SINSP_RESOURCE_UTILIZATION_MEMORY_TOTAL_HOST], \
METRICS_V2_RESOURCE_UTILIZATION, METRIC_VALUE_TYPE_U64, METRIC_VALUE_UNIT_MEMORY_KILOBYTES, METRIC_VALUE_NON_MONOTONIC_CURRENT, memory_used_host));
METRICS_V2_RESOURCE_UTILIZATION, memory_used_host_type, memory_used_host_unit, METRIC_VALUE_NON_MONOTONIC_CURRENT, memory_used_host));
m_metrics.push_back(new_metric(sinsp_stats_v2_resource_utilization_names[SINSP_RESOURCE_UTILIZATION_FDS_TOTAL_HOST], \
METRICS_V2_RESOURCE_UTILIZATION, METRIC_VALUE_TYPE_U64, METRIC_VALUE_UNIT_COUNT, METRIC_VALUE_NON_MONOTONIC_CURRENT, open_fds_host));
}
Expand Down Expand Up @@ -451,11 +462,11 @@ const std::vector<metrics_v2>& metrics_collector::get_metrics() const
std::unique_ptr<metrics_collector> metrics_collector::mc_instance = nullptr;

// Factory method implementation
std::unique_ptr<metrics_collector> metrics_collector::create(sinsp* inspector, const uint32_t flags)
std::unique_ptr<metrics_collector> metrics_collector::create(sinsp* inspector, const uint32_t& flags, const bool& convert_memory_to_mb)
{
if (!mc_instance)
{
mc_instance = std::unique_ptr<metrics_collector>(new metrics_collector(inspector, flags));
mc_instance = std::unique_ptr<metrics_collector>(new metrics_collector(inspector, flags, convert_memory_to_mb));
}

return std::move(mc_instance);
Expand Down
58 changes: 48 additions & 10 deletions userspace/libsinsp/metrics_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
*/

#pragma once
#include <cmath>
#include <libscap/metrics_v2.h>
#include <libscap/scap_machine_info.h>
#include <libsinsp/threadinfo.h>
#include <libscap/strl.h>

struct sinsp_stats_v2
{
Expand All @@ -44,15 +46,15 @@ struct sinsp_stats_v2

enum sinsp_stats_v2_resource_utilization
{
SINSP_RESOURCE_UTILIZATION_CPU_PERC = 0, ///< Current CPU usage, `ps` like, unit: percentage of one CPU.
SINSP_RESOURCE_UTILIZATION_MEMORY_RSS, ///< Current RSS (Resident Set Size), unit: kb.
SINSP_RESOURCE_UTILIZATION_MEMORY_VSZ, ///< Current VSZ (Virtual Memory Size), unit: kb.
SINSP_RESOURCE_UTILIZATION_MEMORY_PSS, ///< Current PSS (Proportional Set Size), unit: kb.
SINSP_RESOURCE_UTILIZATION_CPU_PERC = 0, ///< Current CPU usage, `ps` util like calculation for the calling process (/proc/self), unit: percentage of one CPU.
SINSP_RESOURCE_UTILIZATION_MEMORY_RSS, ///< Current RSS (Resident Set Size), calculated based on /proc/self/status info, unit: kb.
SINSP_RESOURCE_UTILIZATION_MEMORY_VSZ, ///< Current VSZ (Virtual Memory Size), calculated based on /proc/self/status info, unit: kb.
SINSP_RESOURCE_UTILIZATION_MEMORY_PSS, ///< Current PSS (Proportional Set Size), calculated based on /proc/self/smaps_rollup info, unit: kb.
SINSP_RESOURCE_UTILIZATION_CONTAINER_MEMORY, ///< Cgroup current memory used, default Kubernetes /sys/fs/cgroup/memory/memory.usage_in_bytes, unit: bytes.
SINSP_RESOURCE_UTILIZATION_CPU_PERC_TOTAL_HOST, ///< Current total host CPU usage (all CPUs), unit: percentage.
SINSP_RESOURCE_UTILIZATION_MEMORY_TOTAL_HOST, ///< Current total memory used out of available host memory, unit: kb.
SINSP_RESOURCE_UTILIZATION_PROCS_HOST, ///< Number of processes currently running on CPUs on the host, unit: count.
SINSP_RESOURCE_UTILIZATION_FDS_TOTAL_HOST, ///< Number of allocated fds on the host, unit: count.
SINSP_RESOURCE_UTILIZATION_CPU_PERC_TOTAL_HOST, ///< Current total host CPU usage (all CPUs), calculated based on ${HOST_ROOT}/proc/stat info, unit: percentage.
SINSP_RESOURCE_UTILIZATION_MEMORY_TOTAL_HOST, ///< Current total memory used out of available host memory, calculated based on ${HOST_ROOT}/proc/meminfo info, unit: kb.
SINSP_RESOURCE_UTILIZATION_PROCS_HOST, ///< Number of processes currently running on CPUs on the host, retrieved from ${HOST_ROOT}/proc/stat line `procs_running`, unit: count.
SINSP_RESOURCE_UTILIZATION_FDS_TOTAL_HOST, ///< Number of allocated fds on the host, retrieved from ${HOST_ROOT}/proc/sys/fs/file-nr, unit: count.
SINSP_STATS_V2_N_THREADS, ///< Total number of threads currently stored in the sinsp state thread table, unit: count.
SINSP_STATS_V2_N_FDS, ///< Total number of fds currently stored across all threadtables associated with each active thread in the sinsp state thread table, unit: count.
SINSP_STATS_V2_NONCACHED_FD_LOOKUPS, ///< fdtable state related counters, unit: count.
Expand Down Expand Up @@ -84,7 +86,7 @@ class metrics_collector
{
public:
// Factory method for creating instances
static std::unique_ptr<metrics_collector> create(sinsp* inspector, const uint32_t flags);
static std::unique_ptr<metrics_collector> create(sinsp* inspector, const uint32_t& flags, const bool& convert_memory_to_mb);
~metrics_collector();

// Method to fill up m_metrics_buffer with current metrics; refreshes m_metrics with up-to-date metrics on each call
Expand All @@ -93,11 +95,47 @@ class metrics_collector
// Method to get a const reference to m_metrics buffer
const std::vector<metrics_v2>& get_metrics() const;

// Method to convert memory units; however tied to metrics_v2 definitions
template <typename T>
double convert_memory(metrics_v2_value_unit source_unit, metrics_v2_value_unit dest_unit, T val)
{
double factor = double(1);
switch(source_unit)
{
case METRIC_VALUE_UNIT_MEMORY_BYTES:
factor = double(1);
break;
case METRIC_VALUE_UNIT_MEMORY_KILOBYTES:
factor = (double)1024;
break;
case METRIC_VALUE_UNIT_MEMORY_MEGABYTES:
factor = (double)1024 * (double)1024;
break;
default:
return (double)0;
}

double bytes_val = val * (double)factor;
switch(dest_unit)
{
case METRIC_VALUE_UNIT_MEMORY_BYTES:
return (double)bytes_val;
case METRIC_VALUE_UNIT_MEMORY_KILOBYTES:
return std::round((bytes_val / (double)1024) * (double)10) / (double)10; // round to 1 decimal
case METRIC_VALUE_UNIT_MEMORY_MEGABYTES:
return std::round((bytes_val / (double)1024 / (double)1024) * (double)10) / (double)10; // round to 1 decimal
default:
return (double)0;
}
return (double)0;
}

private:
metrics_collector(sinsp* inspector, const uint32_t flags);
metrics_collector(sinsp* inspector, const uint32_t& flags, const bool& convert_memory_to_mb);
static std::unique_ptr<metrics_collector> mc_instance;
sinsp* m_inspector;
uint32_t m_metrics_flags;
bool m_convert_memory_to_mb;
std::vector<metrics_v2> m_metrics;

void get_rss_vsz_pss_total_memory_and_open_fds(uint32_t &rss, uint32_t &vsz, uint32_t &pss, uint64_t &memory_used_host, uint64_t &open_fds_host);
Expand Down
1 change: 0 additions & 1 deletion userspace/libsinsp/sinsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -1080,7 +1080,6 @@ VISIBILITY_PRIVATE
const scap_machine_info* m_machine_info;
const scap_agent_info* m_agent_info;
std::shared_ptr<sinsp_stats_v2> m_sinsp_stats_v2;
metrics_v2 m_sinsp_stats_v2_buffer[SINSP_MAX_STATS_V2];
uint32_t m_num_cpus;
bool m_flush_memory_dump;
bool m_large_envs_enabled;
Expand Down
81 changes: 52 additions & 29 deletions userspace/libsinsp/test/sinsp_metrics.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,85 +32,108 @@ TEST_F(sinsp_with_test_input, sinsp_metrics_collector)

/* Snapshot current metrics and get the updated metrics_snapshot buffer */
uint32_t test_metrics_flags = (METRICS_V2_KERNEL_COUNTERS | METRICS_V2_LIBBPF_STATS | METRICS_V2_RESOURCE_UTILIZATION | METRICS_V2_STATE_COUNTERS);
std::unique_ptr<libsinsp::metrics::metrics_collector> metrics_collector = libsinsp::metrics::metrics_collector::create(&m_inspector, test_metrics_flags);
bool convert_memory_to_mb = true;
std::unique_ptr<libsinsp::metrics::metrics_collector> metrics_collector = libsinsp::metrics::metrics_collector::create(&m_inspector, test_metrics_flags, convert_memory_to_mb);
metrics_collector->snapshot();
auto metrics_snapshot = metrics_collector->get_metrics();
/* Multiple calls */
metrics_collector->snapshot();
metrics_collector->snapshot();
metrics_snapshot = metrics_collector->get_metrics();
ASSERT_EQ(metrics_snapshot.size(), 28);

/* These names should always be available, note that we currently can't check for the merged scap stats metrics here */
std::unordered_set<std::string> minimal_stats_names = {"cpu_usage_perc", "memory_rss", "open_fds_host", \
std::unordered_set<std::string> minimal_metrics_names = {"cpu_usage_perc", "memory_rss", "open_fds_host", \
"n_threads", "n_fds", "n_added_fds", "n_added_threads", "n_removed_threads", "n_containers"};

for(const auto& stat_name : minimal_stats_names)
for(const auto& metric_name : minimal_metrics_names)
{
uint32_t i = 0;
for (const auto& stat : metrics_snapshot)
for (const auto& metric: metrics_snapshot)
{
if(stat_name.compare(stat.name) == 0)
if(metric_name.compare(metric.name) == 0)
{
break;
}
i++;
}
if(i == metrics_snapshot.size())
{
FAIL() << "unable to find stat '" << stat_name << "' in metrics_snapshot buffer";
FAIL() << "unable to find stat '" << metric_name << "' in metrics_snapshot buffer";
}
}

/* Assert some values are greater than 0 */
ASSERT_GT(metrics_snapshot[SINSP_RESOURCE_UTILIZATION_MEMORY_RSS].value.u32, 0);
ASSERT_GT(metrics_snapshot[SINSP_RESOURCE_UTILIZATION_MEMORY_VSZ].value.u32, 0);
ASSERT_GT(metrics_snapshot[SINSP_RESOURCE_UTILIZATION_CPU_PERC_TOTAL_HOST].value.d, 0);
ASSERT_GT(metrics_snapshot[SINSP_RESOURCE_UTILIZATION_MEMORY_TOTAL_HOST].value.u64, 0);
ASSERT_GT(metrics_snapshot[SINSP_RESOURCE_UTILIZATION_PROCS_HOST].value.u32, 0);
ASSERT_GT(metrics_snapshot[SINSP_RESOURCE_UTILIZATION_FDS_TOTAL_HOST].value.u64, 0);

ASSERT_GT(metrics_snapshot[SINSP_STATS_V2_N_THREADS].value.u64, 0);
ASSERT_GT(metrics_snapshot[SINSP_STATS_V2_N_FDS].value.u64, 0);
ASSERT_GT(metrics_snapshot[SINSP_STATS_V2_ADDED_THREADS].value.u64, 0);
/* Assert successful memory unit changes and sanity check some values to be greater than 0 */
const std::vector<std::string> metrics_names_memory = {"memory_rss", "memory_vsz", "memory_pss", "container_memory_used", "memory_used_host"};
const std::vector<std::string> metrics_names_values_gt = {"n_threads", "n_fds", "n_added_threads", "memory_used_host"};
uint32_t success_memory_cnt = 0;
uint32_t success_values_cnt = 0;
for (const auto& metric: metrics_snapshot)
{
if (std::find(metrics_names_memory.begin(), metrics_names_memory.end(), metric.name) != metrics_names_memory.end())
{
ASSERT_EQ(metric.unit, METRIC_VALUE_UNIT_MEMORY_MEGABYTES);
ASSERT_EQ(metric.type, METRIC_VALUE_TYPE_D);
success_memory_cnt++;
}
if (std::find(metrics_names_values_gt.begin(), metrics_names_values_gt.end(), metric.name) != metrics_names_values_gt.end())
{
ASSERT_GT(metric.value.u64, 0);
success_values_cnt++;
}
}
ASSERT_EQ(success_memory_cnt, metrics_names_memory.size());
ASSERT_EQ(success_values_cnt, metrics_names_values_gt.size());

/* Empty call */
metrics_collector = libsinsp::metrics::metrics_collector::create(&m_inspector, 0);
metrics_collector = libsinsp::metrics::metrics_collector::create(&m_inspector, 0, convert_memory_to_mb);
metrics_collector->snapshot();
metrics_snapshot = metrics_collector->get_metrics();
ASSERT_EQ(metrics_snapshot.size(), 0);

/* Just checking that we don't crash w/ selective flags */
/* Sanity check empty inspector */
test_metrics_flags = (METRICS_V2_RESOURCE_UTILIZATION | METRICS_V2_STATE_COUNTERS);
metrics_collector = libsinsp::metrics::metrics_collector::create(nullptr, test_metrics_flags, convert_memory_to_mb);
metrics_collector->snapshot();
metrics_snapshot = metrics_collector->get_metrics();
ASSERT_EQ(metrics_snapshot.size(), 0);

/* Some sanity checks for selective flags */
test_metrics_flags = 0;
test_metrics_flags |= METRICS_V2_KERNEL_COUNTERS; // 20, but can't test it here it's 0
test_metrics_flags |= METRICS_V2_LIBBPF_STATS; // 21 (x86_64 machine), but can't test it here it's 0
metrics_collector = libsinsp::metrics::metrics_collector::create(&m_inspector, test_metrics_flags);
metrics_collector = libsinsp::metrics::metrics_collector::create(&m_inspector, test_metrics_flags, convert_memory_to_mb);
metrics_collector->snapshot();
metrics_snapshot = metrics_collector->get_metrics();
ASSERT_EQ(metrics_snapshot.size(), 0);

test_metrics_flags = 0;
test_metrics_flags |= METRICS_V2_RESOURCE_UTILIZATION;
metrics_collector = libsinsp::metrics::metrics_collector::create(&m_inspector, test_metrics_flags);
metrics_collector = libsinsp::metrics::metrics_collector::create(&m_inspector, test_metrics_flags, convert_memory_to_mb);
metrics_collector->snapshot();
metrics_snapshot = metrics_collector->get_metrics();
ASSERT_EQ(metrics_snapshot.size(), 9);

test_metrics_flags = 0;
test_metrics_flags |= METRICS_V2_STATE_COUNTERS;
metrics_collector = libsinsp::metrics::metrics_collector::create(&m_inspector, test_metrics_flags);
metrics_collector = libsinsp::metrics::metrics_collector::create(&m_inspector, test_metrics_flags, convert_memory_to_mb);
metrics_collector->snapshot();
metrics_snapshot = metrics_collector->get_metrics();
ASSERT_EQ(metrics_snapshot.size(), 19);

test_metrics_flags = (METRICS_V2_RESOURCE_UTILIZATION | METRICS_V2_STATE_COUNTERS);
metrics_collector = libsinsp::metrics::metrics_collector::create(&m_inspector, test_metrics_flags);
metrics_collector = libsinsp::metrics::metrics_collector::create(&m_inspector, test_metrics_flags, convert_memory_to_mb);
metrics_collector->snapshot();
metrics_snapshot = metrics_collector->get_metrics();
ASSERT_EQ(metrics_snapshot.size(), 28);

/* Check we don't crash if inspector is invalid and verify metrics vector is cleared */
test_metrics_flags = (METRICS_V2_RESOURCE_UTILIZATION | METRICS_V2_STATE_COUNTERS);
metrics_collector = libsinsp::metrics::metrics_collector::create(nullptr, test_metrics_flags);
metrics_collector->snapshot();
metrics_snapshot = metrics_collector->get_metrics();
ASSERT_EQ(metrics_snapshot.size(), 0);
/* Test public convert_memory method */
double converted_memory = metrics_collector->convert_memory(METRIC_VALUE_UNIT_MEMORY_BYTES, METRIC_VALUE_UNIT_MEMORY_MEGABYTES, (uint64_t)52428800);
ASSERT_EQ(converted_memory, 50);
converted_memory = metrics_collector->convert_memory(METRIC_VALUE_UNIT_MEMORY_KILOBYTES, METRIC_VALUE_UNIT_MEMORY_MEGABYTES, (uint64_t)51200);
ASSERT_EQ(converted_memory, 50);
converted_memory = metrics_collector->convert_memory(METRIC_VALUE_UNIT_MEMORY_MEGABYTES, METRIC_VALUE_UNIT_MEMORY_MEGABYTES, (uint64_t)50);
ASSERT_EQ(converted_memory, 50);

}
#endif

0 comments on commit 84c56dc

Please sign in to comment.