mirror of
https://github.com/falcosecurity/falco.git
synced 2025-08-18 06:07:18 +00:00
clenaup: add sanitize_metric_name helper
Signed-off-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
This commit is contained in:
parent
e9afe24e17
commit
c15a309781
@ -151,6 +151,18 @@ std::string calculate_file_sha256sum(const std::string& filename)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
std::string sanitize_metric_name(const std::string& name)
|
||||||
|
{
|
||||||
|
std::string sanitized_name = name;
|
||||||
|
RE2::GlobalReplace(&sanitized_name, "[^a-zA-Z0-9_:]", "_");
|
||||||
|
RE2::GlobalReplace(&sanitized_name, "_+", "_");
|
||||||
|
if (!sanitized_name.empty() && sanitized_name.back() == '_')
|
||||||
|
{
|
||||||
|
sanitized_name.pop_back();
|
||||||
|
}
|
||||||
|
return sanitized_name;
|
||||||
|
}
|
||||||
|
|
||||||
std::string wrap_text(const std::string& in, uint32_t indent, uint32_t line_len)
|
std::string wrap_text(const std::string& in, uint32_t indent, uint32_t line_len)
|
||||||
{
|
{
|
||||||
std::istringstream is(in);
|
std::istringstream is(in);
|
||||||
|
@ -31,6 +31,8 @@ uint64_t parse_prometheus_interval(std::string interval_str);
|
|||||||
std::string calculate_file_sha256sum(const std::string& filename);
|
std::string calculate_file_sha256sum(const std::string& filename);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
std::string sanitize_metric_name(const std::string& name);
|
||||||
|
|
||||||
std::string wrap_text(const std::string& in, uint32_t indent, uint32_t linelen);
|
std::string wrap_text(const std::string& in, uint32_t indent, uint32_t linelen);
|
||||||
|
|
||||||
void readfile(const std::string& filename, std::string& data);
|
void readfile(const std::string& filename, std::string& data);
|
||||||
|
@ -17,6 +17,8 @@ limitations under the License.
|
|||||||
|
|
||||||
#include "falco_metrics.h"
|
#include "falco_metrics.h"
|
||||||
|
|
||||||
|
#include "falco_utils.h"
|
||||||
|
|
||||||
#include "app/state.h"
|
#include "app/state.h"
|
||||||
|
|
||||||
#include <libsinsp/sinsp.h>
|
#include <libsinsp/sinsp.h>
|
||||||
@ -91,7 +93,7 @@ std::string falco_metrics::to_text(const falco::app::state& state)
|
|||||||
{
|
{
|
||||||
fs::path fs_path = item.first;
|
fs::path fs_path = item.first;
|
||||||
std::string metric_name_file_sha256 = fs_path.filename().stem();
|
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}});
|
prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus(metric_name_file_sha256, "falcosecurity", "falco", {{metric_name_file_sha256, item.second}});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,7 +101,7 @@ std::string falco_metrics::to_text(const falco::app::state& state)
|
|||||||
{
|
{
|
||||||
fs::path fs_path = item.first;
|
fs::path fs_path = item.first;
|
||||||
std::string metric_name_file_sha256 = fs_path.filename().stem();
|
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}});
|
prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus(metric_name_file_sha256, "falcosecurity", "falco", {{metric_name_file_sha256, item.second}});
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -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++)
|
for (size_t i = 0; i < rules_by_id.size(); i++)
|
||||||
{
|
{
|
||||||
auto rule = rules.at(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
|
// Separate processing of rules counter metrics given we add extra tags
|
||||||
auto metric = libs_metrics_collector.new_metric(rules_metric_name.c_str(),
|
auto metric = libs_metrics_collector.new_metric(rules_metric_name.c_str(),
|
||||||
METRICS_V2_RULE_COUNTERS,
|
METRICS_V2_RULE_COUNTERS,
|
||||||
|
@ -28,6 +28,7 @@ limitations under the License.
|
|||||||
#include "stats_writer.h"
|
#include "stats_writer.h"
|
||||||
#include "logger.h"
|
#include "logger.h"
|
||||||
#include "config_falco.h"
|
#include "config_falco.h"
|
||||||
|
#include "falco_utils.h"
|
||||||
#include <libscap/strl.h>
|
#include <libscap/strl.h>
|
||||||
#include <libscap/scap_vtable.h>
|
#include <libscap/scap_vtable.h>
|
||||||
|
|
||||||
@ -339,7 +340,7 @@ void stats_writer::collector::get_metrics_output_fields_wrapper(
|
|||||||
{
|
{
|
||||||
fs::path fs_path = item.first;
|
fs::path fs_path = item.first;
|
||||||
std::string metric_name_file_sha256 = fs_path.filename().stem();
|
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;
|
output_fields[metric_name_file_sha256] = item.second;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -347,7 +348,7 @@ void stats_writer::collector::get_metrics_output_fields_wrapper(
|
|||||||
{
|
{
|
||||||
fs::path fs_path = item.first;
|
fs::path fs_path = item.first;
|
||||||
std::string metric_name_file_sha256 = fs_path.filename().stem();
|
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;
|
output_fields[metric_name_file_sha256] = item.second;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
@ -393,7 +394,7 @@ void stats_writer::collector::get_metrics_output_fields_additional(
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
auto rule = rules.at(i);
|
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;
|
output_fields[rules_metric_name] = rule_count;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user