Skip to content

Commit

Permalink
metrics: fix dynamic composing of old metric names (fix #9086)
Browse files Browse the repository at this point in the history
Signed-off-by: Eduardo Silva <[email protected]>
  • Loading branch information
edsiper committed Jul 16, 2024
1 parent 133ae8a commit f141ab5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 44 deletions.
6 changes: 2 additions & 4 deletions include/fluent-bit/flb_metrics.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,14 @@

struct flb_metric {
int id;
int title_len;
char *title;
flb_sds_t title;
size_t val;
struct mk_list _head;
};

struct flb_metrics {
int title_len; /* Title string length */
char *title; /* Title or id for this metrics context */
int count; /* Total count of metrics registered */
flb_sds_t title;
struct mk_list list; /* Head of metrics list */
};

Expand Down
65 changes: 25 additions & 40 deletions src/flb_metrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,31 +83,16 @@ struct flb_metrics *flb_metrics_create(const char *title)
size_t threshold = FLB_METRIC_LENGTH_LIMIT;

/* Create a metrics parent context */
metrics = flb_malloc(sizeof(struct flb_metrics));
metrics = flb_calloc(1, sizeof(struct flb_metrics));
if (!metrics) {
flb_errno();
return NULL;
}
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 @@ -120,23 +105,32 @@ 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 = snprintf(NULL, 0, "%s", title);
int len;

ret = snprintf(metrics->title, size, "%s", title);
if (ret == -1) {
len = strlen(title);
if (len > FLB_METRIC_LENGTH_LIMIT) {
flb_warn("[%s] title '%s' was truncated", __FUNCTION__, title);
len = FLB_METRIC_LENGTH_LIMIT;
}

if (metrics->title) {
flb_sds_destroy(metrics->title);
}

metrics->title = flb_sds_create_len(title, len);
if (!metrics->title) {
flb_errno();
return -1;
}

metrics->title_len = strlen(metrics->title);
return 0;
}

int flb_metrics_add(int id, const char *title, struct flb_metrics *metrics)
{
int ret;
int len;
struct flb_metric *m;
size_t size;
size_t threshold = FLB_METRIC_LENGTH_LIMIT;

/* Create context */
Expand All @@ -146,36 +140,27 @@ int flb_metrics_add(int id, const char *title, struct flb_metrics *metrics)
return -1;
}
m->val = 0;
size = strlen(title);
len = strlen(title);

if (size > threshold) {
size = threshold;
if (len > threshold) {
len = 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) {
m->title = flb_sds_create_len(title, len);
if (!m->title) {
flb_errno();
flb_free(m->title);
flb_free(m);
return -1;
}
m->title_len = strlen(m->title);

/* Assign an ID */
if (id >= 0) {
/* Check this new ID is available */
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_sds_destroy(m->title);
flb_free(m);
return -1;
}
Expand Down Expand Up @@ -215,12 +200,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_sds_destroy(m->title);
flb_free(m);
count++;
}

flb_free(metrics->title);
flb_sds_destroy(metrics->title);
flb_free(metrics);
return count;
}
Expand Down Expand Up @@ -258,8 +243,8 @@ int flb_metrics_dump_values(char **out_buf, size_t *out_size,

mk_list_foreach(head, &me->list) {
m = mk_list_entry(head, struct flb_metric, _head);
msgpack_pack_str(&mp_pck, m->title_len);
msgpack_pack_str_body(&mp_pck, m->title, m->title_len);
msgpack_pack_str(&mp_pck, flb_sds_len(m->title));
msgpack_pack_str_body(&mp_pck, m->title, flb_sds_len(m->title));
msgpack_pack_uint64(&mp_pck, m->val);
}

Expand Down

0 comments on commit f141ab5

Please sign in to comment.