Skip to content

Commit

Permalink
clenaup: add sanitize_metric_name helper
Browse files Browse the repository at this point in the history
Signed-off-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
  • Loading branch information
incertum committed May 17, 2024
1 parent ea01751 commit 0b03b07
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
7 changes: 7 additions & 0 deletions userspace/engine/falco_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,13 @@ std::string calculate_file_sha256sum(const std::string& filename)
}
#endif

std::string sanitize_metric_name(const std::string& name)
{
std::string sanitized_name = name;
RE2::GlobalReplace(&sanitized_name, "\\s+", "_");
return sanitized_name;
}

std::string wrap_text(const std::string& in, uint32_t indent, uint32_t line_len)
{
std::istringstream is(in);
Expand Down
2 changes: 2 additions & 0 deletions userspace/engine/falco_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ uint64_t parse_prometheus_interval(std::string interval_str);
std::string calculate_file_sha256sum(const std::string& filename);
#endif

std::string sanitize_metric_name(const std::string& name);

std::string wrap_text(const std::string& in, uint32_t indent, uint32_t linelen);

void readfile(const std::string& filename, std::string& data);
Expand Down
8 changes: 5 additions & 3 deletions userspace/falco/falco_metrics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ limitations under the License.

#include "falco_metrics.h"

#include "falco_utils.h"

#include "app/state.h"

#include <libsinsp/sinsp.h>
Expand Down Expand Up @@ -91,15 +93,15 @@ std::string falco_metrics::to_text(const falco::app::state& state)
{
fs::path fs_path = item.first;
std::string metric_name_file_sha256 = fs_path.filename().stem();
metric_name_file_sha256 = "falco.sha256_rule_file." + metric_name_file_sha256;
metric_name_file_sha256 = "falco.sha256_rule_file." + falco::utils::sanitize_metric_name(metric_name_file_sha256);
prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus(metric_name_file_sha256, "falcosecurity", "falco", {{metric_name_file_sha256, item.second}});
}

for (const auto& item : state.config.get()->m_loaded_configs_filenames_sha256sum)
{
fs::path fs_path = item.first;
std::string metric_name_file_sha256 = fs_path.filename().stem();
metric_name_file_sha256 = "falco.sha256_config_file." + metric_name_file_sha256;
metric_name_file_sha256 = "falco.sha256_config_file." + falco::utils::sanitize_metric_name(metric_name_file_sha256);
prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus(metric_name_file_sha256, "falcosecurity", "falco", {{metric_name_file_sha256, item.second}});
}
#endif
Expand Down Expand Up @@ -170,7 +172,7 @@ std::string falco_metrics::to_text(const falco::app::state& state)
for (size_t i = 0; i < rules_by_id.size(); i++)
{
auto rule = rules.at(i);
std::string rules_metric_name = "rules." + rule->name;
std::string rules_metric_name = "rules." + falco::utils::sanitize_metric_name(rule->name);
// Separate processing of rules counter metrics given we add extra tags
auto metric = libs_metrics_collector.new_metric(rules_metric_name.c_str(),
METRICS_V2_RULE_COUNTERS,
Expand Down
7 changes: 4 additions & 3 deletions userspace/falco/stats_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ limitations under the License.
#include "stats_writer.h"
#include "logger.h"
#include "config_falco.h"
#include "falco_utils.h"
#include <libscap/strl.h>
#include <libscap/scap_vtable.h>

Expand Down Expand Up @@ -339,15 +340,15 @@ void stats_writer::collector::get_metrics_output_fields_wrapper(
{
fs::path fs_path = item.first;
std::string metric_name_file_sha256 = fs_path.filename().stem();
metric_name_file_sha256 = "falco.sha256_rule_file." + metric_name_file_sha256;
metric_name_file_sha256 = "falco.sha256_rule_file." + falco::utils::sanitize_metric_name(metric_name_file_sha256);
output_fields[metric_name_file_sha256] = item.second;
}

for (const auto& item : m_writer->m_config->m_loaded_configs_filenames_sha256sum)
{
fs::path fs_path = item.first;
std::string metric_name_file_sha256 = fs_path.filename().stem();
metric_name_file_sha256 = "falco.sha256_config_file." + metric_name_file_sha256;
metric_name_file_sha256 = "falco.sha256_config_file." + falco::utils::sanitize_metric_name(metric_name_file_sha256);
output_fields[metric_name_file_sha256] = item.second;
}
#endif
Expand Down Expand Up @@ -393,7 +394,7 @@ void stats_writer::collector::get_metrics_output_fields_additional(
continue;
}
auto rule = rules.at(i);
std::string rules_metric_name = "falco.rules." + rule->name;
std::string rules_metric_name = "falco.rules." + falco::utils::sanitize_metric_name(rule->name);
output_fields[rules_metric_name] = rule_count;
}
}
Expand Down

0 comments on commit 0b03b07

Please sign in to comment.