Skip to content

Commit 4b5ad3b

Browse files
lbogdanedsiper
authored andcommitted
pack: gelf: replace '/' with '_' in keys when converting from msgpack (fluent#1166)
Allows logs from Kubernetes pods to have labels or annotations containing '/' by replacing it with '_', as '/' is not a valid GELF field name character. Signed-off-by: Bogdan Luca <[email protected]>
1 parent 0e72710 commit 4b5ad3b

File tree

1 file changed

+86
-17
lines changed

1 file changed

+86
-17
lines changed

src/flb_pack.c

Lines changed: 86 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,26 @@ static char *tokens_to_msgpack(const char *js,
194194
return buf;
195195
}
196196

197+
static char *str_copy_replace(const char *src, int len, char search, char replace) {
198+
char *dst = NULL;
199+
int i;
200+
201+
dst = flb_strndup(src, len);
202+
203+
if (!dst) {
204+
flb_errno();
205+
return NULL;
206+
}
207+
208+
for(i = 0; i < len; i++) {
209+
if (dst[i] == search) {
210+
dst[i] = replace;
211+
}
212+
}
213+
214+
return dst;
215+
}
216+
197217
/*
198218
* It parse a JSON string and convert it to MessagePack format, this packer is
199219
* useful when a complete JSON message exists, otherwise it will fail until
@@ -1005,58 +1025,107 @@ static flb_sds_t flb_msgpack_gelf_key(flb_sds_t *s, int in_array,
10051025
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10061026
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
10071027
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1028+
char *prefix_key_copy = NULL;
1029+
char *key_copy = NULL;
1030+
flb_sds_t ret;
1031+
1032+
if (prefix_key_len > 0) {
1033+
prefix_key_copy = str_copy_replace(prefix_key, prefix_key_len, '/', '_');
1034+
if (!prefix_key_copy) {
1035+
ret = NULL;
1036+
goto cleanup;
1037+
}
1038+
}
1039+
1040+
if (key_len > 0) {
1041+
key_copy = str_copy_replace(key, key_len, '/', '_');
1042+
if (!key_copy) {
1043+
ret = NULL;
1044+
goto cleanup;
1045+
}
1046+
}
10081047

10091048
/* check valid key char [A-Za-z0-9_\.\-] */
10101049
for(i=0; i < prefix_key_len; i++) {
1011-
if (!valid_char[(unsigned char)prefix_key[i]]) {
1012-
flb_debug("[%s] invalid key char '%.*s'", __FUNCTION__,
1013-
prefix_key_len, prefix_key);
1014-
return NULL;
1050+
if (!valid_char[(unsigned char)prefix_key_copy[i]]) {
1051+
flb_error("[%s] invalid prefix key char at pos %d: '%.*s'", __FUNCTION__,
1052+
i, prefix_key_len, prefix_key);
1053+
ret = NULL;
1054+
goto cleanup;
10151055
}
10161056
}
10171057
for(i=0; i < key_len; i++) {
1018-
if (!valid_char[(unsigned char)key[i]]) {
1019-
flb_debug("[%s] invalid key char '%.*s'", __FUNCTION__,
1020-
key_len, key);
1021-
return NULL;
1058+
if (!valid_char[(unsigned char)key_copy[i]]) {
1059+
flb_error("[%s] invalid key char at pos %d: '%.*s'", __FUNCTION__,
1060+
i, key_len, key);
1061+
ret = NULL;
1062+
goto cleanup;
10221063
}
10231064
}
10241065

10251066
if (in_array == FLB_FALSE) {
10261067
tmp = flb_sds_cat(*s, ", \"", 3);
1027-
if (tmp == NULL) return NULL;
1068+
if (tmp == NULL) {
1069+
ret = NULL;
1070+
goto cleanup;
1071+
}
10281072
*s = tmp;
10291073
}
10301074

10311075
if (prefix_key_len > 0) {
1032-
tmp = flb_sds_cat(*s, prefix_key, prefix_key_len);
1033-
if (tmp == NULL) return NULL;
1076+
tmp = flb_sds_cat(*s, prefix_key_copy, prefix_key_len);
1077+
if (tmp == NULL) {
1078+
ret = NULL;
1079+
goto cleanup;
1080+
}
10341081
*s = tmp;
10351082
}
10361083

10371084
if (concat == FLB_TRUE) {
10381085
tmp = flb_sds_cat(*s, "_", 1);
1039-
if (tmp == NULL) return NULL;
1086+
if (tmp == NULL) {
1087+
ret = NULL;
1088+
goto cleanup;
1089+
}
10401090
*s = tmp;
10411091
}
10421092

10431093
if (key_len > 0) {
1044-
tmp = flb_sds_cat(*s, key, key_len);
1045-
if (tmp == NULL) return NULL;
1094+
tmp = flb_sds_cat(*s, key_copy, key_len);
1095+
if (tmp == NULL) {
1096+
ret = NULL;
1097+
goto cleanup;
1098+
}
10461099
*s = tmp;
10471100
}
10481101

10491102
if (in_array == FLB_FALSE) {
10501103
tmp = flb_sds_cat(*s, "\":", 2);
1051-
if (tmp == NULL) return NULL;
1104+
if (tmp == NULL) {
1105+
ret = NULL;
1106+
goto cleanup;
1107+
}
10521108
*s = tmp;
10531109
} else {
10541110
tmp = flb_sds_cat(*s, "=", 1);
1055-
if (tmp == NULL) return NULL;
1111+
if (tmp == NULL) {
1112+
ret = NULL;
1113+
goto cleanup;
1114+
}
10561115
*s = tmp;
10571116
}
10581117

1059-
return *s;
1118+
ret = *s;
1119+
1120+
cleanup:
1121+
if (prefix_key_copy) {
1122+
flb_free(prefix_key_copy);
1123+
}
1124+
if (key_copy) {
1125+
flb_free(key_copy);
1126+
}
1127+
1128+
return ret;
10601129
}
10611130

10621131
static flb_sds_t flb_msgpack_gelf_value(flb_sds_t *s, int quote,

0 commit comments

Comments
 (0)