Skip to content

Commit

Permalink
new(libsinsp): plugin metric to sinsp metric conversion
Browse files Browse the repository at this point in the history
Signed-off-by: Gianmatteo Palmieri <[email protected]>
  • Loading branch information
mrgian authored and poiana committed May 16, 2024
1 parent 8a860c1 commit b1aea19
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 1 deletion.
1 change: 1 addition & 0 deletions userspace/libscap/metrics_v2.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ extern "C" {
#define METRICS_V2_STATE_COUNTERS (1 << 3)
#define METRICS_V2_RULE_COUNTERS (1 << 4)
#define METRICS_V2_MISC (1 << 5)
#define METRICS_V2_PLUGINS (1 << 6)

typedef union metrics_v2_value {
uint32_t u32;
Expand Down
14 changes: 14 additions & 0 deletions userspace/libsinsp/metrics_collector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ limitations under the License.

#include <libsinsp/sinsp_int.h>
#include <libsinsp/metrics_collector.h>
#include <libsinsp/plugin_manager.h>
#include <cmath>
#include <sys/times.h>
#include <sys/stat.h>
Expand Down Expand Up @@ -456,6 +457,19 @@ void libs_metrics_collector::snapshot()
return;
}

/*
* plugins metrics
*/

if(m_metrics_flags & METRICS_V2_PLUGINS)
{
for (auto& p : m_inspector->get_plugin_manager()->plugins())
{
std::vector<metrics_v2> plugin_metrics = p->get_metrics();
m_metrics.insert(m_metrics.end(), plugin_metrics.begin(), plugin_metrics.end());
}
}

/*
* libscap metrics
*/
Expand Down
2 changes: 1 addition & 1 deletion userspace/libsinsp/metrics_collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ class libs_metrics_collector

private:
sinsp* m_inspector;
uint32_t m_metrics_flags = METRICS_V2_KERNEL_COUNTERS | METRICS_V2_LIBBPF_STATS | METRICS_V2_RESOURCE_UTILIZATION | METRICS_V2_STATE_COUNTERS;
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;

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);
Expand Down
71 changes: 71 additions & 0 deletions userspace/libsinsp/plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -886,6 +886,77 @@ std::vector<sinsp_plugin::open_param> sinsp_plugin::list_open_params() const
return list;
}

static void set_plugin_metric_value(metrics_v2& metric, metrics_v2_value_type type, ss_plugin_metric_value val)
{
switch (type)
{
case METRIC_VALUE_TYPE_U32:
metric.value.u32 = val.u32;
break;
case METRIC_VALUE_TYPE_S32:
metric.value.s32 = val.s32;
break;
case METRIC_VALUE_TYPE_U64:
metric.value.u64 = val.u64;
break;
case METRIC_VALUE_TYPE_S64:
metric.value.s64 = val.s64;
break;
case METRIC_VALUE_TYPE_D:
metric.value.d = val.d;
break;
case METRIC_VALUE_TYPE_F:
metric.value.f = val.f;
break;
case METRIC_VALUE_TYPE_I:
metric.value.i = val.i;
break;
default:
break;
}
}

std::vector<metrics_v2> sinsp_plugin::get_metrics() const
{
if(!m_inited)
{
throw sinsp_exception(std::string(s_not_init_err) + ": " + m_name);
}

std::vector<metrics_v2> metrics;
uint32_t num_metrics = 0;

if(!m_handle->api.get_metrics)
{
return metrics;
}

ss_plugin_metric *plugin_metrics = m_handle->api.get_metrics(m_state, &num_metrics);
for (uint32_t i = 0; i < num_metrics; i++)
{
ss_plugin_metric *plugin_metric = plugin_metrics + i;

metrics_v2 metric;

//copy plugin name
int s = strlcpy(metric.name, m_name.c_str(), METRIC_NAME_MAX);
//copy dot
strlcpy(metric.name + s, ".", METRIC_NAME_MAX);
//copy metric name
strlcpy(metric.name + s + 1, plugin_metric->name, METRIC_NAME_MAX);

metric.flags = METRICS_V2_PLUGINS;
metric.unit = METRIC_VALUE_UNIT_COUNT;
metric.type = static_cast<metrics_v2_value_type>(plugin_metric->value_type);
metric.metric_type = static_cast<metrics_v2_metric_type>(plugin_metric->type);
set_plugin_metric_value(metric, metric.type, plugin_metric->value);

metrics.emplace_back(metric);
}

return metrics;
}

/** End of Event Source CAP **/

/** Field Extraction CAP **/
Expand Down
1 change: 1 addition & 0 deletions userspace/libsinsp/plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ class sinsp_plugin
std::string get_last_error() const;
std::string get_init_schema(ss_plugin_schema_type& schema_type) const;
bool set_config(const std::string& config);
std::vector<metrics_v2> get_metrics() const;

/** Event Sourcing **/
inline uint32_t id() const
Expand Down

0 comments on commit b1aea19

Please sign in to comment.