@@ -96,20 +96,17 @@ std::string falco_metrics::to_text(const falco::app::state& state)
96
96
}
97
97
98
98
#if defined(__linux__) and !defined(MINIMAL_BUILD) and !defined(__EMSCRIPTEN__)
99
+ // Distinguish between config and rules files using labels, following Prometheus best practices: https://prometheus.io/docs/practices/naming/#labels
99
100
for (const auto & item : state.config .get ()->m_loaded_rules_filenames_sha256sum )
100
101
{
101
102
fs::path fs_path = item.first ;
102
- std::string metric_name_file_sha256 = fs_path.filename ().stem ();
103
- metric_name_file_sha256 = " falco_sha256_rules_file_" + falco::utils::sanitize_metric_name (metric_name_file_sha256);
104
- prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus (metric_name_file_sha256, " falcosecurity" , " falco" , {{metric_name_file_sha256, item.second }});
103
+ prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus (" falco_sha256_rules_files" , " falcosecurity" , " falco" , {{" file_name" , fs_path.filename ().stem ()}, {" sha256" , item.second }});
105
104
}
106
105
107
106
for (const auto & item : state.config .get ()->m_loaded_configs_filenames_sha256sum )
108
107
{
109
108
fs::path fs_path = item.first ;
110
- std::string metric_name_file_sha256 = fs_path.filename ().stem ();
111
- metric_name_file_sha256 = " falco_sha256_config_file_" + falco::utils::sanitize_metric_name (metric_name_file_sha256);
112
- prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus (metric_name_file_sha256, " falcosecurity" , " falco" , {{metric_name_file_sha256, item.second }});
109
+ prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus (" falco_sha256_config_files" , " falcosecurity" , " falco" , {{" file_name" , fs_path.filename ().stem ()}, {" sha256" , item.second }});
113
110
}
114
111
#endif
115
112
@@ -174,7 +171,7 @@ std::string falco_metrics::to_text(const falco::app::state& state)
174
171
{
175
172
const stats_manager& rule_stats_manager = state.engine ->get_rule_stats_manager ();
176
173
const indexed_vector<falco_rule>& rules = state.engine ->get_rules ();
177
- auto metric = libs_metrics_collector.new_metric (" rules.matches_total " ,
174
+ auto metric = libs_metrics_collector.new_metric (" rules_matches_total " ,
178
175
METRICS_V2_RULE_COUNTERS,
179
176
METRIC_VALUE_TYPE_U64,
180
177
METRIC_VALUE_UNIT_COUNT,
@@ -184,25 +181,28 @@ std::string falco_metrics::to_text(const falco::app::state& state)
184
181
prometheus_metrics_converter.convert_metric_to_unit_convention (metric);
185
182
prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus (metric, " falcosecurity" , " falco" );
186
183
const std::vector<std::unique_ptr<std::atomic<uint64_t >>>& rules_by_id = rule_stats_manager.get_by_rule_id ();
184
+ // Distinguish between rules counters using labels, following Prometheus best practices: https://prometheus.io/docs/practices/naming/#labels
187
185
for (size_t i = 0 ; i < rules_by_id.size (); i++)
188
186
{
189
187
auto rule = rules.at (i);
190
- std::string rules_metric_name = " rules." + falco::utils::sanitize_metric_name (rule->name );
191
- // Separate processing of rules counter metrics given we add extra tags
192
- auto metric = libs_metrics_collector.new_metric (rules_metric_name.c_str (),
188
+ auto count = rules_by_id[i]->load ();
189
+ if (count > 0 )
190
+ {
191
+ auto metric = libs_metrics_collector.new_metric (" rules_counters" ,
193
192
METRICS_V2_RULE_COUNTERS,
194
193
METRIC_VALUE_TYPE_U64,
195
194
METRIC_VALUE_UNIT_COUNT,
196
195
METRIC_VALUE_METRIC_TYPE_MONOTONIC,
197
196
rules_by_id[i]->load ());
198
- prometheus_metrics_converter.convert_metric_to_unit_convention (metric);
199
- const std::map<std::string, std::string>& const_labels = {
200
- {" rule" , rule->name },
201
- {" priority" , std::to_string (rule->priority )},
202
- {" source" , rule->source },
203
- {" tags" , concat_set_in_order (rule->tags )}
204
- };
205
- prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus (metric, " falcosecurity" , " falco" , const_labels);
197
+ prometheus_metrics_converter.convert_metric_to_unit_convention (metric);
198
+ const std::map<std::string, std::string>& const_labels = {
199
+ {" rule_name" , rule->name },
200
+ {" priority" , std::to_string (rule->priority )},
201
+ {" source" , rule->source },
202
+ {" tags" , concat_set_in_order (rule->tags )}
203
+ };
204
+ prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus (metric, " falcosecurity" , " falco" , const_labels);
205
+ }
206
206
}
207
207
}
208
208
}
0 commit comments