Skip to content

Commit

Permalink
metrics: allocate metrics titles dynamically (#8969)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
cosmo0920 authored Jun 26, 2024
1 parent 9ec5374 commit 060418c
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 11 deletions.
7 changes: 5 additions & 2 deletions include/fluent-bit/flb_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
};
Expand Down
46 changes: 37 additions & 9 deletions src/flb_metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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));
Expand All @@ -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 */
Expand All @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 060418c

Please sign in to comment.