@@ -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,35 +171,29 @@ 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" ,
178
- METRICS_V2_RULE_COUNTERS,
179
- METRIC_VALUE_TYPE_U64,
180
- METRIC_VALUE_UNIT_COUNT,
181
- METRIC_VALUE_METRIC_TYPE_MONOTONIC,
182
- rule_stats_manager.get_total ().load ());
183
-
184
- prometheus_metrics_converter.convert_metric_to_unit_convention (metric);
185
- prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus (metric, " falcosecurity" , " falco" );
186
174
const std::vector<std::unique_ptr<std::atomic<uint64_t >>>& rules_by_id = rule_stats_manager.get_by_rule_id ();
175
+ // Distinguish between rules counters using labels, following Prometheus best practices: https://prometheus.io/docs/practices/naming/#labels
187
176
for (size_t i = 0 ; i < rules_by_id.size (); i++)
188
177
{
189
178
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 (),
179
+ auto count = rules_by_id[i]->load ();
180
+ if (count > 0 )
181
+ {
182
+ auto metric = libs_metrics_collector.new_metric (" rules_counters" ,
193
183
METRICS_V2_RULE_COUNTERS,
194
184
METRIC_VALUE_TYPE_U64,
195
185
METRIC_VALUE_UNIT_COUNT,
196
186
METRIC_VALUE_METRIC_TYPE_MONOTONIC,
197
187
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);
188
+ prometheus_metrics_converter.convert_metric_to_unit_convention (metric);
189
+ const std::map<std::string, std::string>& const_labels = {
190
+ {" rule_name" , rule->name },
191
+ {" priority" , std::to_string (rule->priority )},
192
+ {" source" , rule->source },
193
+ {" tags" , concat_set_in_order (rule->tags )}
194
+ };
195
+ prometheus_text += prometheus_metrics_converter.convert_metric_to_text_prometheus (metric, " falcosecurity" , " falco" , const_labels);
196
+ }
206
197
}
207
198
}
208
199
}
0 commit comments