Skip to content

Commit

Permalink
new(tests): plugin metrics tests
Browse files Browse the repository at this point in the history
Signed-off-by: Gianmatteo Palmieri <[email protected]>
  • Loading branch information
mrgian committed May 15, 2024
1 parent c85aef5 commit 2ccdd8e
Show file tree
Hide file tree
Showing 3 changed files with 173 additions and 0 deletions.
37 changes: 37 additions & 0 deletions userspace/libsinsp/test/plugins.ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ 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 @@ -890,3 +891,39 @@ 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);
libs::metrics::libs_metrics_collector libs_metrics_collector(&m_inspector, test_metrics_flags);

libs_metrics_collector.snapshot();
auto metrics_snapshot = libs_metrics_collector.get_metrics();
ASSERT_EQ(metrics_snapshot.size(), 0);

register_plugin(&m_inspector, get_plugin_api_sample_metrics);
open_inspector();

libs_metrics_collector.snapshot();
metrics_snapshot = libs_metrics_collector.get_metrics();
ASSERT_EQ(metrics_snapshot.size(), 2);

int events = 256;
for (int i = 0; i < events; i++)
{
add_event_advance_ts(increasing_ts(), 0, PPME_SYSCALL_OPEN_E, 3, "/tmp/the_file", PPM_O_RDWR, 0);
}

libs_metrics_collector.snapshot();
metrics_snapshot = libs_metrics_collector.get_metrics();
ASSERT_EQ(metrics_snapshot.size(), 2);

ASSERT_EQ(std::string(metrics_snapshot.at(0).name), "sample_metrics.dummy_metric");
ASSERT_EQ(std::string(metrics_snapshot.at(1).name), "sample_metrics.evt_count");

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

#endif
135 changes: 135 additions & 0 deletions userspace/libsinsp/test/plugins/metrics.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
// SPDX-License-Identifier: Apache-2.0
/*
Copyright (C) 2024 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <sstream>
#include <iostream>

#include <driver/ppm_events_public.h>
#include "test_plugins.h"

struct plugin_state
{
std::string lasterr;
struct ss_plugin_metric metrics[2];
uint64_t count = 0;
};

static const char* plugin_get_required_api_version()
{
return PLUGIN_API_VERSION_STR;
}

static const char* plugin_get_version()
{
return "0.1.0";
}

static const char* plugin_get_name()
{
return "sample_metrics";
}

static const char* plugin_get_description()
{
return "some desc";
}

static const char* plugin_get_contact()
{
return "some contact";
}

static const char* plugin_get_parse_event_sources()
{
return "[\"syscall\"]";
}

static uint16_t* plugin_get_parse_event_types(uint32_t* num_types, ss_plugin_t* s)
{
static uint16_t types[] = {
PPME_SYSCALL_OPEN_E,
//PPME_SYSCALL_OPEN_X,
};
*num_types = sizeof(types) / sizeof(uint16_t);
return &types[0];
}

static ss_plugin_t* plugin_init(const ss_plugin_init_input* in, ss_plugin_rc* rc)
{
*rc = SS_PLUGIN_SUCCESS;
plugin_state *ret = new plugin_state();

ret->metrics[0].type = SS_PLUGIN_METRIC_TYPE_NON_MONOTONIC;
ret->metrics[0].value_type = SS_PLUGIN_METRIC_VALUE_TYPE_U64;
ret->metrics[0].value.u64 = 1234;
ret->metrics[0].name = "dummy_metric";

ret->metrics[1].type = SS_PLUGIN_METRIC_TYPE_MONOTONIC;
ret->metrics[1].value_type = SS_PLUGIN_METRIC_VALUE_TYPE_U64;
ret->metrics[1].value.u64 = 0;
ret->metrics[1].name = "evt_count";

return ret;
}

static void plugin_destroy(ss_plugin_t* s)
{
delete ((plugin_state *) s);
}

static const char* plugin_get_last_error(ss_plugin_t* s)
{
return ((plugin_state *) s)->lasterr.c_str();
}

static ss_plugin_rc plugin_parse_event(ss_plugin_t *s, const ss_plugin_event_input *ev, const ss_plugin_event_parse_input* in)
{
plugin_state *ps = (plugin_state *) s;
ps->count++;
ps->metrics[1].value.u64 = ps->count;

return SS_PLUGIN_SUCCESS;
}

static ss_plugin_metric* plugin_get_metrics(ss_plugin_t *s, uint32_t *num_metrics)
{
plugin_state *ps = (plugin_state *) s;

*num_metrics = sizeof(ps->metrics) / sizeof(ss_plugin_metric);

return ps->metrics;
}

void get_plugin_api_sample_metrics(plugin_api& out)
{
memset(&out, 0, sizeof(plugin_api));
out.get_required_api_version = plugin_get_required_api_version;
out.get_version = plugin_get_version;
out.get_description = plugin_get_description;
out.get_contact = plugin_get_contact;
out.get_name = plugin_get_name;
out.get_last_error = plugin_get_last_error;
out.init = plugin_init;
out.destroy = plugin_destroy;
out.get_parse_event_sources = plugin_get_parse_event_sources;
out.get_parse_event_types = plugin_get_parse_event_types;
out.parse_event = plugin_parse_event;
out.get_metrics = plugin_get_metrics;
}
1 change: 1 addition & 0 deletions userspace/libsinsp/test/plugins/test_plugins.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ void get_plugin_api_sample_plugin_extract(plugin_api& out);
void get_plugin_api_sample_syscall_tables(plugin_api& out);
void get_plugin_api_sample_syscall_subtables(plugin_api& out);
void get_plugin_api_sample_syscall_subtables_array(plugin_api& out);
void get_plugin_api_sample_metrics(plugin_api& out);

0 comments on commit 2ccdd8e

Please sign in to comment.