diff --git a/src/cmt_encode_influx.c b/src/cmt_encode_influx.c index 1dfe379..747355d 100644 --- a/src/cmt_encode_influx.c +++ b/src/cmt_encode_influx.c @@ -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; @@ -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 */ @@ -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); } } @@ -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); @@ -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); } diff --git a/tests/encoding.c b/tests/encoding.c index 9d4478e..b962a46 100644 --- a/tests/encoding.c +++ b/tests/encoding.c @@ -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; @@ -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},