Skip to content

Commit ee86e0c

Browse files
cosmo0920edsiper
authored andcommitted
encode_influx: Handle no namespace case
This issue is reported in fluent/fluent-bit#9238. Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 6f22893 commit ee86e0c

File tree

2 files changed

+76
-11
lines changed

2 files changed

+76
-11
lines changed

src/cmt_encode_influx.c

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,9 @@ static void format_metric(struct cmt *cmt, cfl_sds_t *buf, struct cmt_map *map,
226226
{
227227
int i;
228228
int n;
229-
int count = 0;
229+
int static_count = 0;
230230
int static_labels = 0;
231+
int has_namespace = CMT_FALSE;
231232
struct cmt_map_label *label_k;
232233
struct cmt_map_label *label_v;
233234
struct cfl_list *head;
@@ -241,19 +242,26 @@ static void format_metric(struct cmt *cmt, cfl_sds_t *buf, struct cmt_map *map,
241242
opts = map->opts;
242243

243244
/* Measurement */
244-
cfl_sds_cat_safe(buf, opts->ns, cfl_sds_len(opts->ns));
245-
246-
if (cfl_sds_len(opts->subsystem) > 0) {
247-
cfl_sds_cat_safe(buf, "_", 1);
248-
cfl_sds_cat_safe(buf, opts->subsystem, cfl_sds_len(opts->subsystem));
245+
if (cfl_sds_len(opts->ns) > 0) {
246+
cfl_sds_cat_safe(buf, opts->ns, cfl_sds_len(opts->ns));
247+
if (cfl_sds_len(opts->subsystem) > 0) {
248+
cfl_sds_cat_safe(buf, "_", 1);
249+
cfl_sds_cat_safe(buf, opts->subsystem, cfl_sds_len(opts->subsystem));
250+
}
251+
has_namespace = CMT_TRUE;
252+
}
253+
else {
254+
has_namespace = CMT_FALSE;
249255
}
250256

251257
/* Static labels (tags) */
252258
static_labels = cmt_labels_count(cmt->static_labels);
253259
if (static_labels > 0) {
254-
cfl_sds_cat_safe(buf, ",", 1);
260+
if (has_namespace == CMT_TRUE) {
261+
cfl_sds_cat_safe(buf, ",", 1);
262+
}
255263
cfl_list_foreach(head, &cmt->static_labels->list) {
256-
count++;
264+
static_count++;
257265
slabel = cfl_list_entry(head, struct cmt_label, _head);
258266

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

268-
if (count < static_labels) {
276+
if (static_count < static_labels) {
269277
cfl_sds_cat_safe(buf, ",", 1);
270278
}
271279
}
@@ -274,7 +282,9 @@ static void format_metric(struct cmt *cmt, cfl_sds_t *buf, struct cmt_map *map,
274282
/* Labels / Tags */
275283
n = cfl_list_size(&metric->labels);
276284
if (n > 0) {
277-
cfl_sds_cat_safe(buf, ",", 1);
285+
if (static_labels > 0 || has_namespace == CMT_TRUE) {
286+
cfl_sds_cat_safe(buf, ",", 1);
287+
}
278288

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

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

300-
cfl_sds_cat_safe(buf, " ", 1);
310+
if (has_namespace == CMT_TRUE || static_labels > 0 || n > 0) {
311+
cfl_sds_cat_safe(buf, " ", 1);
312+
}
301313
append_metric_value(map, buf, metric);
302314
}
303315

tests/encoding.c

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -803,6 +803,58 @@ void test_influx()
803803
cmt_destroy(cmt);
804804
}
805805

806+
void test_influx_without_namespaces()
807+
{
808+
uint64_t ts;
809+
cfl_sds_t text;
810+
struct cmt *cmt;
811+
struct cmt_counter *c1;
812+
struct cmt_counter *c2;
813+
814+
char *out1 = \
815+
"test=1 1435658235000000123\n"
816+
"host=calyptia.com,app=cmetrics test=2 1435658235000000123\n"
817+
"host=aaa,app=bbb nosubsystem=1 1435658235000000123\n";
818+
819+
char *out2 = \
820+
"dev=Calyptia,lang=C test=1 1435658235000000123\n"
821+
"dev=Calyptia,lang=C,host=calyptia.com,app=cmetrics test=2 1435658235000000123\n"
822+
"dev=Calyptia,lang=C,host=aaa,app=bbb nosubsystem=1 1435658235000000123\n";
823+
824+
cmt = cmt_create();
825+
TEST_CHECK(cmt != NULL);
826+
827+
c1 = cmt_counter_create(cmt, "", "", "test", "Static labels test",
828+
2, (char *[]) {"host", "app"});
829+
830+
ts = 1435658235000000123;
831+
cmt_counter_inc(c1, ts, 0, NULL);
832+
cmt_counter_inc(c1, ts, 2, (char *[]) {"calyptia.com", "cmetrics"});
833+
cmt_counter_inc(c1, ts, 2, (char *[]) {"calyptia.com", "cmetrics"});
834+
835+
c2 = cmt_counter_create(cmt, "", "", "nosubsystem", "No subsystem",
836+
2, (char *[]) {"host", "app"});
837+
838+
cmt_counter_inc(c2, ts, 2, (char *[]) {"aaa", "bbb"});
839+
840+
/* Encode to prometheus (no static labels) */
841+
text = cmt_encode_influx_create(cmt);
842+
printf("%s\n", text);
843+
TEST_CHECK(strcmp(text, out1) == 0);
844+
cmt_encode_influx_destroy(text);
845+
846+
/* append static labels */
847+
cmt_label_add(cmt, "dev", "Calyptia");
848+
cmt_label_add(cmt, "lang", "C");
849+
850+
text = cmt_encode_influx_create(cmt);
851+
printf("%s\n", text);
852+
TEST_CHECK(strcmp(text, out2) == 0);
853+
cmt_encode_influx_destroy(text);
854+
855+
cmt_destroy(cmt);
856+
}
857+
806858
void test_splunk_hec()
807859
{
808860
uint64_t ts;
@@ -1125,6 +1177,7 @@ TEST_LIST = {
11251177
{"prometheus", test_prometheus},
11261178
{"text", test_text},
11271179
{"influx", test_influx},
1180+
{"influx_without_namespaces", test_influx_without_namespaces},
11281181
{"splunk_hec", test_splunk_hec},
11291182
{"splunk_hec_floating_point", test_splunk_hec_floating_point},
11301183
{"splunk_hec_histogram", test_splunk_hec_histogram},

0 commit comments

Comments
 (0)