Skip to content

Commit

Permalink
fix(libsinsp): enable metrics collector on all platforms
Browse files Browse the repository at this point in the history
Signed-off-by: Gianmatteo Palmieri <mail@gian.im>
  • Loading branch information
mrgian committed May 16, 2024
1 parent 0c3e799 commit 7b2e258
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 28 deletions.
42 changes: 23 additions & 19 deletions userspace/libsinsp/metrics_collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,16 @@ limitations under the License.
*/

#ifdef __linux__

#include <libsinsp/sinsp_int.h>
#include <libsinsp/metrics_collector.h>
#include <libsinsp/plugin_manager.h>
#include <cmath>
#include <re2/re2.h>

#ifdef __linux__
#include <sys/times.h>
#include <sys/stat.h>
#include <re2/re2.h>
#endif

static re2::RE2 s_libs_metrics_units_suffix_pre_prometheus_text_conversion("(_kb|_bytes|_mb|_perc|_percentage|_ratio|_ns|_ts|_sec|_total)", re2::RE2::POSIX);
static re2::RE2 s_libs_metrics_units_memory_suffix("(_kb|_bytes)", re2::RE2::POSIX);
Expand All @@ -34,26 +35,26 @@ static re2::RE2 s_libs_metrics_banned_prometheus_naming_characters("(\\.)", re2:
// For simplicity, needs to stay in sync w/ typedef enum metrics_v2_value_unit
// https://prometheus.io/docs/practices/naming/ or https://prometheus.io/docs/practices/naming/#base-units.
static const char *const metrics_unit_name_mappings_prometheus[] = {
[METRIC_VALUE_UNIT_COUNT] = "total",
[METRIC_VALUE_UNIT_RATIO] = "ratio",
[METRIC_VALUE_UNIT_PERC] = "percentage",
[METRIC_VALUE_UNIT_MEMORY_BYTES] = "bytes",
[METRIC_VALUE_UNIT_MEMORY_KIBIBYTES] = "kibibytes",
[METRIC_VALUE_UNIT_MEMORY_MEGABYTES] = "megabytes",
[METRIC_VALUE_UNIT_TIME_NS] = "nanoseconds",
[METRIC_VALUE_UNIT_TIME_S] = "seconds",
[METRIC_VALUE_UNIT_TIME_NS_COUNT] = "nanoseconds_total",
[METRIC_VALUE_UNIT_TIME_S_COUNT] = "seconds_total",
[METRIC_VALUE_UNIT_TIME_TIMESTAMP_NS] = "timestamp_nanoseconds",
"total",
"ratio",
"percentage",
"bytes",
"kibibytes",
"megabytes",
"nanoseconds",
"seconds",
"nanoseconds_total",
"seconds_total",
"timestamp_nanoseconds",
};

static_assert(sizeof(metrics_unit_name_mappings_prometheus) / sizeof(metrics_unit_name_mappings_prometheus[0]) == METRIC_VALUE_UNIT_MAX, "metrics_unit_name_mappings_prometheus array size does not match expected size");

// For simplicity, needs to stay in sync w/ typedef enum metrics_v2_metric_type
// https://github.com/prometheus/docs/blob/main/content/docs/instrumenting/exposition_formats.md
static const char *const metrics_metric_type_name_mappings_prometheus[] = {
[METRIC_VALUE_METRIC_TYPE_MONOTONIC] = "counter",
[METRIC_VALUE_METRIC_TYPE_NON_MONOTONIC_CURRENT] = "gauge",
"counter",
"gauge",
};

namespace libs::metrics {
Expand Down Expand Up @@ -207,6 +208,8 @@ void prometheus_metrics_converter::convert_metric_to_unit_convention(metrics_v2&
}
}

#ifdef __linux__

void libs_metrics_collector::get_rss_vsz_pss_total_memory_and_open_fds(uint32_t &rss, uint32_t &vsz, uint32_t &pss, uint64_t &host_memory_used, uint64_t &host_open_fds)
{
FILE* f;
Expand Down Expand Up @@ -449,6 +452,8 @@ uint64_t libs_metrics_collector::get_container_memory_used() const
return memory_used;
}

#endif /*__linux__*/

void libs_metrics_collector::snapshot()
{
m_metrics.clear();
Expand All @@ -470,6 +475,7 @@ void libs_metrics_collector::snapshot()
}
}

#ifdef __linux__
/*
* libscap metrics
*/
Expand Down Expand Up @@ -729,7 +735,6 @@ void libs_metrics_collector::snapshot()

static_assert(sizeof(sinsp_stats_v2_collectors) / sizeof(sinsp_stats_v2_collectors[0]) == SINSP_MAX_STATS_V2, "sinsp_stats_v2_resource_utilization_names array size does not match expected size");


/*
* libsinsp metrics
*/
Expand Down Expand Up @@ -783,6 +788,7 @@ void libs_metrics_collector::snapshot()
}
}
}
#endif /*__linux__*/
}

const std::vector<metrics_v2>& libs_metrics_collector::get_metrics() const
Expand All @@ -802,5 +808,3 @@ libs_metrics_collector::libs_metrics_collector(sinsp* inspector, uint32_t flags)
}

} // namespace libs::metrics

#endif
6 changes: 2 additions & 4 deletions userspace/libsinsp/metrics_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,6 @@ enum sinsp_stats_v2_resource_utilization
SINSP_MAX_STATS_V2
};

#ifdef __linux__

namespace libs::metrics
{

Expand Down Expand Up @@ -274,9 +272,11 @@ class libs_metrics_collector
uint32_t m_metrics_flags = METRICS_V2_KERNEL_COUNTERS | METRICS_V2_LIBBPF_STATS | METRICS_V2_RESOURCE_UTILIZATION | METRICS_V2_STATE_COUNTERS | METRICS_V2_PLUGINS;
std::vector<metrics_v2> m_metrics;

#ifdef __linux__
void get_rss_vsz_pss_total_memory_and_open_fds(uint32_t &rss, uint32_t &vsz, uint32_t &pss, uint64_t &host_memory_used, uint64_t &host_open_fds);
void get_cpu_usage_and_total_procs(double start_time, double &cpu_usage_perc, double &host_cpu_usage_perc, uint32_t &host_procs_running);
uint64_t get_container_memory_used() const;
#endif /*__linux__*/

template <typename T>
static void set_metric_value(metrics_v2& metric, metrics_v2_value_type type, T val)
Expand Down Expand Up @@ -311,5 +311,3 @@ class libs_metrics_collector
};

} // namespace libs::metrics

#endif
5 changes: 0 additions & 5 deletions userspace/libsinsp/test/plugins.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ limitations under the License.

#include <gtest/gtest.h>
#include <libsinsp/plugin.h>
#include <libsinsp/test/helpers/threads_helpers.h>

#include <sinsp_with_test_input.h>
#include "test_utils.h"
Expand Down Expand Up @@ -892,8 +891,6 @@ TEST(sinsp_plugin, plugin_set_config)
libsinsp_logger()->remove_callback_log();
}

#ifdef __linux__

TEST_F(sinsp_with_test_input, plugin_metrics)
{
uint32_t test_metrics_flags = (METRICS_V2_PLUGINS);
Expand Down Expand Up @@ -925,5 +922,3 @@ TEST_F(sinsp_with_test_input, plugin_metrics)

ASSERT_EQ(metrics_snapshot.back().value.u64, events);
}

#endif

0 comments on commit 7b2e258

Please sign in to comment.