Skip to content

Commit

Permalink
out_http: default the output format to JSON (#8493)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
pwhelan authored Mar 12, 2024
1 parent ea71935 commit 1e2bb3e
Show file tree
Hide file tree
Showing 4 changed files with 133 additions and 7 deletions.
6 changes: 3 additions & 3 deletions plugins/out_http/http.c
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ static int http_post(struct flb_out_http *ctx,
FLB_HTTP_MIME_JSON,
sizeof(FLB_HTTP_MIME_JSON) - 1);
}
else {
else if ((ctx->out_format == FLB_HTTP_OUT_MSGPACK)) {
flb_http_add_header(c,
FLB_HTTP_CONTENT_TYPE,
sizeof(FLB_HTTP_CONTENT_TYPE) - 1,
Expand Down Expand Up @@ -665,8 +665,8 @@ static struct flb_config_map config_map[] = {
"Set a HTTP header which value is the Tag"
},
{
FLB_CONFIG_MAP_STR, "format", NULL,
0, FLB_FALSE, 0,
FLB_CONFIG_MAP_STR, "format", "json",
0, FLB_TRUE, offsetof(struct flb_out_http, format),
"Set desired payload format: json, json_stream, json_lines, gelf or msgpack"
},
{
Expand Down
1 change: 1 addition & 0 deletions plugins/out_http/http.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ struct flb_out_http {

/* Output format */
int out_format;
flb_sds_t format;

int json_date_format;
flb_sds_t json_date_key;
Expand Down
10 changes: 6 additions & 4 deletions plugins/out_http/http_conf.c
Original file line number Diff line number Diff line change
Expand Up @@ -208,13 +208,15 @@ struct flb_out_http *flb_http_conf_create(struct flb_output_instance *ins,

/* Output format */
ctx->out_format = FLB_PACK_JSON_FORMAT_NONE;
tmp = flb_output_get_property("format", ins);
if (tmp) {
if (strcasecmp(tmp, "gelf") == 0) {
if (ctx->format) {
if (strcasecmp(ctx->format, "gelf") == 0) {
ctx->out_format = FLB_HTTP_OUT_GELF;
}
else if (strcasecmp(ctx->format, "msgpack") == 0) {
ctx->out_format = FLB_HTTP_OUT_MSGPACK;
}
else {
ret = flb_pack_to_json_format_type(tmp);
ret = flb_pack_to_json_format_type(ctx->format);
if (ret == -1) {
flb_plg_error(ctx->ins, "unrecognized 'format' option. "
"Using 'msgpack'");
Expand Down
123 changes: 123 additions & 0 deletions tests/runtime/out_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -1040,6 +1040,128 @@ void flb_test_json_date_format_java_sql_timestamp()
test_ctx_destroy(ctx);
}

static struct test_ctx *test_ctx_create_in_http(struct flb_lib_out_cb *cb)
{
int i_ffd;
int o_ffd;
struct test_ctx *ctx = NULL;

ctx = flb_malloc(sizeof(struct test_ctx));
if (!TEST_CHECK(ctx != NULL)) {
TEST_MSG("malloc failed");
flb_errno();
return NULL;
}

/* Service config */
ctx->flb = flb_create();
flb_service_set(ctx->flb,
"Flush", "0.200000000",
"Grace", "1",
"Log_Level", "error",
NULL);

/* Input */
i_ffd = flb_input(ctx->flb, (char *) "http", NULL);
TEST_CHECK(i_ffd >= 0);
ctx->i_ffd = i_ffd;

/* Output */
o_ffd = flb_output(ctx->flb, (char *) "lib", cb);
ctx->o_ffd = o_ffd;

return ctx;
}

int callback_test(void* data, size_t size, void* cb_data)
{
int num;

if (size > 0) {
num = get_output_num();
set_output_num(num+1);
}
return 0;
}

/* test to make sure out_http is always able to work with in_http by default. */
void flb_test_in_http()
{
struct test_ctx *ctx_in_http;
struct test_ctx *ctx_out_http;
int ret;
int num;
struct flb_lib_out_cb cb;

cb.cb = callback_test;
cb.data = NULL;
clear_output_num();

char *buf1 = "[1, {\"msg\":\"hello world\"}]";
size_t size1 = strlen(buf1);
char *buf2 = "[2, {\"msg\":\"hello world\"}]";
size_t size2 = strlen(buf2);

ctx_in_http = test_ctx_create_in_http(&cb);
if (!TEST_CHECK(ctx_in_http != NULL)) {
TEST_MSG("test_ctx_create failed");
exit(EXIT_FAILURE);
}

ctx_out_http = test_ctx_create();
if (!TEST_CHECK(ctx_out_http != NULL)) {
TEST_MSG("test_ctx_create failed");
exit(EXIT_FAILURE);
}

ret = flb_input_set(ctx_in_http->flb,
ctx_in_http->i_ffd,
"port", "8888",
"host", "127.0.0.1",
NULL);
TEST_CHECK(ret == 0);

/* explicitly do not set anything beyond match, port and localhost
* to be sure the default options work with in_http.
*/
ret = flb_output_set(ctx_out_http->flb,
ctx_out_http->o_ffd,
"match", "*",
"host", "127.0.0.1",
"port", "8888",
NULL);
TEST_CHECK(ret == 0);

/* Start the engines */
ret = flb_start(ctx_in_http->flb);
TEST_CHECK(ret == 0);
ret = flb_start(ctx_out_http->flb);
TEST_CHECK(ret == 0);

/* Ingest data sample */
ret = flb_lib_push(ctx_out_http->flb,
ctx_out_http->i_ffd,
(char *) buf1,
size1);
TEST_CHECK(ret >= 0);
ret = flb_lib_push(ctx_out_http->flb,
ctx_out_http->i_ffd,
(char *) buf2,
size2);
TEST_CHECK(ret >= 0);

/* waiting to flush */
flb_time_msleep(500);

num = get_output_num();
if (!TEST_CHECK(num > 0)) {
TEST_MSG("no outputs");
}

test_ctx_destroy(ctx_out_http);
test_ctx_destroy(ctx_in_http);
}

/* Test list */
TEST_LIST = {
{"format_msgpack" , flb_test_format_msgpack},
Expand All @@ -1056,5 +1178,6 @@ TEST_LIST = {
{"json_date_format_epoch" , flb_test_json_date_format_epoch},
{"json_date_format_iso8601" , flb_test_json_date_format_iso8601},
{"json_date_format_java_sql_timestamp" , flb_test_json_date_format_java_sql_timestamp},
{"in_http", flb_test_in_http},
{NULL, NULL}
};

0 comments on commit 1e2bb3e

Please sign in to comment.