diff --git a/plugins/in_podman_metrics/podman_metrics.c b/plugins/in_podman_metrics/podman_metrics.c index 342649bf624..47fc2297ca0 100644 --- a/plugins/in_podman_metrics/podman_metrics.c +++ b/plugins/in_podman_metrics/podman_metrics.c @@ -22,6 +22,7 @@ #include #include #include +#include #include @@ -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++; } } } @@ -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; } ctx->coll_fd_runtime = coll_fd_runtime; @@ -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; } @@ -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; } diff --git a/plugins/in_podman_metrics/podman_metrics.h b/plugins/in_podman_metrics/podman_metrics.h index db503225a3e..45b57ee9eff 100644 --- a/plugins/in_podman_metrics/podman_metrics.h +++ b/plugins/in_podman_metrics/podman_metrics.h @@ -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), diff --git a/plugins/in_podman_metrics/podman_metrics_config.h b/plugins/in_podman_metrics/podman_metrics_config.h index 1f6133e199a..4d87e85a79f 100644 --- a/plugins/in_podman_metrics/podman_metrics_config.h +++ b/plugins/in_podman_metrics/podman_metrics_config.h @@ -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;