From eadc3c55a78eea845a74402b17c84bb6f3edc5f4 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Mon, 6 Jul 2026 16:13:44 +0900 Subject: [PATCH 1/2] in_node_exporter_metrics: Add a capability to adjust log level w/ error paths Signed-off-by: Hiroshi Hatake --- .../in_node_exporter_metrics/ne_cpu_linux.c | 20 +++-- plugins/in_node_exporter_metrics/ne_utils.c | 82 +++++++++++++------ plugins/in_node_exporter_metrics/ne_utils.h | 16 ++++ 3 files changed, 85 insertions(+), 33 deletions(-) diff --git a/plugins/in_node_exporter_metrics/ne_cpu_linux.c b/plugins/in_node_exporter_metrics/ne_cpu_linux.c index bc7c8a9feec..67f85b7cb53 100644 --- a/plugins/in_node_exporter_metrics/ne_cpu_linux.c +++ b/plugins/in_node_exporter_metrics/ne_cpu_linux.c @@ -130,10 +130,12 @@ static int cpu_thermal_update(struct flb_ne *ctx, uint64_t ts) } /* Package Metric: node_cpu_core_throttles_total */ - ret = ne_utils_file_read_uint64(ctx, ctx->path_sysfs, - entry->str, - "thermal_throttle", "core_throttle_count", - &core_throttle_count); + ret = ne_utils_file_read_uint64_at_level(ctx, ctx->path_sysfs, + entry->str, + "thermal_throttle", + "core_throttle_count", + &core_throttle_count, + FLB_LOG_DEBUG); if (ret != 0) { flb_plg_debug(ctx->ins, "CPU is missing core_throttle_count: %s", @@ -150,10 +152,12 @@ static int cpu_thermal_update(struct flb_ne *ctx, uint64_t ts) } /* Package Metric: node_cpu_package_throttles_total */ - ret = ne_utils_file_read_uint64(ctx, ctx->path_sysfs, - entry->str, - "thermal_throttle", "package_throttle_count", - &package_throttle_count); + ret = ne_utils_file_read_uint64_at_level(ctx, ctx->path_sysfs, + entry->str, + "thermal_throttle", + "package_throttle_count", + &package_throttle_count, + FLB_LOG_DEBUG); if (ret != 0) { flb_plg_debug(ctx->ins, "CPU is missing package_throttle_count: %s", diff --git a/plugins/in_node_exporter_metrics/ne_utils.c b/plugins/in_node_exporter_metrics/ne_utils.c index 591c856b54f..9005c6228d1 100644 --- a/plugins/in_node_exporter_metrics/ne_utils.c +++ b/plugins/in_node_exporter_metrics/ne_utils.c @@ -20,7 +20,7 @@ #include #include #include -#include "ne.h" +#include "ne_utils.h" /* required by stat(2), open(2) */ #include @@ -30,6 +30,34 @@ #include +static void ne_utils_file_log(struct flb_ne *ctx, int log_level, + const char *operation, const char *path) +{ + if (!ctx) { + flb_errno(); + return; + } + + switch (log_level) { + case FLB_LOG_TRACE: + flb_plg_trace(ctx->ins, "could not %s '%s'", operation, path); + break; + case FLB_LOG_DEBUG: + flb_plg_debug(ctx->ins, "could not %s '%s'", operation, path); + break; + case FLB_LOG_INFO: + flb_plg_info(ctx->ins, "could not %s '%s'", operation, path); + break; + case FLB_LOG_WARN: + flb_plg_warn(ctx->ins, "could not %s '%s'", operation, path); + break; + case FLB_LOG_ERROR: + default: + flb_plg_error(ctx->ins, "could not %s '%s'", operation, path); + break; + } +} + int ne_utils_str_to_double(char *str, double *out_val) { double val; @@ -70,6 +98,18 @@ int ne_utils_file_read_uint64(struct flb_ne *ctx, const char *path, const char *join_a, const char *join_b, uint64_t *out_val) +{ + return ne_utils_file_read_uint64_at_level(ctx, mount, path, join_a, join_b, + out_val, FLB_LOG_ERROR); +} + +int ne_utils_file_read_uint64_at_level(struct flb_ne *ctx, + const char *mount, + const char *path, + const char *join_a, + const char *join_b, + uint64_t *out_val, + int log_level) { int fd; int len; @@ -123,24 +163,14 @@ int ne_utils_file_read_uint64(struct flb_ne *ctx, fd = open(p, O_RDONLY); if (fd == -1) { - if (ctx) { - flb_plg_error(ctx->ins, "could not open '%s'", p); - } - else { - flb_errno(); - } + ne_utils_file_log(ctx, log_level, "open", p); flb_sds_destroy(p); return -1; } bytes = read(fd, &tmp, sizeof(tmp)); if (bytes == -1) { - if (ctx) { - flb_plg_error(ctx->ins, "could not read from '%s'", p); - } - else { - flb_errno(); - } + ne_utils_file_log(ctx, log_level, "read from", p); close(fd); flb_sds_destroy(p); return -1; @@ -220,6 +250,18 @@ int ne_utils_file_read_sds(struct flb_ne *ctx, const char *join_a, const char *join_b, flb_sds_t *str) +{ + return ne_utils_file_read_sds_at_level(ctx, mount, path, join_a, join_b, + str, FLB_LOG_ERROR); +} + +int ne_utils_file_read_sds_at_level(struct flb_ne *ctx, + const char *mount, + const char *path, + const char *join_a, + const char *join_b, + flb_sds_t *str, + int log_level) { int fd; int len; @@ -269,24 +311,14 @@ int ne_utils_file_read_sds(struct flb_ne *ctx, fd = open(p, O_RDONLY); if (fd == -1) { - if (ctx) { - flb_plg_error(ctx->ins, "could not open '%s'", p); - } - else { - flb_errno(); - } + ne_utils_file_log(ctx, log_level, "open", p); flb_sds_destroy(p); return -1; } bytes = read(fd, &tmp, sizeof(tmp)); if (bytes == -1) { - if (ctx) { - flb_plg_error(ctx->ins, "could not read from '%s'", p); - } - else { - flb_errno(); - } + ne_utils_file_log(ctx, log_level, "read from", p); close(fd); flb_sds_destroy(p); return -1; diff --git a/plugins/in_node_exporter_metrics/ne_utils.h b/plugins/in_node_exporter_metrics/ne_utils.h index a365fbd4937..70089316e99 100644 --- a/plugins/in_node_exporter_metrics/ne_utils.h +++ b/plugins/in_node_exporter_metrics/ne_utils.h @@ -34,6 +34,14 @@ int ne_utils_file_read_uint64(struct flb_ne *ctx, const char *join_a, const char *join_b, uint64_t *out_val); +int ne_utils_file_read_uint64_at_level(struct flb_ne *ctx, + const char *mount, + const char *path, + const char *join_a, + const char *join_b, + uint64_t *out_val, + int log_level); + int ne_utils_file_read_sds(struct flb_ne *ctx, const char *mount, const char *path, @@ -41,6 +49,14 @@ int ne_utils_file_read_sds(struct flb_ne *ctx, const char *join_b, flb_sds_t *str); +int ne_utils_file_read_sds_at_level(struct flb_ne *ctx, + const char *mount, + const char *path, + const char *join_a, + const char *join_b, + flb_sds_t *str, + int log_level); + int ne_utils_file_read_lines(struct flb_ne *ctx, const char *mount, const char *path, From 7325cee8e3a44df4df1bef3cf3f989bdeb070cc3 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Mon, 6 Jul 2026 16:39:29 +0900 Subject: [PATCH 2/2] in_node_exporter_metrics: Handle errno on log utility Signed-off-by: Hiroshi Hatake --- plugins/in_node_exporter_metrics/ne_utils.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/plugins/in_node_exporter_metrics/ne_utils.c b/plugins/in_node_exporter_metrics/ne_utils.c index 9005c6228d1..543de19498e 100644 --- a/plugins/in_node_exporter_metrics/ne_utils.c +++ b/plugins/in_node_exporter_metrics/ne_utils.c @@ -30,14 +30,19 @@ #include -static void ne_utils_file_log(struct flb_ne *ctx, int log_level, +static void ne_utils_file_log(struct flb_ne *ctx, int log_level, int err_num, const char *operation, const char *path) { if (!ctx) { + errno = err_num; flb_errno(); return; } + if (err_num != ENOENT) { + log_level = FLB_LOG_ERROR; + } + switch (log_level) { case FLB_LOG_TRACE: flb_plg_trace(ctx->ins, "could not %s '%s'", operation, path); @@ -163,14 +168,14 @@ int ne_utils_file_read_uint64_at_level(struct flb_ne *ctx, fd = open(p, O_RDONLY); if (fd == -1) { - ne_utils_file_log(ctx, log_level, "open", p); + ne_utils_file_log(ctx, log_level, errno, "open", p); flb_sds_destroy(p); return -1; } bytes = read(fd, &tmp, sizeof(tmp)); if (bytes == -1) { - ne_utils_file_log(ctx, log_level, "read from", p); + ne_utils_file_log(ctx, log_level, errno, "read from", p); close(fd); flb_sds_destroy(p); return -1; @@ -311,14 +316,14 @@ int ne_utils_file_read_sds_at_level(struct flb_ne *ctx, fd = open(p, O_RDONLY); if (fd == -1) { - ne_utils_file_log(ctx, log_level, "open", p); + ne_utils_file_log(ctx, log_level, errno, "open", p); flb_sds_destroy(p); return -1; } bytes = read(fd, &tmp, sizeof(tmp)); if (bytes == -1) { - ne_utils_file_log(ctx, log_level, "read from", p); + ne_utils_file_log(ctx, log_level, errno, "read from", p); close(fd); flb_sds_destroy(p); return -1;