Skip to content

Commit

Permalink
in_http: use 'tag_key' option when json array is received
Browse files Browse the repository at this point in the history
When a json array is received by http input, it doesn't use the
'tag_key' option and always sets the tag to 'http.N'. So this
fixes the bug and also adds test cases to test for both json object
and json array.

Signed-off-by: Ankur Patel <[email protected]>
  • Loading branch information
imankurpatel000 committed Nov 13, 2024
1 parent 50512ed commit a046f56
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
5 changes: 5 additions & 0 deletions plugins/in_http/http_prot.c
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,11 @@ static int process_pack_ng(struct flb_http *ctx, flb_sds_t tag, char *buf, size_
{
record = obj->via.array.ptr[i];

tag_from_record = NULL;
if (ctx->tag_key) {
tag_from_record = tag_key(ctx, &record);
}

if (tag_from_record) {
ret = process_pack_record(ctx, &tm, tag_from_record, &record);
flb_sds_destroy(tag_from_record);
Expand Down
17 changes: 14 additions & 3 deletions tests/runtime/in_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -588,7 +588,7 @@ void flb_test_http_failure_400_bad_disk_write()
test_ctx_destroy(ctx);
}

void flb_test_http_tag_key()
void flb_test_http_tag_key(char *input)
{
struct flb_lib_out_cb cb_data;
struct test_ctx *ctx;
Expand All @@ -597,7 +597,7 @@ void flb_test_http_tag_key()
int num;
size_t b_sent;

char *buf = "{\"test\":\"msg\", \"tag\":\"new_tag\"}";
char *buf = input;

clear_output_num();

Expand Down Expand Up @@ -661,12 +661,23 @@ void flb_test_http_tag_key()
test_ctx_destroy(ctx);
}

void flb_test_http_tag_key_with_map_input()
{
flb_test_http_tag_key("{\"tag\":\"new_tag\",\"test\":\"msg\"}");
}

void flb_test_http_tag_key_with_array_input()
{
flb_test_http_tag_key("[{\"tag\":\"new_tag\",\"test\":\"msg\"}]");
}

TEST_LIST = {
{"http", flb_test_http},
{"successful_response_code_200", flb_test_http_successful_response_code_200},
{"successful_response_code_204", flb_test_http_successful_response_code_204},
{"failure_response_code_400_bad_json", flb_test_http_failure_400_bad_json},
{"failure_response_code_400_bad_disk_write", flb_test_http_failure_400_bad_disk_write},
{"tag_key", flb_test_http_tag_key},
{"tag_key_with_map_input", flb_test_http_tag_key_with_map_input},
{"tag_key_with_array_input", flb_test_http_tag_key_with_array_input},
{NULL, NULL}
};

0 comments on commit a046f56

Please sign in to comment.