Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

in_opentelemetry: support tag_from_uri #8134

Merged
merged 2 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions plugins/in_opentelemetry/opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
1 change: 1 addition & 0 deletions plugins/in_opentelemetry/opentelemetry.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
4 changes: 2 additions & 2 deletions plugins/in_opentelemetry/opentelemetry_prot.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
75 changes: 75 additions & 0 deletions tests/runtime/in_opentelemetry.c
Original file line number Diff line number Diff line change
Expand Up @@ -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}
};

Loading