Skip to content

Commit

Permalink
Cache formatters.
Browse files Browse the repository at this point in the history
Instead of creating a formatter for each event, cache them and create
them only when needed. A new function output_cleanup cleans up the
cached formatters, and is called in the destructor if init() was called.
  • Loading branch information
mstemm committed Dec 7, 2016
1 parent 8e2a3ef commit c89c066
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
17 changes: 17 additions & 0 deletions userspace/falco/falco_outputs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,28 @@ along with falco. If not, see <http://www.gnu.org/licenses/>.
using namespace std;

falco_outputs::falco_outputs()
: m_initialized(false)
{

}

falco_outputs::~falco_outputs()
{
if(m_initialized)
{
lua_getglobal(m_ls, m_lua_output_cleanup.c_str());

if(!lua_isfunction(m_ls, -1))
{
throw falco_exception("No function " + m_lua_output_cleanup + " found. ");
}

if(lua_pcall(m_ls, 0, 0, 0) != 0)
{
const char* lerr = lua_tostring(m_ls, -1);
throw falco_exception(string(lerr));
}
}
}

void falco_outputs::init(bool json_output)
Expand All @@ -52,6 +67,8 @@ void falco_outputs::init(bool json_output)
falco_formats::init(m_inspector, m_ls, json_output);

falco_logger::init(m_ls);

m_initialized = true;
}

void falco_outputs::add_output(output_config oc)
Expand Down
3 changes: 3 additions & 0 deletions userspace/falco/falco_outputs.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ class falco_outputs : public falco_common
void handle_event(sinsp_evt *ev, std::string &level, std::string &priority, std::string &format);

private:
bool m_initialized;

std::string m_lua_add_output = "add_output";
std::string m_lua_output_event = "output_event";
std::string m_lua_output_cleanup = "output_cleanup";
std::string m_lua_main_filename = "output.lua";
};
18 changes: 16 additions & 2 deletions userspace/falco/lua/output.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ mod.levels = levels

local outputs = {}

local formatters = {}

function mod.stdout(level, msg)
print (msg)
end
Expand Down Expand Up @@ -75,14 +77,26 @@ end
function output_event(event, rule, priority, format)
local level = level_of(priority)
format = "*%evt.time: "..levels[level+1].." "..format
formatter = formats.formatter(format)
if formatters[rule] == nil then
formatter = formats.formatter(format)
formatters[rule] = formatter
else
formatter = formatters[rule]
end

msg = formats.format_event(event, rule, levels[level+1], formatter)

for index,o in ipairs(outputs) do
o.output(level, msg, o.config)
end
end

function output_cleanup()
for rule, formatter in pairs(formatters) do
formats.free_formatter(formatter)
end

formats.free_formatter(formatter)
formatters = {}
end

function add_output(output_name, config)
Expand Down

0 comments on commit c89c066

Please sign in to comment.