Skip to content

Commit

Permalink
cleanup(outputs): adopt different style for outputs_queue params enco…
Browse files Browse the repository at this point in the history
…dings

Co-authored-by: Leonardo Grasso <me@leonardograsso.com>
Signed-off-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
  • Loading branch information
incertum and leogr committed Aug 3, 2023
1 parent 2c20831 commit 3139ad2
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 31 deletions.
19 changes: 19 additions & 0 deletions userspace/engine/falco_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ static std::vector<std::string> priority_names = {
"Debug"
};

static std::vector<std::string> outputs_recovery_names = {
"continue",
"exit",
"empty",
};

bool falco_common::parse_priority(std::string v, priority_type& out)
{
for (size_t i = 0; i < priority_names.size(); i++)
Expand Down Expand Up @@ -54,6 +60,19 @@ falco_common::priority_type falco_common::parse_priority(std::string v)
return out;
}

bool falco_common::parse_recovery(std::string v, outputs_recovery_type& out)
{
for (size_t i = 0; i < outputs_recovery_names.size(); i++)
{
if (!strcasecmp(v.c_str(), outputs_recovery_names[i].c_str()))
{
out = (outputs_recovery_type) i;
return true;
}
}
return false;
}

bool falco_common::format_priority(priority_type v, std::string& out, bool shortfmt)
{
if ((size_t) v < priority_names.size())
Expand Down
10 changes: 10 additions & 0 deletions userspace/engine/falco_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ limitations under the License.
#include <mutex>
#include <sinsp.h>

#define DEFAULT_OUTPUTS_QUEUE_CAPACITY 0

//
// Most falco_* classes can throw exceptions. Unless directly related
// to low-level failures like inability to open file, etc, they will
Expand Down Expand Up @@ -52,6 +54,13 @@ struct falco_exception : std::exception

namespace falco_common
{

enum outputs_recovery_type {
RECOVERY_CONTINUE = 0, /* queue_capacity_outputs recovery strategy of continuing on. */
RECOVERY_EXIT = 1, /* queue_capacity_outputs recovery strategy of exiting, self OOM kill. */
RECOVERY_EMPTY = 2, /* queue_capacity_outputs recovery strategy of emptying queue then continuing. */
};

const std::string syscall_source = sinsp_syscall_event_source_name;

// Same as numbers/indices into the above vector
Expand All @@ -69,6 +78,7 @@ namespace falco_common

bool parse_priority(std::string v, priority_type& out);
priority_type parse_priority(std::string v);
bool parse_recovery(std::string v, outputs_recovery_type& out);
bool format_priority(priority_type v, std::string& out, bool shortfmt=false);
std::string format_priority(priority_type v, bool shortfmt=false);
};
10 changes: 7 additions & 3 deletions userspace/falco/configuration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ limitations under the License.
#include "falco_utils.h"

#include "configuration.h"
#include "configuration_aux.h"
#include "logger.h"
#include "banned.h" // This raises a compilation error when certain functions are used

Expand All @@ -41,7 +40,7 @@ falco_configuration::falco_configuration():
m_watch_config_files(true),
m_buffered_outputs(false),
m_outputs_queue_capacity(DEFAULT_OUTPUTS_QUEUE_CAPACITY),
m_outputs_queue_recovery(RECOVERY_EXIT),
m_outputs_queue_recovery(falco_common::RECOVERY_EXIT),
m_time_format_iso_8601(false),
m_output_timeout(2000),
m_grpc_enabled(false),
Expand Down Expand Up @@ -255,7 +254,12 @@ void falco_configuration::load_yaml(const std::string& config_name, const yaml_h

m_buffered_outputs = config.get_scalar<bool>("buffered_outputs", false);
m_outputs_queue_capacity = config.get_scalar<size_t>("outputs_queue.capacity", DEFAULT_OUTPUTS_QUEUE_CAPACITY);
m_outputs_queue_recovery = config.get_scalar<uint32_t>("outputs_queue.recovery", RECOVERY_EXIT);
std::string recovery = config.get_scalar<std::string>("outputs_queue.recovery", "exit");
if (!falco_common::parse_recovery(recovery, m_outputs_queue_recovery))
{
throw std::logic_error("Unknown recovery \"" + recovery + "\"--must be one of exit, continue, empty");
}

m_time_format_iso_8601 = config.get_scalar<bool>("time_format_iso_8601", false);

falco_logger::log_stderr = config.get_scalar<bool>("log_stderr", false);
Expand Down
2 changes: 1 addition & 1 deletion userspace/falco/configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class falco_configuration
bool m_watch_config_files;
bool m_buffered_outputs;
size_t m_outputs_queue_capacity;
uint32_t m_outputs_queue_recovery;
falco_common::outputs_recovery_type m_outputs_queue_recovery;
bool m_time_format_iso_8601;
uint32_t m_output_timeout;

Expand Down
22 changes: 0 additions & 22 deletions userspace/falco/configuration_aux.h

This file was deleted.

8 changes: 4 additions & 4 deletions userspace/falco/falco_outputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ limitations under the License.

#include "falco_outputs.h"
#include "config_falco.h"
#include "configuration_aux.h"

#include "formats.h"
#include "logger.h"
Expand Down Expand Up @@ -48,7 +47,7 @@ falco_outputs::falco_outputs(
uint32_t timeout,
bool buffered,
size_t outputs_queue_capacity,
uint32_t outputs_queue_recovery,
falco_common::outputs_recovery_type outputs_queue_recovery,
bool time_format_iso_8601,
const std::string& hostname)
{
Expand Down Expand Up @@ -278,12 +277,13 @@ inline void falco_outputs::push(const ctrl_msg& cmsg)
{
switch (m_recovery)
{
case RECOVERY_EXIT:
case falco_common::RECOVERY_EXIT:
fprintf(stderr, "Fatal error: Output queue out of memory. Exiting ... \n");
exit(EXIT_FAILURE);
case RECOVERY_EMPTY:
case falco_common::RECOVERY_EMPTY:
fprintf(stderr, "Output queue out of memory. Empty queue and continue ... \n");
m_queue.empty();
break;
default:
fprintf(stderr, "Output queue out of memory. Continue on ... \n");
break;
Expand Down
2 changes: 1 addition & 1 deletion userspace/falco/falco_outputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class falco_outputs
uint32_t timeout,
bool buffered,
size_t outputs_queue_capacity,
uint32_t outputs_queue_recovery,
falco_common::outputs_recovery_type outputs_queue_recovery,
bool time_format_iso_8601,
const std::string& hostname);

Expand Down

0 comments on commit 3139ad2

Please sign in to comment.