Skip to content

Commit 1e2bb3e

Browse files
authored
out_http: default the output format to JSON (#8493)
* out_http: default the output format to JSON. * out_http: add test to make sure default settings work with in_http. Signed-off-by: Phillip Whelan <[email protected]> --------- Signed-off-by: Phillip Whelan <[email protected]>
1 parent ea71935 commit 1e2bb3e

File tree

4 files changed

+133
-7
lines changed

4 files changed

+133
-7
lines changed

plugins/out_http/http.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ static int http_post(struct flb_out_http *ctx,
186186
FLB_HTTP_MIME_JSON,
187187
sizeof(FLB_HTTP_MIME_JSON) - 1);
188188
}
189-
else {
189+
else if ((ctx->out_format == FLB_HTTP_OUT_MSGPACK)) {
190190
flb_http_add_header(c,
191191
FLB_HTTP_CONTENT_TYPE,
192192
sizeof(FLB_HTTP_CONTENT_TYPE) - 1,
@@ -665,8 +665,8 @@ static struct flb_config_map config_map[] = {
665665
"Set a HTTP header which value is the Tag"
666666
},
667667
{
668-
FLB_CONFIG_MAP_STR, "format", NULL,
669-
0, FLB_FALSE, 0,
668+
FLB_CONFIG_MAP_STR, "format", "json",
669+
0, FLB_TRUE, offsetof(struct flb_out_http, format),
670670
"Set desired payload format: json, json_stream, json_lines, gelf or msgpack"
671671
},
672672
{

plugins/out_http/http.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ struct flb_out_http {
5555

5656
/* Output format */
5757
int out_format;
58+
flb_sds_t format;
5859

5960
int json_date_format;
6061
flb_sds_t json_date_key;

plugins/out_http/http_conf.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -208,13 +208,15 @@ struct flb_out_http *flb_http_conf_create(struct flb_output_instance *ins,
208208

209209
/* Output format */
210210
ctx->out_format = FLB_PACK_JSON_FORMAT_NONE;
211-
tmp = flb_output_get_property("format", ins);
212-
if (tmp) {
213-
if (strcasecmp(tmp, "gelf") == 0) {
211+
if (ctx->format) {
212+
if (strcasecmp(ctx->format, "gelf") == 0) {
214213
ctx->out_format = FLB_HTTP_OUT_GELF;
215214
}
215+
else if (strcasecmp(ctx->format, "msgpack") == 0) {
216+
ctx->out_format = FLB_HTTP_OUT_MSGPACK;
217+
}
216218
else {
217-
ret = flb_pack_to_json_format_type(tmp);
219+
ret = flb_pack_to_json_format_type(ctx->format);
218220
if (ret == -1) {
219221
flb_plg_error(ctx->ins, "unrecognized 'format' option. "
220222
"Using 'msgpack'");

tests/runtime/out_http.c

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,128 @@ void flb_test_json_date_format_java_sql_timestamp()
10401040
test_ctx_destroy(ctx);
10411041
}
10421042

1043+
static struct test_ctx *test_ctx_create_in_http(struct flb_lib_out_cb *cb)
1044+
{
1045+
int i_ffd;
1046+
int o_ffd;
1047+
struct test_ctx *ctx = NULL;
1048+
1049+
ctx = flb_malloc(sizeof(struct test_ctx));
1050+
if (!TEST_CHECK(ctx != NULL)) {
1051+
TEST_MSG("malloc failed");
1052+
flb_errno();
1053+
return NULL;
1054+
}
1055+
1056+
/* Service config */
1057+
ctx->flb = flb_create();
1058+
flb_service_set(ctx->flb,
1059+
"Flush", "0.200000000",
1060+
"Grace", "1",
1061+
"Log_Level", "error",
1062+
NULL);
1063+
1064+
/* Input */
1065+
i_ffd = flb_input(ctx->flb, (char *) "http", NULL);
1066+
TEST_CHECK(i_ffd >= 0);
1067+
ctx->i_ffd = i_ffd;
1068+
1069+
/* Output */
1070+
o_ffd = flb_output(ctx->flb, (char *) "lib", cb);
1071+
ctx->o_ffd = o_ffd;
1072+
1073+
return ctx;
1074+
}
1075+
1076+
int callback_test(void* data, size_t size, void* cb_data)
1077+
{
1078+
int num;
1079+
1080+
if (size > 0) {
1081+
num = get_output_num();
1082+
set_output_num(num+1);
1083+
}
1084+
return 0;
1085+
}
1086+
1087+
/* test to make sure out_http is always able to work with in_http by default. */
1088+
void flb_test_in_http()
1089+
{
1090+
struct test_ctx *ctx_in_http;
1091+
struct test_ctx *ctx_out_http;
1092+
int ret;
1093+
int num;
1094+
struct flb_lib_out_cb cb;
1095+
1096+
cb.cb = callback_test;
1097+
cb.data = NULL;
1098+
clear_output_num();
1099+
1100+
char *buf1 = "[1, {\"msg\":\"hello world\"}]";
1101+
size_t size1 = strlen(buf1);
1102+
char *buf2 = "[2, {\"msg\":\"hello world\"}]";
1103+
size_t size2 = strlen(buf2);
1104+
1105+
ctx_in_http = test_ctx_create_in_http(&cb);
1106+
if (!TEST_CHECK(ctx_in_http != NULL)) {
1107+
TEST_MSG("test_ctx_create failed");
1108+
exit(EXIT_FAILURE);
1109+
}
1110+
1111+
ctx_out_http = test_ctx_create();
1112+
if (!TEST_CHECK(ctx_out_http != NULL)) {
1113+
TEST_MSG("test_ctx_create failed");
1114+
exit(EXIT_FAILURE);
1115+
}
1116+
1117+
ret = flb_input_set(ctx_in_http->flb,
1118+
ctx_in_http->i_ffd,
1119+
"port", "8888",
1120+
"host", "127.0.0.1",
1121+
NULL);
1122+
TEST_CHECK(ret == 0);
1123+
1124+
/* explicitly do not set anything beyond match, port and localhost
1125+
* to be sure the default options work with in_http.
1126+
*/
1127+
ret = flb_output_set(ctx_out_http->flb,
1128+
ctx_out_http->o_ffd,
1129+
"match", "*",
1130+
"host", "127.0.0.1",
1131+
"port", "8888",
1132+
NULL);
1133+
TEST_CHECK(ret == 0);
1134+
1135+
/* Start the engines */
1136+
ret = flb_start(ctx_in_http->flb);
1137+
TEST_CHECK(ret == 0);
1138+
ret = flb_start(ctx_out_http->flb);
1139+
TEST_CHECK(ret == 0);
1140+
1141+
/* Ingest data sample */
1142+
ret = flb_lib_push(ctx_out_http->flb,
1143+
ctx_out_http->i_ffd,
1144+
(char *) buf1,
1145+
size1);
1146+
TEST_CHECK(ret >= 0);
1147+
ret = flb_lib_push(ctx_out_http->flb,
1148+
ctx_out_http->i_ffd,
1149+
(char *) buf2,
1150+
size2);
1151+
TEST_CHECK(ret >= 0);
1152+
1153+
/* waiting to flush */
1154+
flb_time_msleep(500);
1155+
1156+
num = get_output_num();
1157+
if (!TEST_CHECK(num > 0)) {
1158+
TEST_MSG("no outputs");
1159+
}
1160+
1161+
test_ctx_destroy(ctx_out_http);
1162+
test_ctx_destroy(ctx_in_http);
1163+
}
1164+
10431165
/* Test list */
10441166
TEST_LIST = {
10451167
{"format_msgpack" , flb_test_format_msgpack},
@@ -1056,5 +1178,6 @@ TEST_LIST = {
10561178
{"json_date_format_epoch" , flb_test_json_date_format_epoch},
10571179
{"json_date_format_iso8601" , flb_test_json_date_format_iso8601},
10581180
{"json_date_format_java_sql_timestamp" , flb_test_json_date_format_java_sql_timestamp},
1181+
{"in_http", flb_test_in_http},
10591182
{NULL, NULL}
10601183
};

0 commit comments

Comments
 (0)