From 723fa77f9904d6678729ca81df82f9a845c1dbe5 Mon Sep 17 00:00:00 2001 From: Ankur Patel Date: Tue, 26 Nov 2024 18:43:42 +0530 Subject: [PATCH] in_http: use 'tag_key' option when json array is received 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 --- plugins/in_http/http_prot.c | 5 +++++ tests/runtime/in_http.c | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/plugins/in_http/http_prot.c b/plugins/in_http/http_prot.c index 175fe4461e1..4e2aa6a761c 100644 --- a/plugins/in_http/http_prot.c +++ b/plugins/in_http/http_prot.c @@ -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); diff --git a/tests/runtime/in_http.c b/tests/runtime/in_http.c index d4d88faadf8..66ddaea5230 100644 --- a/tests/runtime/in_http.c +++ b/tests/runtime/in_http.c @@ -588,7 +588,7 @@ void flb_test_http_failure_400_bad_disk_write() test_ctx_destroy(ctx); } -void flb_test_http_tag_key() +void test_http_tag_key(char *input) { struct flb_lib_out_cb cb_data; struct test_ctx *ctx; @@ -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(); @@ -661,12 +661,23 @@ void flb_test_http_tag_key() test_ctx_destroy(ctx); } +void flb_test_http_tag_key_with_map_input() +{ + test_http_tag_key("{\"tag\":\"new_tag\",\"test\":\"msg\"}"); +} + +void flb_test_http_tag_key_with_array_input() +{ + 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} };