From 060418c51bea221f0f6a1992f44dbdee7845f490 Mon Sep 17 00:00:00 2001 From: Hiroshi Hatake Date: Wed, 26 Jun 2024 11:44:57 +0900 Subject: [PATCH] metrics: allocate metrics titles dynamically (#8969) * metrics: Allocate metrics' titles dynamically However, still aded limitations over 1024 characters to prevent a waste of memory consumptions for title names. Signed-off-by: Hiroshi Hatake --- include/fluent-bit/flb_metrics.h | 7 +++-- src/flb_metrics.c | 46 +++++++++++++++++++++++++------- 2 files changed, 42 insertions(+), 11 deletions(-) diff --git a/include/fluent-bit/flb_metrics.h b/include/fluent-bit/flb_metrics.h index 25b0443224c..1aa0676f90d 100644 --- a/include/fluent-bit/flb_metrics.h +++ b/include/fluent-bit/flb_metrics.h @@ -56,17 +56,20 @@ #define FLB_METRIC_OUT_DROPPED_RECORDS 15 /* dropped_records_total */ #define FLB_METRIC_OUT_RETRIED_RECORDS 16 /* retried_records_total */ +/* The limitation of title name length */ +#define FLB_METRIC_LENGTH_LIMIT 1024 + struct flb_metric { int id; int title_len; - char title[64]; + char *title; size_t val; struct mk_list _head; }; struct flb_metrics { int title_len; /* Title string length */ - char title[64]; /* Title or id for this metrics context */ + char *title; /* Title or id for this metrics context */ int count; /* Total count of metrics registered */ struct mk_list list; /* Head of metrics list */ }; diff --git a/src/flb_metrics.c b/src/flb_metrics.c index 5ac4e6af2d2..5b50b138c39 100644 --- a/src/flb_metrics.c +++ b/src/flb_metrics.c @@ -78,6 +78,9 @@ struct flb_metrics *flb_metrics_create(const char *title) { int ret; struct flb_metrics *metrics; + size_t title_len = 0; + char *allocated_title = NULL; + size_t threshold = FLB_METRIC_LENGTH_LIMIT; /* Create a metrics parent context */ metrics = flb_malloc(sizeof(struct flb_metrics)); @@ -87,9 +90,24 @@ struct flb_metrics *flb_metrics_create(const char *title) } metrics->count = 0; + title_len = snprintf(NULL, 0, "%s", title); + + if (title_len > threshold) { + title_len = threshold; + flb_warn("[%s] title '%s' was truncated", __FUNCTION__, title); + } + + allocated_title = flb_calloc(title_len + 1, sizeof(char)); + if (allocated_title == NULL) { + flb_free(metrics); + return NULL; + } + metrics->title = allocated_title; + /* Set metrics title */ ret = flb_metrics_title(title, metrics); if (ret == -1) { + flb_free(metrics->title); flb_free(metrics); return NULL; } @@ -102,16 +120,14 @@ struct flb_metrics *flb_metrics_create(const char *title) int flb_metrics_title(const char *title, struct flb_metrics *metrics) { int ret; - size_t size = sizeof(metrics->title) - 1; + size_t size = snprintf(NULL, 0, "%s", title); ret = snprintf(metrics->title, size, "%s", title); if (ret == -1) { flb_errno(); return -1; } - else if (ret >= size){ - flb_warn("[%s] title '%s' was truncated", __FUNCTION__, title); - } + metrics->title_len = strlen(metrics->title); return 0; } @@ -121,6 +137,7 @@ int flb_metrics_add(int id, const char *title, struct flb_metrics *metrics) int ret; struct flb_metric *m; size_t size; + size_t threshold = FLB_METRIC_LENGTH_LIMIT; /* Create context */ m = flb_malloc(sizeof(struct flb_metric)); @@ -129,19 +146,27 @@ int flb_metrics_add(int id, const char *title, struct flb_metrics *metrics) return -1; } m->val = 0; - size = sizeof(m->title) - 1; + size = strlen(title); + + if (size > threshold) { + size = threshold; + flb_warn("[%s] title '%s' was truncated", __FUNCTION__, title); + } + + m->title = flb_calloc(size + 1, sizeof(char)); + if (m->title == NULL) { + flb_free(m); + return -1; + } /* Write title */ ret = snprintf(m->title, size, "%s", title); if (ret == -1) { flb_errno(); + flb_free(m->title); flb_free(m); return -1; } - else if (ret >= size) { - flb_warn("[%s] title '%s' was truncated", __FUNCTION__, title); - } - m->title_len = strlen(m->title); /* Assign an ID */ @@ -150,6 +175,7 @@ int flb_metrics_add(int id, const char *title, struct flb_metrics *metrics) if (id_exists(id, metrics) == FLB_TRUE) { flb_error("[metrics] id=%i already exists for metric '%s'", id, metrics->title); + flb_free(m->title); flb_free(m); return -1; } @@ -189,10 +215,12 @@ int flb_metrics_destroy(struct flb_metrics *metrics) mk_list_foreach_safe(head, tmp, &metrics->list) { m = mk_list_entry(head, struct flb_metric, _head); mk_list_del(&m->_head); + flb_free(m->title); flb_free(m); count++; } + flb_free(metrics->title); flb_free(metrics); return count; }