Skip to content

Commit

Permalink
encode_influx: Handle no namespace case
Browse files Browse the repository at this point in the history
This issue is reported in
fluent/fluent-bit#9238.

Signed-off-by: Hiroshi Hatake <hiroshi@chronosphere.io>
  • Loading branch information
cosmo0920 authored and edsiper committed Aug 20, 2024
1 parent 6f22893 commit ee86e0c
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 11 deletions.
34 changes: 23 additions & 11 deletions src/cmt_encode_influx.c
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,9 @@ static void format_metric(struct cmt *cmt, cfl_sds_t *buf, struct cmt_map *map,
{
int i;
int n;
int count = 0;
int static_count = 0;
int static_labels = 0;
int has_namespace = CMT_FALSE;
struct cmt_map_label *label_k;
struct cmt_map_label *label_v;
struct cfl_list *head;
Expand All @@ -241,19 +242,26 @@ static void format_metric(struct cmt *cmt, cfl_sds_t *buf, struct cmt_map *map,
opts = map->opts;

/* Measurement */
cfl_sds_cat_safe(buf, opts->ns, cfl_sds_len(opts->ns));

if (cfl_sds_len(opts->subsystem) > 0) {
cfl_sds_cat_safe(buf, "_", 1);
cfl_sds_cat_safe(buf, opts->subsystem, cfl_sds_len(opts->subsystem));
if (cfl_sds_len(opts->ns) > 0) {
cfl_sds_cat_safe(buf, opts->ns, cfl_sds_len(opts->ns));
if (cfl_sds_len(opts->subsystem) > 0) {
cfl_sds_cat_safe(buf, "_", 1);
cfl_sds_cat_safe(buf, opts->subsystem, cfl_sds_len(opts->subsystem));
}
has_namespace = CMT_TRUE;
}
else {
has_namespace = CMT_FALSE;
}

/* Static labels (tags) */
static_labels = cmt_labels_count(cmt->static_labels);
if (static_labels > 0) {
cfl_sds_cat_safe(buf, ",", 1);
if (has_namespace == CMT_TRUE) {
cfl_sds_cat_safe(buf, ",", 1);
}
cfl_list_foreach(head, &cmt->static_labels->list) {
count++;
static_count++;
slabel = cfl_list_entry(head, struct cmt_label, _head);

/* key */
Expand All @@ -265,7 +273,7 @@ static void format_metric(struct cmt *cmt, cfl_sds_t *buf, struct cmt_map *map,
/* val */
append_string(buf, slabel->val);

if (count < static_labels) {
if (static_count < static_labels) {
cfl_sds_cat_safe(buf, ",", 1);
}
}
Expand All @@ -274,7 +282,9 @@ static void format_metric(struct cmt *cmt, cfl_sds_t *buf, struct cmt_map *map,
/* Labels / Tags */
n = cfl_list_size(&metric->labels);
if (n > 0) {
cfl_sds_cat_safe(buf, ",", 1);
if (static_labels > 0 || has_namespace == CMT_TRUE) {
cfl_sds_cat_safe(buf, ",", 1);
}

label_k = cfl_list_entry_first(&map->label_keys, struct cmt_map_label, _head);

Expand All @@ -297,7 +307,9 @@ static void format_metric(struct cmt *cmt, cfl_sds_t *buf, struct cmt_map *map,
}
}

cfl_sds_cat_safe(buf, " ", 1);
if (has_namespace == CMT_TRUE || static_labels > 0 || n > 0) {
cfl_sds_cat_safe(buf, " ", 1);
}
append_metric_value(map, buf, metric);
}

Expand Down
53 changes: 53 additions & 0 deletions tests/encoding.c
Original file line number Diff line number Diff line change
Expand Up @@ -803,6 +803,58 @@ void test_influx()
cmt_destroy(cmt);
}

void test_influx_without_namespaces()
{
uint64_t ts;
cfl_sds_t text;
struct cmt *cmt;
struct cmt_counter *c1;
struct cmt_counter *c2;

char *out1 = \
"test=1 1435658235000000123\n"
"host=calyptia.com,app=cmetrics test=2 1435658235000000123\n"
"host=aaa,app=bbb nosubsystem=1 1435658235000000123\n";

char *out2 = \
"dev=Calyptia,lang=C test=1 1435658235000000123\n"
"dev=Calyptia,lang=C,host=calyptia.com,app=cmetrics test=2 1435658235000000123\n"
"dev=Calyptia,lang=C,host=aaa,app=bbb nosubsystem=1 1435658235000000123\n";

cmt = cmt_create();
TEST_CHECK(cmt != NULL);

c1 = cmt_counter_create(cmt, "", "", "test", "Static labels test",
2, (char *[]) {"host", "app"});

ts = 1435658235000000123;
cmt_counter_inc(c1, ts, 0, NULL);
cmt_counter_inc(c1, ts, 2, (char *[]) {"calyptia.com", "cmetrics"});
cmt_counter_inc(c1, ts, 2, (char *[]) {"calyptia.com", "cmetrics"});

c2 = cmt_counter_create(cmt, "", "", "nosubsystem", "No subsystem",
2, (char *[]) {"host", "app"});

cmt_counter_inc(c2, ts, 2, (char *[]) {"aaa", "bbb"});

/* Encode to prometheus (no static labels) */
text = cmt_encode_influx_create(cmt);
printf("%s\n", text);
TEST_CHECK(strcmp(text, out1) == 0);
cmt_encode_influx_destroy(text);

/* append static labels */
cmt_label_add(cmt, "dev", "Calyptia");
cmt_label_add(cmt, "lang", "C");

text = cmt_encode_influx_create(cmt);
printf("%s\n", text);
TEST_CHECK(strcmp(text, out2) == 0);
cmt_encode_influx_destroy(text);

cmt_destroy(cmt);
}

void test_splunk_hec()
{
uint64_t ts;
Expand Down Expand Up @@ -1125,6 +1177,7 @@ TEST_LIST = {
{"prometheus", test_prometheus},
{"text", test_text},
{"influx", test_influx},
{"influx_without_namespaces", test_influx_without_namespaces},
{"splunk_hec", test_splunk_hec},
{"splunk_hec_floating_point", test_splunk_hec_floating_point},
{"splunk_hec_histogram", test_splunk_hec_histogram},
Expand Down

0 comments on commit ee86e0c

Please sign in to comment.