Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 39 additions & 3 deletions plugins/in_podman_metrics/podman_metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <fluent-bit/flb_metrics.h>
#include <fluent-bit/flb_metrics_exporter.h>
#include <fluent-bit/flb_jsmn.h>
#include <fluent-bit/flb_regex.h>

#include <monkey/mk_core/mk_list.h>

Expand Down Expand Up @@ -119,13 +120,26 @@ static int collect_container_data(struct flb_in_metrics *ctx)
image_name[metadata_token_size] = '\0';

flb_plg_trace(ctx->ins, "Found image name %s", image_name);
add_container_to_list(ctx, id, name, image_name);
}
else {
flb_plg_warn(ctx->ins, "Image name was not found for %s", id);
add_container_to_list(ctx, id, name, "unknown");
strncpy(image_name, "unknown", IMAGE_NAME_SIZE - 1);
image_name[sizeof("unknown") - 1] = '\0';
}

/* Optionally skip containers whose name matches the
* user-provided exclude regex (e.g. pod infra/service
* containers that carry no useful application metrics). */
if (ctx->exclude_name_regex &&
flb_regex_match(ctx->exclude_name_regex,
(unsigned char *) name, strlen(name)) > 0) {
flb_plg_debug(ctx->ins,
"Skipping excluded container %s", name);
}
else {
add_container_to_list(ctx, id, name, image_name);
collected_containers++;
}
collected_containers++;
}
}
}
Expand Down Expand Up @@ -427,16 +441,32 @@ static int in_metrics_init(struct flb_input_instance *in, struct flb_config *con
ctx->rx_errors = NULL;
ctx->tx_bytes = NULL;
ctx->tx_errors = NULL;
ctx->exclude_name_regex = NULL;

if (flb_input_config_map_set(in, (void *) ctx) == -1) {
flb_free(ctx);
return -1;
}

if (ctx->exclude_name_regex_text) {
ctx->exclude_name_regex =
flb_regex_create(ctx->exclude_name_regex_text);
if (!ctx->exclude_name_regex) {
flb_plg_error(ctx->ins,
"could not compile exclude_name_regex '%s'",
ctx->exclude_name_regex_text);
flb_free(ctx);
return -1;
}
}

flb_input_set_context(in, ctx);
coll_fd_runtime = flb_input_set_collector_time(in, cb_metrics_collect_runtime, ctx->scrape_interval, 0, config);
if (coll_fd_runtime == -1) {
flb_plg_error(ctx->ins, "Could not set collector for podman metrics plugin");
if (ctx->exclude_name_regex) {
flb_regex_destroy(ctx->exclude_name_regex);
}
return -1;
Comment on lines 463 to 470

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor | ⚡ Quick win

Free plugin context on collector setup failure.

Line 465-470 returns after destroying the compiled regex, but ctx is not released on this error path. That makes init cleanup inconsistent with other failure branches and can leak initialization state.

Suggested fix
-    flb_input_set_context(in, ctx);
     coll_fd_runtime = flb_input_set_collector_time(in, cb_metrics_collect_runtime, ctx->scrape_interval, 0, config);
     if (coll_fd_runtime == -1) {
         flb_plg_error(ctx->ins, "Could not set collector for podman metrics plugin");
         if (ctx->exclude_name_regex) {
             flb_regex_destroy(ctx->exclude_name_regex);
         }
+        flb_free(ctx);
         return -1;
     }
     ctx->coll_fd_runtime = coll_fd_runtime;
+    flb_input_set_context(in, ctx);
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@plugins/in_podman_metrics/podman_metrics.c` around lines 463 - 470, The error
handling path for the collector setup failure (when coll_fd_runtime equals -1)
destroys the exclude_name_regex but fails to free the ctx structure itself
before returning. Add a call to free the ctx structure after destroying the
regex to ensure proper cleanup and prevent memory leaks. Check other error paths
in the same function to match the cleanup pattern used elsewhere when releasing
ctx on initialization failures.

}
ctx->coll_fd_runtime = coll_fd_runtime;
Expand Down Expand Up @@ -468,6 +498,9 @@ static int in_metrics_init(struct flb_input_instance *in, struct flb_config *con
flb_plg_error(ctx->ins, "Could not start collector for podman metrics plugin");
flb_sds_destroy(ctx->config);
destroy_container_list(ctx);
if (ctx->exclude_name_regex) {
flb_regex_destroy(ctx->exclude_name_regex);
}
flb_free(ctx);
return -1;
}
Expand All @@ -491,6 +524,9 @@ static int in_metrics_exit(void *data, struct flb_config *config)

flb_sds_destroy(ctx->config);
destroy_container_list(ctx);
if (ctx->exclude_name_regex) {
flb_regex_destroy(ctx->exclude_name_regex);
}
flb_free(ctx);
return 0;
}
Expand Down
6 changes: 6 additions & 0 deletions plugins/in_podman_metrics/podman_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ static struct flb_config_map config_map[] = {
0, FLB_TRUE, offsetof(struct flb_in_metrics, podman_config_path),
"Path to podman config file"
},
{
FLB_CONFIG_MAP_STR, "exclude_name_regex", NULL,
0, FLB_TRUE, offsetof(struct flb_in_metrics, exclude_name_regex_text),
"Exclude containers whose name matches this regular expression, e.g. "
"'-(infra|service)$' to skip pod infra and service containers"
},
{
FLB_CONFIG_MAP_STR, "path.sysfs", SYSFS_PATH,
0, FLB_TRUE, offsetof(struct flb_in_metrics, sysfs_path),
Expand Down
2 changes: 2 additions & 0 deletions plugins/in_podman_metrics/podman_metrics_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,8 @@ struct flb_in_metrics {
int scrape_on_start;
int scrape_interval;
flb_sds_t podman_config_path;
flb_sds_t exclude_name_regex_text;
struct flb_regex *exclude_name_regex;

/* container list */
struct mk_list items;
Expand Down
Loading