From 3a3bcf1cec4f27883989894b438d3d516aa27ab5 Mon Sep 17 00:00:00 2001 From: Takahiro Yamashita Date: Sat, 4 Nov 2023 09:09:44 +0900 Subject: [PATCH 1/2] tests: runtime: in_otel: add test for tag_from_uri Signed-off-by: Takahiro Yamashita --- tests/runtime/in_opentelemetry.c | 75 ++++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/tests/runtime/in_opentelemetry.c b/tests/runtime/in_opentelemetry.c index 6f721cbec19..c28c58dfb8c 100644 --- a/tests/runtime/in_opentelemetry.c +++ b/tests/runtime/in_opentelemetry.c @@ -367,10 +367,85 @@ void flb_test_otel_successful_response_code_204() flb_test_otel_successful_response_code("204"); } +void flb_test_otel_tag_from_uri_false() +{ + struct flb_lib_out_cb cb_data; + struct test_ctx *ctx; + struct flb_http_client *c; + int ret; + int num; + size_t b_sent; + + char *buf = TEST_MSG_OTEL_LOGS; + + clear_output_num(); + + cb_data.cb = cb_check_result_json; + cb_data.data = TEST_CB_MSG_OTEL_LOGS; + + ctx = test_ctx_create(&cb_data); + if (!TEST_CHECK(ctx != NULL)) { + TEST_MSG("test_ctx_create failed"); + exit(EXIT_FAILURE); + } + + ret = flb_output_set(ctx->flb, ctx->o_ffd, + "match", "not_uri", + "format", "json", + NULL); + TEST_CHECK(ret == 0); + + ret = flb_input_set(ctx->flb, ctx->i_ffd, + "tag_from_uri", "false", + "tag", "not_uri", + NULL); + TEST_CHECK(ret == 0); + + /* Start the engine */ + ret = flb_start(ctx->flb); + TEST_CHECK(ret == 0); + + ctx->httpc = http_client_ctx_create(); + TEST_CHECK(ctx->httpc != NULL); + + c = flb_http_client(ctx->httpc->u_conn, FLB_HTTP_POST, V1_ENDPOINT_LOGS, buf, strlen(buf), + "127.0.0.1", PORT_OTEL, NULL, 0); + ret = flb_http_add_header(c, FLB_HTTP_HEADER_CONTENT_TYPE, strlen(FLB_HTTP_HEADER_CONTENT_TYPE), + JSON_CONTENT_TYPE, strlen(JSON_CONTENT_TYPE)); + TEST_CHECK(ret == 0); + if (!TEST_CHECK(c != NULL)) { + TEST_MSG("http_client failed"); + exit(EXIT_FAILURE); + } + + ret = flb_http_do(c, &b_sent); + if (!TEST_CHECK(ret == 0)) { + TEST_MSG("ret error. ret=%d\n", ret); + } + else if (!TEST_CHECK(b_sent > 0)){ + TEST_MSG("b_sent size error. b_sent = %lu\n", b_sent); + } + else if (!TEST_CHECK(c->resp.status == 201)) { + TEST_MSG("http response code error. expect: 201, got: %d\n", c->resp.status); + } + + /* waiting to flush */ + flb_time_msleep(1500); + + num = get_output_num(); + if (!TEST_CHECK(num > 0)) { + TEST_MSG("no outputs"); + } + flb_http_client_destroy(c); + flb_upstream_conn_release(ctx->httpc->u_conn); + test_ctx_destroy(ctx); +} + TEST_LIST = { {"otel_logs", flb_test_otel_logs}, {"successful_response_code_200", flb_test_otel_successful_response_code_200}, {"successful_response_code_204", flb_test_otel_successful_response_code_204}, + {"tag_from_uri_false", flb_test_otel_tag_from_uri_false}, {NULL, NULL} }; From 5f773cf14671141efc59b9f2ac22ac8ee1183c35 Mon Sep 17 00:00:00 2001 From: Takahiro Yamashita Date: Sat, 4 Nov 2023 09:10:29 +0900 Subject: [PATCH 2/2] in_opentelemetry: support tag_from_uri Signed-off-by: Takahiro Yamashita --- plugins/in_opentelemetry/opentelemetry.c | 5 +++++ plugins/in_opentelemetry/opentelemetry.h | 1 + plugins/in_opentelemetry/opentelemetry_prot.c | 4 ++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/plugins/in_opentelemetry/opentelemetry.c b/plugins/in_opentelemetry/opentelemetry.c index 5cd26f8e69f..46ff4cc6812 100644 --- a/plugins/in_opentelemetry/opentelemetry.c +++ b/plugins/in_opentelemetry/opentelemetry.c @@ -169,6 +169,11 @@ static struct flb_config_map config_map[] = { 0, FLB_TRUE, offsetof(struct flb_opentelemetry, tag_key), "" }, + { + FLB_CONFIG_MAP_BOOL, "tag_from_uri", "true", + 0, FLB_TRUE, offsetof(struct flb_opentelemetry, tag_from_uri), + "If true, tag will be created from uri. e.g. v1_metrics from /v1/metrics ." + }, { FLB_CONFIG_MAP_INT, "successful_response_code", "201", 0, FLB_TRUE, offsetof(struct flb_opentelemetry, successful_response_code), diff --git a/plugins/in_opentelemetry/opentelemetry.h b/plugins/in_opentelemetry/opentelemetry.h index 512f2ab6fd7..57d8df86c69 100644 --- a/plugins/in_opentelemetry/opentelemetry.h +++ b/plugins/in_opentelemetry/opentelemetry.h @@ -35,6 +35,7 @@ struct flb_opentelemetry { flb_sds_t tcp_port; const char *tag_key; bool raw_traces; + int tag_from_uri; size_t buffer_max_size; /* Maximum buffer size */ size_t buffer_chunk_size; /* Chunk allocation size */ diff --git a/plugins/in_opentelemetry/opentelemetry_prot.c b/plugins/in_opentelemetry/opentelemetry_prot.c index c9ccba7f90e..4c7fde15cae 100644 --- a/plugins/in_opentelemetry/opentelemetry_prot.c +++ b/plugins/in_opentelemetry/opentelemetry_prot.c @@ -1568,8 +1568,8 @@ int opentelemetry_prot_handle(struct flb_opentelemetry *ctx, struct http_conn *c /* Compose the query string using the URI */ len = strlen(uri); - if (len == 1) { - tag = NULL; /* use default tag */ + if (ctx->tag_from_uri != FLB_TRUE) { + tag = flb_sds_create(ctx->ins->tag); } else { tag = flb_sds_create_size(len);